title: Ping multiple ips and write to JSON file python
body: <p>I am pinging multiple ips in the LAN to check if it is alive or not. the code will be run every minute based on schedule.For pinging multiple ips, i used multiprocessing. It is done great with the help of multiprocessing. Simultaneously, i want to write the ping results to the json file after pinging. But When writing to the JSON file, it is writing only the output of the last ip.I want all the three.
  Is there any way to do this</p>
  
  <p>Here's the sample Code:</p>
  
  <pre><code>import json
  from multiprocessing import Pool
  import subprocess
  from datetime import datetime
  timestamp = datetime.now().strftime("%B %d %Y, %H:%M:%S")
  hosts =  ["192.168.1.47","192.168.1.42"]
  count = 1
  wait_sec = 1
  n = len(hosts)
  def main(hosts):
      p = Pool(processes= n)
      result = p.map(beat, hosts)
  def beat(hosts):
      #Name for the log file
      name = 'icmp.json'
      ip4write(hosts, name)
  def ip4write(hosts, name):
      global ip4a
      ip4a = hosts
      ipve4(hosts, name)
      write(hosts, name)
  def ipve4(hosts, name):
      global u
      status, result = subprocess.getstatusoutput("ping -c1 -w2 " + str(ip4a))
      if status == 0:
          print(str(ip4a) + " UP")
          u = " UP"
  def write(hosts, name):
      text_file = open(name, "a+")
      with open(name) as json_file:
        try:
            data = json.load(json_file)
        except:
            data = {}
        with open(name, 'w') as outfile:
          data[timestamp] = {
            'monitor.ip':str(hosts),
            'monitor.status': u
          }
          print(data)
          json.dump(data, outfile)
          print('Data written')
      text_file.close()
  main(hosts)
  </code></pre>
  
  <p>Output in JSON file:</p>
  
  <pre><code>{"February 15 2019, 16:38:12": {"monitor.status": " UP", "monitor.ip": "192.168.1.42"}}
  </code></pre>
  
  <p>My required Output:</p>
  
  <pre><code>{"February 15 2019, 16:38:12": {"monitor.ip": "192.168.1.47", "monitor.status": " UP"}, "February 15 2019, 16:38:12": {"monitor.ip": "192.168.1.42", "monitor.status": " UP"}}
  </code></pre>