Skip to content

Instantly share code, notes, and snippets.

Created October 27, 2014 04:11
Show Gist options
  • Save anonymous/11a537184bb932e5c79b to your computer and use it in GitHub Desktop.
Save anonymous/11a537184bb932e5c79b to your computer and use it in GitHub Desktop.
python ping.py --ips 10.0.0.* -t 100
from optparse import OptionParser
from threading import Thread
import subprocess
from Queue import Queue
def pinger(q):
while True:
_ip = q.get()
ret = subprocess.call("timeout 1 ping -c 1 %s >/dev/null && echo $_;" % _ip,
shell=True,
stdout=open('/dev/null', 'w'),
stderr=subprocess.STDOUT)
if ret == 0:
print "%s: is alive" % _ip
q.task_done()
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-i", "--ips")
parser.add_option("-t", "--thread")
(options, args) = parser.parse_args()
num_threads = int(options.thread)
queue = Queue()
for i in range(num_threads):
worker = Thread(target=pinger, args=(queue,))
worker.setDaemon(True)
worker.start()
range_ips = options.ips.replace("*", "%s")
for ip in range(1, 255):
queue.put(range_ips % ip)
queue.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment