Python Forum
Ping command using python 3.6.5 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Ping command using python 3.6.5 (/thread-14529.html)



Ping command using python 3.6.5 - Martin2998 - Dec-04-2018

Hey all,

I want to ping a server and perform an action based on the result. My problem is getting the ping ressult in an format that i can use for in a if, else statement. ping is always 0 weither the ping is successful or not

Using Windows 10 and python 3.6.5

------------------------------------------------------------------------
import os

ping = os.system('ping 8.8.8.8')
if ping == 0:
print ('up')
else:
print('down')


Any help would be appreciated


RE: Ping command using python 3.6.5 - nilamo - Dec-04-2018

os.system() returns the result of the system call, which is normally 0. It DOES NOT return whatever that program wrote to stdout/stderr. If you want to do something based on the output of a program, you should probably use subprocess.run() instead. https://docs.python.org/3/library/subprocess.html
>>> import subprocess
>>> prog = subprocess.run(["ping", "8.8.8.8"], stdout=subprocess.PIPE)
>>> prog.stdout
b'\r\nPinging 8.8.8.8 with 32 bytes of data:\r\nReply from 8.8.8.8: bytes=32 time=82ms TTL=121\r\nReply from 8.8.8.8: bytes=32 time=15ms TTL=121\r\nReply from 8.8.8.8: bytes=32 time=18ms TTL=121\r\nReply from 8.8.8.8: bytes=32 time=14ms TTL=121\r\n\r\nPing statistics for 8.8.8.8:\r\n    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),\r\nApproximate round trip times in milli-seconds:\r\n    Minimum = 14ms, Maximum = 82ms, Average = 32ms\r\n'



RE: Ping command using python 3.6.5 - snippsat - Dec-05-2018

os.system() do not use,it's been replaced bye subprocess which is better and safer.
subprocess.run() now also has a parameter capture_output=True,maybe clearer than stdout=subprocess.PIPE.
Get bytes back so can decode if needed.
import subprocess
 
out = subprocess.run(['ping', 'google.com'], capture_output=True)
print(out.stdout.decode())
Output:
Pinging google.com [216.58.207.238] with 32 bytes of data: Reply from 216.58.207.238: bytes=32 time=36ms TTL=55 Reply from 216.58.207.238: bytes=32 time=34ms TTL=55 Reply from 216.58.207.238: bytes=32 time=33ms TTL=55 Reply from 216.58.207.238: bytes=32 time=32ms TTL=55 Ping statistics for 216.58.207.238: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 32ms, Maximum = 36ms, Average = 33ms
Can also use check_output() to capture output.


RE: Ping command using python 3.6.5 - Martin2998 - Dec-05-2018

Thanks that works good. i now get the output from windows command promt in python.

What could be the best way to distinguish between the two different result (ping successful,ping not successful) when i get the results in text. i was thinking i could identify a word or number that is only in the successful or not successful ping result and make a if statement based on that. my problem is how i can search the text result and identify that specific word with python.

Any thoughts?


RE: Ping command using python 3.6.5 - Gribouillis - Dec-05-2018

You can examine the return code of the ping command with the subprocess module. Also note that you have the -c 4 option to send only 4 packets for example.


RE: Ping command using python 3.6.5 - wavic - Dec-05-2018

from scapy import *

ping = IP(dst='192.168.1.1')/ISMP() # creating the packet
resp = sr1(ping) # sending one packet
Now you can look at the response and use the data you need.


RE: Ping command using python 3.6.5 - blazejwiecha - Apr-19-2021

Any idea how to make something like 1: ping google 2: enter adress (http,IP) .... just idea ?