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"
 


4 comments:

  1. Thanks Man
    Really Appreciate

    ReplyDelete
  2. import socket
    from os import system
    from sys import exit
    system("clear")
    try:
    servidor = input("Introduzca servidor: ")
    except KeyboardInterrupt: exit("Saliendo...")
    try:
    tope = str(input("Hasta que puerto buscar: "))
    if str(tope) == "":
    tope = 1000
    print ("Hasta el 1000 pues")
    else: tope = int(tope)
    except ValueError: exit("Eso no es un numero, amigo mio.")
    if tope > 65536:
    tope = 65536
    print ("Hasta 65536")
    elif tope<1:exit("Elija un numero postivo")
    print ("-"*60)
    print ("Escaneado...")
    print ("-"*60)
    try:
    ipservidor = socket.gethostbyname(servidor)
    for puerto in range(1,tope+1):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    resultado = sock.connect_ex((ipservidor, puerto))
    if resultado == 0:
    print ("Puerto %s abierto" % (puerto))
    sock.close()
    except KeyboardInterrupt: exit("Saliendo...")
    except socket.gaierror: exit("No se ha podido resolver ese servidor")
    except socket.error: exit("No he podido conectar con el servidor")

    ReplyDelete