Friday, January 13, 2012

How to create a simple port scanner in python

A port scanner is an application used to probe system in network for open ports.
We shall see how to develop a simple port scanner in python

Following is a python program that tries to connect every port in given range and prints ports opened.


#Simple port scanner 
#Developed by Malhar Vora
#Status: Completed
#Date: 13-1-2012
#WebSite : www.malhar2010.blogspot.com
#Email   : vbmade2000@gmail.com
#-----------------------------------------------------------------------------------------------
from socket import * 

remotehost = raw_input("Enter host to scan: ")
fromport = int(raw_input("Enter from port : "))
toport = int(raw_input("Enter to port port: "))

print "Scanning started"
for i in range(fromport,toport):

 #Creating instance of socket class with AF_INET socket family and SOCK_STREAM for connection-oriented communication
 s = socket(AF_INET,SOCK_STREAM)
 # If port is opened result will be 0
 if s.connect_ex((remotehost,i)) == 0:
  print i, " is open"
 s.close()

print "Scanning finished"