Miscellaneouss

Port Scanner in Python for Linux

Port Scanner in Python is used to locate the open ports available on a particular hostPorts are like doors on a computer, they allow or deny incoming data. And as such, open ports are especially useful in hacking.

An open port, much like an open door, tends to be exploited by hackers everywhere. As such, port scanners – scanners that determine whether a port is open or closed – is of great use to all.

In fact, it is an essential tool. Port scanners can be written in python with a few lines of code. In this tutorial, we will learn how to code a port scanners in python.

Port Scanner in Python

In order to achieve this, we will be using the socket module. The first thing we will do is to ask the user for input. The user can input a website or an ip address.

import socket

# Ask for user input of a website or ip
user_input = input("Which website (ex: www.google.com) or IP address would you like to scan?")

The second thing we do is to check whether the input is a website or an ip address. For this, we will use the ipaddress module which will throw an exception if the inputted variable is not an ip.

We then set the ip address as the user input. Now, if it’s a website, we use gethostbyname() to convert the website to an ip, and then set the ip address to the one retrieved.

# Check if it's a website or an IP address

import ipaddress

try:
    checking_IP = ipaddress.ip_address(user_input)
    ip_address = user_input
    print("The ip address to scan is %s" % ip_address)
except:
    ip_address = socket.gethostbyname(user_input)
    print("The ip address to scan is %s" % ip_address)

Now that we have an ip address, we need ports. Since we’ll be scanning a large number of ports, we create a for loop. Within the for loop, we create a socket, and define the tuple (ip address, and port). Next, we use sock.connect_ex() to verify whether the port is open or not. Sock_connect_ex() takes a tuple as input argument.

Since we don’t want the code to run forever, we will also set a time limit using sock.settimeout(). Lastly, if the output of sock.connect_ex() is equal to 0, it means that the port is open, else it’s closed.

# Create a socket for each port

for port in range(1, 2000):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tuple = (ip_address, port)
    port_result = sock.connect_ex(tuple)
    sock.settimeout(10)
    if port_result == 0:
        print("Port %s : Open" % port)

The full code is:

import socket

# Ask for user input of a website or ip
user_input = input("Which website (ex: www.google.com) or IP address would you like to scan?")

# Check if it's a website or an IP address

import ipaddress

try:
    checking_IP = ipaddress.ip_address(user_input)
    ip_address = user_input
    print("The ip address to scan is %s" % ip_address)
except:
    ip_address = socket.gethostbyname(user_input)
    print("The ip address to scan is %s" % ip_address)

# Create a socket for each port

for port in range(1, 2000):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tuple = (ip_address, port)
    port_result = sock.connect_ex(tuple)
    sock.settimeout(10)
    if port_result == 0:
        print("Port %s : Open" % port)

Port Scanner in Python are useful tools indeed to verify whether ports are open or closed. Further, it only takes one module, the socket module, to achieve this. In this tutorial, we learned how to create a simple and easy port scanner for use in Linux.

Happy Coding!

FAQs

What is a port scanner?

Port scanners are used to determine whether a port is open or closed.

What module do you use to create a port scanner in python?

It only takes one module, the socket module, to create a port scanner.

How do I make a port scanner in Python?

import socket
# Ask for user input of a website or ip
user_input = input(“Which website (ex: www.google.com) or IP address would you like to scan?”)

# Check if it’s a website or an IP address
import ipaddress

try:
checking_IP = ipaddress.ip_address(user_input)
ip_address = user_input
print(“The ip address to scan is %s” % ip_address)
except:
ip_address = socket.gethostbyname(user_input)
print(“The ip address to scan is %s” % ip_address)

# Create a socket for each port
for port in range(1, 2000):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tuple = (ip_address, port)
port_result = sock.connect_ex(tuple)
sock.settimeout(10)
if port_result == 0:
print(“Port %s : Open” % port)

Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.

About the author

mm

Kalyani Rajalingham

I'm from Sri Lanka (live in Canada), and am a Linux and code lover.

Leave a Comment