Created
September 18, 2016 03:56
-
-
Save willcharlton/942abc4653a327d9b1fd68c88836c9e7 to your computer and use it in GitHub Desktop.
Multiprocessing Port Sweep
This file contains 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
#!/usr/bin/python2 | |
import multiprocessing | |
import subprocess | |
import os | |
def pinger( job_q, results_q ): | |
DEVNULL = open(os.devnull,'w') | |
while True: | |
ip = job_q.get() | |
if ip is None: break | |
try: | |
subprocess.check_call(['ping','-c1',ip], | |
stdout=DEVNULL) | |
results_q.put(ip) | |
except: | |
pass | |
if __name__ == '__main__': | |
pool_size = 255 | |
jobs = multiprocessing.Queue() | |
results = multiprocessing.Queue() | |
pool = [ multiprocessing.Process(target=pinger, args=(jobs,results)) | |
for i in range(pool_size) ] | |
for p in pool: | |
p.start() | |
for i in range(1,255): | |
jobs.put('192.168.1.{0}'.format(i)) | |
for p in pool: | |
jobs.put(None) | |
for p in pool: | |
p.join() | |
while not results.empty(): | |
ip = results.get() | |
print(ip) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment