Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple ping script in Python

Tags:

I'm unable to find any good easy to learn documentation on python and networking. In this instance, I'm just trying to make a easy script which I can ping a number of remote machines.

for ping in range(1,10):    ip="127.0.0."+str(ping)    os.system("ping -c 3 %s" % ip) 

A simple script like that will ping the machines fine, but I'd like to get the script to returns 'active' 'no response' Which makes me think I'll have to look up the time module as well, I think time.sleep(5) and after that, there would be a break statement. Which makes me think there should be a while loop inside for. I'm not 100% sure, I could be going in the wrong direction completely :/ if anyone could help or point me in the direction of some documentation that'd be great.

like image 1000
kuiper Avatar asked Aug 23 '12 23:08

kuiper


People also ask

How check IP is ping or not in Python?

If you don't need to support Windows, here's a really concise way to do it: import os hostname = "google.com" #example response = os. system("ping -c 1 " + hostname) #and then check the response... if response == 0: print hostname, 'is up! ' else: print hostname, 'is down!


2 Answers

Try subprocess.call. It saves the return value of the program that was used.

According to my ping manual, it returns 0 on success, 2 when pings were sent but no reply was received and any other value indicates an error.

# typo error in import import subprocess  for ping in range(1,10):     address = "127.0.0." + str(ping)     res = subprocess.call(['ping', '-c', '3', address])     if res == 0:         print "ping to", address, "OK"     elif res == 2:         print "no response from", address     else:         print "ping to", address, "failed!" 
like image 179
Roland Smith Avatar answered Nov 27 '22 02:11

Roland Smith


This script:

import subprocess import os with open(os.devnull, "wb") as limbo:         for n in xrange(1, 10):                 ip="192.168.0.{0}".format(n)                 result=subprocess.Popen(["ping", "-c", "1", "-n", "-W", "2", ip],                         stdout=limbo, stderr=limbo).wait()                 if result:                         print ip, "inactive"                 else:                         print ip, "active" 

will produce something like this output:

192.168.0.1 active 192.168.0.2 active 192.168.0.3 inactive 192.168.0.4 inactive 192.168.0.5 inactive 192.168.0.6 inactive 192.168.0.7 active 192.168.0.8 inactive 192.168.0.9 inactive 

You can capture the output if you replace limbo with subprocess.PIPE and use communicate() on the Popen object:

p=Popen( ... ) output=p.communicate() result=p.wait() 

This way you get the return value of the command and can capture the text. Following the manual this is the preferred way to operate a subprocess if you need flexibility:

The underlying process creation and management in this module is handled by the Popen class. It offers a lot of flexibility so that developers are able to handle the less common cases not covered by the convenience functions.

like image 34
hochl Avatar answered Nov 27 '22 01:11

hochl