-
-
Save ariesmcrae/64c69a020335892b00d4 to your computer and use it in GitHub Desktop.
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 |
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.
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.
I used the ping function from your script. it is working as I needed.
Thank you so much for the code.
Awesome buddy was looking for it perfectly fine
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?
I used your script and its working.
Thanks for sharing!.
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