1

I'm trying to create small script that provide whois and external ip and ping a site to find its statue. my code is running fine except for the ping part. It's pinging but not for the limit of 3 I asked for. I'm trying to run it in ubuntu server any suggestions?

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)
2
  • 2
    have you tried to run the ping command on the ubuntu command line? Commented Oct 16, 2016 at 2:19
  • Read about minimal code sample used for StackOverflow Qs at stackoverflow.com/help/mcve . That is to say, we don't need all the other code, just the ping stuff. AND given the answer below, learn to use the documentation for Linux/unix, i.e. man ping. Good luck. Commented Oct 16, 2016 at 3:02

1 Answer 1

1

Try adding the count -c option to your command:

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

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

You can do away with the loop also...

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome, and please mark this answer as "correct".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.