Skip to content

Instantly share code, notes, and snippets.

@ariesmcrae
Last active July 30, 2023 10:07
Show Gist options
  • Save ariesmcrae/64c69a020335892b00d4 to your computer and use it in GitHub Desktop.
Save ariesmcrae/64c69a020335892b00d4 to your computer and use it in GitHub Desktop.
Python3 script that will ping a list of servers in an external file. Output is unreachable_or_timeout.txt, server-not-found.txt, and server-ok.txt
import subprocess
import csv
def ping(hostname):
p = subprocess.Popen('ping ' + hostname, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
pingStatus = 'ok';
for line in p.stdout:
output = line.rstrip().decode('UTF-8')
if (output.endswith('unreachable.')) :
#No route from the local system. Packets sent were never put on the wire.
pingStatus = 'unreacheable'
break
elif (output.startswith('Ping request could not find host')) :
pingStatus = 'host_not_found'
break
if (output.startswith('Request timed out.')) :
#No Echo Reply messages were received within the default time of 1 second.
pingStatus = 'timed_out'
break
#end if
#endFor
return pingStatus
#endDef
def printPingResult(hostname):
statusOfPing = ping(hostname)
if (statusOfPing == 'host_not_found') :
writeToFile('!server-not-found.txt', hostname)
elif (statusOfPing == 'unreacheable') :
writeToFile('!unreachable.txt', hostname)
elif (statusOfPing == 'timed_out') :
writeToFile('!timed_out.txt', hostname)
elif (statusOfPing == 'ok') :
writeToFile('!ok.txt', hostname)
#endIf
#endPing
def writeToFile(filename, data) :
with open(filename, 'a') as output:
output.write(data + '\n')
#endWith
#endDef
'''
servers.txt example
vm8558
host2
server873
google.com
'''
file = open('servers.txt')
try:
reader = csv.reader(file)
for item in reader:
printPingResult(item[0].strip())
#endFor
finally:
file.close()
#endTry
@arundese
Copy link

arundese commented Nov 9, 2019

In above code i dont want to print the ping usage, it is excute at the end when it dint get ip address in file, can i know it should excute only when ip address is there in file, i tried put if condition but it dint work

@ShUGo95
Copy link

ShUGo95 commented Dec 10, 2019

I'm getting this one
File "ip.py", line 66, in
pingResult(item[0].strip())
File "ip.py", line 31, in pingResult
StatusOfPing = ping_ip(hostname)
File "ip.py", line 5, in ping_ip
p = subprocess.Popen(['ping_ip ' + hostname], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
File "/usr/lib/python3.7/subprocess.py", line 800, in init
restore_signals, start_new_session)
File "/usr/lib/python3.7/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory

@NuKESec
Copy link

NuKESec commented Dec 20, 2019

guys this programmer is a complete moron. 85% of this code is a mix of errors, and improper coding. don't waste your time on this script.

@NiraliPatel83
Copy link

guys this programmer is a complete moron. 85% of this code is a mix of errors, and improper coding. don't waste your time on this script.

show respect for the programmer who shared code.

@Neseef
Copy link

Neseef commented Nov 18, 2021

I used the ping function from your script. it is working as I needed.
Thank you so much for the code.

@EviLMang0
Copy link

Awesome buddy was looking for it perfectly fine

@smohies
Copy link

smohies commented Nov 11, 2022

guys this programmer is a complete moron. 85% of this code is a mix of errors, and improper coding. don't waste your time on this script.

Could you try and be a decent human being? Show some respect to others effort/work, and if you have opinions can you not be a total jerk?

@nasim-ashraf
Copy link

I used your script and its working.
Thanks for sharing!.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment