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?
continuous? You loop over the IPs and check only once. You need awhileloop (probably with some sleep inside) or a scheduler to do it "continuously".