Last active
July 30, 2023 10:07
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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!.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.