-
-
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 |
Hi, i am very new on Python, i try to run this script but i am some errors, you can helpme?:
Traceback (most recent call last):
File "ping.py", line 65, in
printPingResult(item[0].strip())
File "ping.py", line 31, in printPingResult
statusOfPing = ping(hostname)
File "ping.py", line 5, in ping
p = subprocess.Popen('ping ' + hostname, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
File "/usr/lib64/python3.4/subprocess.py", line 856, in init
restore_signals, start_new_session)
File "/usr/lib64/python3.4/subprocess.py", line 1460, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'ping google.com'
Something wrong ?
End up in below error.
File "pdns.py", line 22, in ping
p = subprocess.Popen(['ls -lrt'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
File "/usr/lib64/python2.6/subprocess.py", line 642, in init
errread, errwrite)
File "/usr/lib64/python2.6/subprocess.py", line 1238, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Change "p = subprocess.Popen('ping ' + hostname, stdout=subprocess.PIPE, stderr=subprocess.PIPE)" to "p = subprocess.Popen(['ping',hostname], stdout=subprocess.PIPE, stderr=subprocess.PIPE)"
This is because Python3 is trying to find a file named "ping" and run it.
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
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
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!.
How to run
python ping.py