Python Script: write a Port Scanner (Source code)


A port scanner is a software application or online tool used to test network communication ports on internet protocol (IP) enabled devices, such as computers, routers, firewalls, and switches. Port scanners are used by system administrators and penetration testers to detect vulnerabilities of systems and to leverage those vulnerabilities to gain access.

Port scanners can be used to determine whether a specific port is open or closed on a target machine. An attacker can use this information to gain access to the target machine by exploiting open ports. In this blog post, we will show you how to write a port scanner in Python.

Creating a Port Scanner in Python

We will be creating a simple port scanner in Python. This port scanner will take an IP address and a list of ports as input and scan the given ports on the given IP address. If the port is open, it will print out the message “Port is open”. We will be using the socket module for this purpose. The socket module provides an easy way to write a network-based application in Python.

To use the socket module, we first need to import it into our script:

import socket
Code language: JavaScript (javascript)

Now, we will define two variables:

ip_address = "127.0.0.1" # replace with your IP address ports = [22, 80, 443] # replace with your ports
Code language: PHP (php)

The ip_address variable contains the IP address that we want to scan and the ports variable contains a list of ports that we want to scan on that IP address.

Next, we will create a function named scan_port() which takes two arguments: ip and port. This function will try to create a socket object with the given IP address and port number.

If the socket creation is successful, it means that the port is open and it will print out “Port is open” message. Otherwise, it means that the port is closed and it will print out “Port is closed” message:

def scan_port(ip, port): try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((ip, port)) print("Port is open") except: print("Port is closed")
Code language: Python (python)

That’s it! Now you know how to write a simple port scanner in python.

A full python script to scan ports (port_scanner.py)

import concurrent.futures import argparse import socket import time common_ports = { "21": "FTP", "22": "SSH", "23": "TELNET", "25": "SMTP", "53": "DNS", "69": "TFTP", "80": "HTTP", "109": "POP2", "110": "POP3", "123": "NTP", "137": "NETBIOS-NS", "138": "NETBIOS-DGM", "139": "NETBIOS-SSN", "143": "IMAP", "156": "SQL-SERVER", "389": "LDAP", "443": "HTTPS", "546": "DHCP-CLIENT", "547": "DHCP-SERVER", "995": "POP3-SSL", "993": "IMAP-SSL", "2086": "WHM/CPANEL", "2087": "WHM/CPANEL", "2082": "CPANEL", "2083": "CPANEL", "3306": "MYSQL", "8443": "PLESK", "10000": "VIRTUALMIN/WEBMIN", } parser = argparse.ArgumentParser() parser.add_argument("host", help="host IP") parser.add_argument("--start", "-s", help="Start Port") parser.add_argument("--end", "-e", help="End Port") args = parser.parse_args() host = args.host if args.start: start_port = int(args.start) else: start_port = 0 if args.end: end_port = int(args.end) else: end_port = 65535 # No end port = we scan all print("Scanning %s for open ports.\n" % host) def scanner(port): skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM) skt.settimeout(0.3) skt.connect((host, port)) try: port_name = " (%s)" % common_ports.get(str(port), "") if port_name == " ()": port_name = "" ext_port_name = "%s%s" % (str(port), port_name) except KeyError: ext_port_name = str(port) print(" - Port {} is open".format(ext_port_name)) t1 = time.perf_counter() ports = [j for j in range(start_port, end_port + 1)] # 2x the deafult threads for 4 cores processor with concurrent.futures.ThreadPoolExecutor(max_workers=40) as executor: executor.map(scanner, ports) t2 = time.perf_counter() print("Finished scannning for %d ports in %s seconds" % (len(ports) , str(t2 - t1)))
Code language: Python (python)

Usage

python3 port_scanner.py <host-ip> --start <start-port> --end <end-port>
Code language: HTML, XML (xml)

In this blog post, we showed you how to write a simple port scanner in Python. We hope you found this blog post helpful! If you have any questions or comments, please feel free to leave them below. Thanks for reading !

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts