1
\$\begingroup\$

Please review my code for scanning active IP address . Please Suggest improvements ...

import socket
import threading
import sys

ip0=input('STARTING IP  : ')
ip1=input('ENDING IP    : ')
port=int(input('PORT NUMBER  : '))
timeout=int(input('TIMEOUT      :'))
timeout=timeout/1000
print()
ip0=list(map(int,ip0.split(".")))
ip01=list(map(int,ip1.split(".")))
class IpAddr:
    def __init__(self,d,c,b,a):
        self.a=a
        self.b=b
        self.c=c
        self.d=d
    def increase(self):
        self.a+=1
        if self.a>255:
            self.a=0
            self.b+=1
        if self.b>255:
            self.b=0
            self.c+=1
        if self.c>255:
            self.c=0
            self.d+=1
        return str(self.d)+"."+str(self.c)+"."+str(self.b)+"."+str(self.a)
    def data(self):
        return str(self.d)+"."+str(self.c)+"."+str(self.b)+"."+str(self.a)
    def next(self):
        self.b+=1
        if self.b>255:
            self.b=0
            self.c+=1
        if self.c>255:
            self.c=0
            self.d+=1
        return str(self.d)+"."+str(self.c)+"."+str(self.b)+"."+str(self.a)
    def diff(self,x,y,z,w):
        return (x-self.d)*256**2+(y-self.c)*256+z-self.b



def find(z,i):
    i=list(map(int,i.split(".")))
    ip=IpAddr(i[0],i[1],i[2],i[3])
    global cnt
    while i!=z:
        i=ip.data()
        s=socket.socket()
        s.settimeout(timeout)
        try:
            s.connect((i,port))
            print('[*] ACTIVE HOST',i)
            cnt+=1
        except:
            pass
        s.close()
        i=ip.increase()
ip=IpAddr(ip0[0],ip0[1],ip0[2],ip0[3])
n=ip.diff(ip01[0],ip01[1],ip01[2],ip01[3])-1
cnt=0
d={}
for i in range(n):
    p,q=ip.data(),ip.next()
    d[i]=threading.Thread(target=lambda:find(q,p))
    d[i].daemon=True
    d[i].start()
find(ip1,ip.data())
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

Starting with Python 3.3, there is a standard ipaddress module. Avoid .

Your IpAddr class is cumbersome because you store the four dotted-quad components separately. It would be easier to represent it internally as a 32-bit integer, and convert it from/to dotted-quad notation in the constructor and the __str__ method. For those conversions, you may find struct.pack() and struct.unpack() useful.

I would expect IpAddr objects to be immutable; the .next() method should return a new object instead of mutating the original one.

When scanning a range of IP addresses, watch out for network and broadcast addresses. Opening a connection on a broadcast address would be inappropriate.

The find function is poorly named, and its parameters z and i are cryptic. In fact, your variable names are generally horrible — what do p, q, n, and d mean?

Using a global cnt variable is inappropriate — especially since you access it in threads without any mutex. But why even bother, if it's just a write-only variable whose value never matters?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.