How To Make Port Scanner Using Python

How To Make Port Scanner Using Python

Welcome, to the python project I'm Vicky Pawar and today, I'm going to show you how to make PORT SCANNER USING PYTHON

This Port Scanner will work for both the Web Applications as well as remote Host. This tool has been created to provide the basic functionality of a Port Scanner. The general concept of Sockets had been used to provide the functionality. Port Scanner is built on Python 3 and uses some extra libraries such as socket and pyfiglet (for a fancy banner).

port-scan-hero.png

When we run the above script, it will prompt for the hostname, you can provide any hostname like name of any website but be careful because port scanning can be seen as, or construed as, a crime. We should never execute a port scanner against any website or IP address without explicit, written permission from the owner of the server or computer that you are targeting. Port scanning is akin to going to someone’s house and checking their doors and windows. That is why it is advisable to use port scanner on localhost or your own website (if any)

SOURCE CODE


import socket 
import sys
import time
import pyfiglet
import threading

result = pyfiglet.figlet_format("NETWORK SCANNER")
print(result)

print("-"*70)
print("GIGA-BYTE")
print("-"*70)

usage = "python3 portscanner.py TARGET START_PORT END_PORT"

if(len(sys.argv) != 4):
    print(usage)
    sys.exit()
try:
    target = socket.gethostbyname(sys.argv[1])
except sockket.gaierror:
    print("Name resolution error")
    sys.exit()

s_port = int(sys.argv[2])
e_port = int(sys.argv[3])

print("Scanning  target",target)

def scan_port(port):

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(2)
    conn = s.connect_ex((target, port))
    if(not conn):
        print("Port {} is OPEN".format(port))

    s.close()    

for port in range(s_port, e_port+1):
    thread = threading.Thread(target = scan_port, args = (port,))
    thread.start()

OUTPUT

Capture.PNG

This port scanner will generally take the time of 2 mins maximum to produce output in the format, that so and so ports are open or closed.