Python Port Scanner


Simple python port scanner project to understand how comprehensive tools like Nmap function

Packages needed:

Socket

IPy

This simple port scanner gets an input from the user and proceeds to scan for open ports and grab banners. (Inputs can either be IP addresses or domain names)

Notes

-Port scanner can only scan a single port at a time

-For a wider range of port scanning please edit line 8

-For longer and detailed scanning , increase sock.timeout(_) on line 23

import socket
from IPy import IP

def scan(ipaddress):
    converted_ip = check_ip(ipaddress)
    print('\n' + '[Scannipycharget]' + str(ipaddress))
    for port in range(1,100):
        scan_port(converted_ip, port)

def check_ip(ip):
    try:
        IP(ip)
        return(ip)
    except ValueError:
        return socket.gethostbyname(ip) #returns the IP address of the host.
def get_banner(s):
    return s.recv(1024)

def scan_port(ipaddress, port):
    try:
        sock = socket.socket()
        sock.settimeout(0.5) #for fast scanning
        sock.connect((ipaddress, port))
        try:
            banner = get_banner(sock)
            print('Port ' + str(port) + ' is open ' + ' : ' + str(banner.decode().strip('\n')))
        except:
            print('Port ' + str(port) + ' is open ')
    except:
        pass

ipaddress = input("Enter target ")
scan(ipaddress)

Create a website or blog at WordPress.com