Port Scanning in Python for Open Ports

""" Python v2.x Port Scanning for the given host. Display all the open ports"""


import socket

global host

def help():
print("Usage")
print("Enter the host IP: 10.2.5.6")
print("Enter the host IP: 172.16.5.32")

def port_scan():
global host
print("[+] Port scanning initiated for "+str(host)+"...")
for i in range(1,65535):
soc=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
soc.connect((host,i))
print("Port %s : OPEN"%(str(i)))
except:
pass
soc.close()
print("[+] Scanning Done [+]")

valid=True
host=raw_input("Enter the host IP:")
valid_host=host.split(".")
len_host=len(valid_host)
if (len_host<4) or (len_host>4):
print("Invalid Host")
help()
elif len_host==4:
for i in valid_host:
try:
if (int(i)<0) or (int(i)>255):
print("Invalid Host")
valid=False
help()
break
except:
print("Invalid Host")
valid=False
help()
break

if valid:
                soc=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
                try:
                        soc.connect((host,80))
                        soc.close()
                        port_scan()
                except:
                        print("!!!!!!!!System is not Reacable!!!!!!")
                        flg=raw_input("Do you want to proceed(Y\N)?")
                        if flg:
                                port_scan()
                        else:
                                print("[+]System Exit[+]")

Comments

Popular posts from this blog

JTAG PIN Identification

CWE vs CVE