HackingScripts/pingscan.py

65 lines
1.8 KiB
Python
Raw Normal View History

2020-08-28 23:04:41 +02:00
#!/usr/bin/python
import sys
import os
import ipaddress
import subprocess
from importlib import util
threading_spec = util.find_spec("threading")
queue_spec = util.find_spec("queue")
if threading_spec is not None:
import threading
import queue
NUM_THREADS = 10
THREADING_ENABLED = True
QUEUE = queue.Queue()
else:
THREADING_ENABLED = False
def checkHost(host):
proc = subprocess.Popen(["ping", str(host), "-c", "1"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return proc.wait() == 0
def doWork(q):
while not q.empty():
host = q.get()
if checkHost(host):
print("[+] Host %s is online" % host)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: %s <hosts>" % sys.argv[0])
exit(1)
hosts = sys.argv[1]
if "/" not in hosts:
if checkHost(hosts):
print("[+] Host %s is online" % hosts)
else:
print("[-] Host %s is not reachable" % hosts)
else:
cidr = ipaddress.ip_network(hosts)
hosts = list(cidr.hosts())
if not THREADING_ENABLED:
print("Scanning %d hosts in range: %s..." % (len(hosts), cidr))
for host in hosts:
if checkHost(host):
print("[+] Host %s is online" % host)
print("Done")
else:
print("Scanning %d hosts in range: %s with %d threads..." % (len(hosts), cidr, NUM_THREADS))
for host in hosts:
QUEUE.put(host)
threads = []
for i in range(NUM_THREADS):
t = threading.Thread(target=doWork, args=(QUEUE, ))
t.start()
threads.append(t)
for t in threads:
t.join()
print("Done")