Python Forum
Ping command using python 3.6.5
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ping command using python 3.6.5
#1
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
Reply
#2
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'
ndc85430 likes this post
Reply
#3
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.
jao likes this post
Reply
#4
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?
Reply
#5
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.
Reply
#6
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
Any idea how to make something like 1: ping google 2: enter adress (http,IP) .... just idea ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need some guidance on a script to ping a list of ip's cubangt 11 1,690 Aug-10-2023, 02:39 PM
Last Post: snippsat
  non-stop ping script kucingkembar 1 1,321 Aug-23-2022, 06:29 AM
Last Post: menator01
  Win32\ping.exe windows pops up -very annoying... tester_V 9 3,127 Aug-12-2021, 06:54 AM
Last Post: tester_V
  Looking for discord bot to make loop ping for address ip tinkode 0 1,789 Jul-26-2021, 03:51 PM
Last Post: tinkode
  How to run parallel command (same command -ping) korenron 3 2,778 Dec-12-2020, 11:04 AM
Last Post: palladium
  GPIO high if network IP has good ping duckredbeard 3 2,282 Oct-12-2020, 10:41 PM
Last Post: bowlofred
  Create a program that PING a list of IPs skaailet 7 6,127 Mar-26-2020, 10:46 PM
Last Post: snippsat
  ping with output jacklee26 1 4,853 Nov-28-2019, 01:01 PM
Last Post: Axel_Erfurt
  PING PONG GAME akea 0 5,645 May-08-2019, 04:30 PM
Last Post: akea
  Ping Code Problem MTom5 1 2,735 Sep-04-2018, 09:58 PM
Last Post: MTom5

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020