Altair Ayoub
Altair Ayoub

Reputation: 27

Python Port scanning am i doing it right?

So i am making a python program with what i learnd so far where the user enters two ips that represents the start and and of the range of ips to be scanned than saves the wanted ip in a text file. here is what i came up with:

   #ip range and scanning
import socket
import sys
ok=[]
def ipscan(start2,port):
    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(2)
    try:
        s.connect((start2,port))
        print start2 ,'-->port %s is Open'%port
        ok.append(start2)
    except: print start2 ,'-->port %s is Closed ! '%port
def iprange(start,end):
    while end>start:
       start[3]+=1
       ipscan('.'.join(map(str,start)),p)
       for i in (3,2,1,0):
          if start[i]==255:
             start[i-1]+=1
             start[i]=0
 #--------------------------------------------#    
sta=map(int,raw_input('From : ').split('.'))
fin=map(int,raw_input('to : ').split('.'))
p=input('Port to scan : ')
iprange(sta,fin)
print '-----------end--------------'
of=open('Output.txt','w')
for ip in ok:
    of.writelines(ip+'\n')
of.close()

it seems to be working but i need to be sure,and wanted to know if i can make it any faster?or if there is a better way.

Upvotes: 1

Views: 387

Answers (1)

corny
corny

Reputation: 7852

You could use nmap ;)

Your socket connections are all sequential. You could parallelize the connections, because that is the slow factor, especially for filtered (not responding) ports.

Upvotes: 1

Related Questions