ping через linux и получить вывод в python

Я пытаюсь создать небольшой скрипт, который предоставляет whois и внешний ip и пингует сайт, чтобы найти его статую. мой код работает нормально, за исключением части пинга. Это пингует, но не за предел 3, который я просил. Я пытаюсь запустить его на сервере Ubuntu какие-либо предложения?

import os

os.system("clear") # clear the screen

inp = input("Enter your choice from the menu: \n1) To find your external IP address\n2) To check domain name whois information\n3) To check if the website or ip address is up\n")

if (inp == "1"):
 print("Your external ip address is: ") # me trying to be smart XD
 ip = os.system("dig +short myip.opendns.com @resolver1.opendns.com")
 #print("Your external ip address is: %d" % ip)

elif (inp == "2"):

 domain = input("Enter the domain you want to whois it: \n")

 info = os.system("whois %s" % domain)
 print(info)

elif (inp == "3"):
 target = input("Enter the url or ip address you want to check:\n")

 for x in range(3):
     response = os.system("ping %s" % target)

 if (response == 1): # how can I decide based on response result ?
     print("%s is up" % target)
 else:
     print("%s is down" % target)

1 ответ

Решение

Попробуйте добавить количество -c вариант вашей команды:

target = "stackru.com"
response = os.system("ping %s -c 3" % target)

if response is not 0:
    print("failed")
else:
    print("success")

Вы можете покончить с петлей также...

Другие вопросы по тегам