I have a csv file with lots of IP addresses, e.g.
216.156.156.26
192.225.161.138
...
182.16.169.129
I want to ping each IP address and append the average time to the csv file. E.g.
216.156.156.26, 134
192.225.161.138, 95
...
182.16.169.129, 83
So far I have the following code which will read the IPs from the csv file and ping them:
import os
import csv
with open('ip_ping.csv') as csvfile:
reader = csv.reader(csvfile)
for line in reader:
for ip in line:
response = os.system('ping ' + str(ip))
if response == 0:
print (ip, ' is up')
else:
print (ip, ' is down')
It gives me the following output:
Pinging 216.156.156.26 with 32 bytes of data:
Reply from 216.156.156.26: bytes=32 time=96ms TTL=54
Reply from 216.156.156.26: bytes=32 time=95ms TTL=54
Reply from 216.156.156.26: bytes=32 time=99ms TTL=54
Request timed out.
Ping statistics for 23.229.39.34:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
Request timed out.
Ping statistics for 23.229.39.34:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
23.229.39.34 is down
23.229.39.34 is down
Pinging 45.72.45.144 with 32 bytes of data:
Pinging 45.72.45.144 with 32 bytes of data:
Reply from 216.156.156.26: bytes=32 time=131ms TTL=54
Ping statistics for 216.156.156.26:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 95ms, Maximum = 131ms, Average = 105ms
216.156.156.26 is up
...
and so on
Any help on how to best do this would be greatly appreciated!