troubleshooting Question

Write the output of my python ping script to a csv file

Avatar of adbyits
adbyits asked on
Operating SystemsNetworkingPython
13 Comments1 Solution325 ViewsLast Modified:
I would like to add the results of my ping test in python to a csv file i was hoping someone could please help me with this please as i have been pulling my hair out trying

here the code of my pings

import os
import socket
import csv

iplist = []

#loop from 1 to 255
# Appends the concatenated ip to the ip_list

for ip in range(1,256):
    iplist.append("192.168.1." + str(ip))

#show me the list of ip address in the list

# Loop to ping ip_list and check if device up or down
# Outputs to results.txt file
print("Starting ping test")
for ip in iplist:
    response = os.popen(f"ping  {ip} -n 1").read()
    if "Received = 4"  and "Approximate" in response:
            hostname = socket.gethostbyaddr(ip)

            print(" {}ONLINE".format( hostname))
        except socket.error:
            hostname = "No HOST NAME "

            print(" {}{} ONLINE".format( hostname,ip))

        #print({hostname} +  "The System is online")
       # print(f"{ip} OFFLINE")
nociSoftware Engineer
Join our community to see this answer!
Unlock 1 Answer and 13 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros