1

I have a program running a ping. On my terminal screen i get this:

--- google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 47.963/47.963/47.963/0.000 ms
Connection successful

But on my console i am only getting:

Connection successful

I want my console to show the same ping statistics as my terminal does. I will eventually want to log the ping results onto a txt or csv file, but that will be down the road.

import platform    # For getting the operating system name
import subprocess  # For executing a shell command
import time
import logging

host = "google.com"

def ping(host):
    param = '-n' if platform.system().lower()=='windows' else '-c'
    command = ['ping', param, '1', host]
    return subprocess.call(command) == 0

while ping(host) == False:
    print("There is no network connection")
    time.sleep(1)
while ping(host) == True:
    print("Connection successful")
    time.sleep(1)

How do i get my Terminal Ping statistics to display on my console output?

adrtam
  • 6,991
  • 2
  • 12
  • 27
Lzypenguin
  • 945
  • 1
  • 7
  • 18
  • Possible duplicate of [Python: How to get stdout after running os.system?](https://stackoverflow.com/questions/18739239/python-how-to-get-stdout-after-running-os-system) – JR87 May 23 '19 at 20:52

2 Answers2

3

To log the full output of the command, use Popen.

import platform    # For getting the operating system name
import subprocess  # For executing a shell command
import time

host = "google.com"

def ping(host):
    param = '-n' if platform.system().lower()=='windows' else '-c'
    command = ['ping', param, '1', host]
    return subprocess.Popen(command, stdout=subprocess.PIPE).stdout.read()

while True:
    output = ping(host)
    print(output)
    time.sleep(1)

I tested on Ubuntu with Python 3.6.7

Michael H.
  • 3,323
  • 2
  • 23
  • 31
  • This is great, and now i have it running. What does the stdout part mean? Is there a way to parse the data and only print certain parts? I want to eventually print out the date/time - successful or not successful, and the AVG ping speed since i am only sending one ping at a time. – Lzypenguin May 23 '19 at 21:24
  • stdout captures what the process prints. If you want to print the time as well, you should have a look at the `datetime` package and its `strftime` method. – Michael H. May 24 '19 at 08:52
0

I re-edited for Linux to ping 3 times and output to a file. This will effectively tell you if the host is up or down. It should still print to terminal, you can use os.system('pause') though I don't remember if that works in Linux.

import os

def main():
f=['8.8.8.8','yahoo.com']
    for ips in f:
        response = os.system("ping -c3" + ips)
        if response == 0:
            writeToOut(ips,"host is up")

        else:
            writeToOut(ips, "host is down")


def writeToOut(ip,result):
    f=open('C:/Users/user/Desktop/hostsResults.txt','a')
    ip = ip.rstrip()
    f.write(ip + "," + result + '\n')
    f.close()


main()
JR87
  • 95
  • 8
  • Ahh, I forget Linux ping command requires an output. I think it's something -n 3 or something like that. I can't remember exactly but that's why it keeps going on. – JR87 May 23 '19 at 21:39
  • I re-edited for Linux/Unix/Mac. I believe they're all ping -c3 (3 being how many times to ping). This will output to your terminal, and write the results to a text file. It's effectively an "Is Host Up" thing. – JR87 May 23 '19 at 21:50
  • When i run your new prompt i get this: ping: invalid count of packets to transmit: `38.8.8.8' ping: invalid count of packets to transmit: `3yahoo.com' – Lzypenguin May 23 '19 at 22:00
  • I was able to fix it by removing the 3 from -c3 and adding a space. Now its working. So i want to write to the text file that the host is up, and the average ping response. How would i parse out just the average ping response? – Lzypenguin May 23 '19 at 22:08