nabs2 nabs2 - 2 years ago 136
Python Question

Scanning a Class C network Python

I'm trying to figure out how can I scan a class C ip range;
For example a user provide by cmd line to my script :
python (OR

Let's figure my script does only a tcp connect action:

import socket, sys

host = sys.argv[1],65535

s = socket(AF_INET, SOCK_STREAM)



Do i really need to do a "for x in ip range" ?!?
Or a build in module can do that ?

Thanks !

Answer Source

It doesn't take too much code it you want to do it in pure python

import socket, struct

def atod(a): # ascii_to_decimal
    return struct.unpack("!L",socket.inet_aton(a))[0]

def dtoa(d): # decimal_to_ascii
    return socket.inet_ntoa(struct.pack("!L", d))

net,_,mask = sys.argv[1].partition('/')
mask = int(mask)
net = atod(net)

for host in (dtoa(net+n) for n in range(0, 1<<32-mask)):
    print host
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download