I'm trying to build a small tcp scanner for a netmask.
The code is as follow:
import socket,sys,re,struct
from socket import *
host = sys.argv[1]
def RunScanner(host):
s = socket(AF_INET, SOCK_STREAM)
s.connect((host,80))
s.settimeout(0.1)
String = "GET / HTTP/1.0"
s.send(String)
data = s.recv(1024)
if data:
print "host: %s have port 80 open"%(host)
Slash = re.search("/", str(host))
if Slash :
netR,_,Wholemask = host.partition('/')
Wholemask = int(Wholemask)
netR = struct.unpack("!L",inet_aton(netR))[0]
for host in (inet_ntoa(struct.pack("!L", netR+n)) for n in range(0, 1<<32-Wholemask)):
try:
print "Doing host",host
RunScanner(host)
except:
pass
else:
RunScanner(host)
To launch : python script.py 10.50.23.0/24
The problem I'm having is that even with a ridiculous low settimeout value set, it takes ages to cover the 255 ip addresses since most of them are not assigned to a machine.
How can i make a way faster scanner that wont get stuck if the port is close.MultiThreading ?
Thanks !