-1

Hey i have made a little tool for ping any website / ip in python.

So my bot works find but i wan't to add a feature for get the average.

My code below :

import os, time, requests

url = 'WEBHOOK URL'


while True:
    hostname = "google.com" 
    response = os.system("ping -c 1 " + hostname)
    if response == 0:
      print (hostname, 'is online!')
      data = {
    "content" : "Message content",
    "username" : "Author name"
}
      data["embeds"] = [
    {
        "description" : "Google is  onligne",
        "title" : "TITLE EMBEDS"
    }
]
      requests.post(url, json = data)
      time.sleep(900)
    else:
      data = {
    "content" : "Message content",
    "username" : "Author name"
}
      data["embeds"] = [
    {
        "description" : "Google is offligne",
        "title" : "TITTLE EMBEDS"
    }
]
      requests.post(url, json = data)
      print (hostname, 'is down!')
      time.sleep(900)

This code works very nice, but i wanna to complete my code with getting the average speed like :

with someting like this :

Average speed is result of ping.

When i do this :

print (response)

i get :

0

I wan't to get something like :

Average = 49ms

But i don't know how to do this...

Thanks for you're help

PROBLEM FIXED WITH PYTHONPING

Akyna
  • 67
  • 1
  • 8
  • Does this answer your question? [Measuring ping latency of a server - Python](https://stackoverflow.com/questions/2525312/measuring-ping-latency-of-a-server-python) – wovano Oct 11 '21 at 12:47
  • 1
    There is already a full question on this subject where you could find all the information you need [here](https://stackoverflow.com/questions/2525312/measuring-ping-latency-of-a-server-python) – polypode Oct 11 '21 at 11:47
  • No i need to extract the Average speed from the ping command – Akyna Oct 11 '21 at 13:20

1 Answers1

1

If you want to capture o/p of ping command, os.system is not the way, use subprocess.Popen instead.

from subprocess import (
    Popen,
    PIPE,
)

def run_cmd(cmd):
    try:
        proc = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE,)
        (out, err,) = proc.communicate()
        exitcode = proc.returncode
        return (
            out.decode("utf-8"),
            err.decode("utf-8"),
            exitcode,
        )
    except Exception as e:
        raise e

out, err_msg, exit_code = run_cmd("ping -c1 {hostname}")

Now from out you can apply regex to extract the information you need. I am not sure on how you get the speed, so did not have the code for it.

Bhagyesh Dudhediya
  • 1,800
  • 1
  • 13
  • 16
  • I just to extract the average speed on this command into cmd : `ping -c 4 www.stackoverflow.com | tail -1| awk '{print $4}' | cut -d '/' -f 2` But when i try to add into a variable it's return me 0 – Akyna Oct 11 '21 at 13:25
  • Just replace last line of the above code as: `out, err_msg,exit_code = run_cmd("ping -c 4 www.stackoverflow.com | tail -1| awk '{print $4}' | cut -d '/' -f 2") ` and you are done.. i.e call `run_cmd` with the command you want to execute as argument to it. – Bhagyesh Dudhediya Oct 11 '21 at 17:21