-
-
Save congto/9624dea80407de9cec77 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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