import os
import socket
import csv
iplist = []
with open('result.txt', 'w') as f:
#loop from 1 to 254
# Appends the concatenated ip to the ip_list
# Outputs to results.txt file
for ip in range(1,255):
iplist.append("192.168.1." + str(ip))
#show me the list of ip address in the list
print("*********ADBY IT AND MEDIA SOLUTIONS NETWORK SCAN*********")
print("Starting ping test")
for ip in iplist:
response = os.popen(f"ping {ip} -n 1").read()
if "Received = 4" and "Approximate" in response:
try:
hostname = socket.gethostbyaddr(ip)
print('"{}",{},"ONLINE"'.format( hostname,ip), file=f)
except socket.error:
hostname = "No HOST NAME "
print('"{}",{},"ONLINE"'.format( hostname,ip), file=f)
#print({hostname} + "The System is online")
else:
print(f'"",{},"OFFLINE"'.format(ip), file=f)
and to: print(",{},OFFLINE".format
and print a optional header row first: print('"host","ip","status
and running the program with: python myping.py >results.txt
will do.