0

I have this code :

import os, time, requests

url = 'WEBHOOK URL'

while True:

    hostname = "google.com" #example
    response = os.system("ping -c 1 " + hostname)    
    if response == 0:
      print (hostname, 'est en ligne!')
      ping = os.system("ping -c 4 " + hostname + "| tail -1| awk -F '/' '{print $5}' ")
      data = {"content" : "Service online with ping of "+ str(ping) +"ms",
        "username" : "Ping Bot" }
      requests.post(url, json = data)

The bot should return a values likes :

Service online with ping of 29.815 ms

But i got this :

Service online with ping of 0 ms

I don't know why i got 0 instead of my variable values ...

Thanks for you're assistance

Taku
  • 31,927
  • 11
  • 74
  • 85
Akyna
  • 67
  • 1
  • 8
  • 1
    `os.system()` returns the exit code, not the output. See this question: [Assign output of os.system to a variable and prevent it from being displayed on the screen](https://stackoverflow.com/questions/3503879/assign-output-of-os-system-to-a-variable-and-prevent-it-from-being-displayed-on) – Sven Eberth Oct 11 '21 at 13:00
  • So if i have good understand i should replace os.system with : `os.popen` – Akyna Oct 11 '21 at 13:04

0 Answers0