Skip to main content
edited tags
Link
Source Link

IP Scanner via python sockets

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())