Reputation: 1
I am a fresher when it comes to Programming and my current work requires me to write a python script to continously ping a set of hosts and save the result to a CSV file with timestamps.
So my CSV file should fill up with logs when the host is down which i am tracking through "Request timed out"
import os
results_file = open("results.txt","w")
ip_list = []
len(ip_list)
for ip in range(1,11):
ip_list.append("10.100."+ str(ip)+ ".1")
len(ip_list)
for ip in ip_list:
response = os.popen(f"ping {ip} -t").read()
print(response)
if "Request timed out" in response:
results_file.write(f"DOWN {ip} Connection Unsuccessful" + "\n")
else:
results_file.write(f"Works fine" + "\n")
I managed to figure out how to ping hosts and check if they are UP of DOWN. But continous ping does not fetch any result.
What Can i Do?
Upvotes: 0
Views: 769
Reputation: 51
This works but im not sure what you intended with -t.
I used -c so that it sends only 1 request.
for ip in ip_list:
response = os.system(f"ping -c 1 {ip}")
print(response)
if response != 0:
results_file.write(f"DOWN {ip} Connection Unsuccessful" + "\n")
else:
results_file.write(f"Works fine" + "\n")
Upvotes: 1