Making a Port-Scanner in Kali Linux Terminal Using Python
Last Updated :
17 Dec, 2021
In computer networking, a port is a virtual point where network connections start and end. It’s like an open door of your home, If you don’t close this then anyone can Enter your home. A port scanner is a program that is searching ports in a network and tries to find which ports are virtually open and close. It is common technique hackers or cyber-security experts used to discover open doors or weak points in a network.
How Does a Port Scanner Program Work?
This program sends a network request throw network and tries to connect to a specific TCP or UDP port on a computer and records the response.
The three types of responses are below:
- Open, Accepted: When ports are open, and you can access this system by them.
- Closed, Not Listening: When this port is in use or unavailable at this time
- Filtered, Dropped, Blocked: The computer doesn’t even bother to respond.
Design a Port Scanner on Kali Linux Terminal Using Python
Step 1: Design a Program
At the very first we need to open Our Kali Linux terminal and the very first thing that we should do is find out where our python interpreter is so we command
Syntax:
which python

where is our python interpreter
That’s going to be important when we do this very next step.
Next step we need to open the shell because in there we are going to type our code, so we can type nano command and hit enter.
Syntax:
nano (name).extension
Example:
nano port-scanner.py

give our program a name
Now In here at first we need to assign our Python interpreter Location
#!//usr/bin/python

location of Python interpreter
Now our main program start, we want to import sockets, sockets will tell us the ports how to operate and essentially how to transmit data.
import socket
Then we need to input IP addresses and after that, we also need a program to ask for which port we are looking for
ip = raw_input("enter the IP Address: ")
port = input("Enter the Port Number: ")
Now we want to Define the socket what type of transmitted data are we are looking for to be more specific what Protocol are we asking for to see.
- socket.AF_INET this socket allows us to see TCP connection
- SOCK_STREAM this allows us to look at UDP protocol that’s essentially streaming video and audio
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
Now we are going to print how our program shows us if the specific port open or either IP is down or The port is closed.
if sock.connect_ex((ip,port)):
print "Port ",port, "is closed"
else:
print"Port",port,"is OPEN"

full program
Now save this program press control key + x key to save this program
Syntax:
cnrl+x
Step 2: Compile and Run The program
We all Know How to Run a Script on the Kali Linux terminal
Example
./script name . extension
Syntax
./port-scanner.py

Program Didn’t Run
Look here is the tricky part, We write Our program Perfectly, but it’s Not compiled and Run. So It’s time to give our System Permission.
Chmod 775 is a Linux command which sets permission so that User/Owner can Read, Write and execute. Group can Read Write and execute. Others can Read But can’t Write and execute.
Syntax
chmod 775 ./port-scanner.py

chmod 775 gives you permission
Now we can Run Our program again, and it will compile and Run Successfully
Syntax
./port-scanner.py

Run successfully
Test Our program
So for port scanning, We need a specific IP, and we need to Tell Our Program which port we are Looking For after Input Hit Enter
Example
Enter Your Ip Address : 192.168.43.1
Enter The Port Number : 80

Port scanning Successfully
Similar Reads
Port scanner using 'python-nmap'
In this article, we will learn how to program a port scanner using the 'nmap' module in Python. The program will take a range of port numbers as input and print the state (open or closed) of all the ports in that range. Nmap: Nmap is a free and open-source network scanning tool. To run the program d
2 min read
How Use Linux Command In Python Using System.Os
Using the system module from the os library in Python allows you to interact with the Linux command line directly from your Python script. This module provides a way to execute system commands, enabling you to automate various tasks by integrating Linux commands seamlessly into your Python code. Whe
3 min read
Get Your System Information - Using Python Script
Getting system information for your system can easily be done by the operating system in use, Ubuntu let's say. But won't it be fun to get this System information using Python script? In this article, we will look into various ways to derive your system information using Python. There are two ways t
2 min read
Python Program to Print Lines Containing Given String in File
In this article, we are going to see how to fetch and display lines containing a given string from a given text file. Assume that you have a text file named geeks.txt saved on the location where you are going to create your python file. Here is the content of the geeks.txt file: Approach: Load the t
2 min read
Amazon product price tracker using Python
As we know Python is a multi-purpose language and widely used for scripting. We can write Python scripts to automate daily life task. Let's say we want to track any Amazon product price and grab the deal when the price changes below our defined price. Let's write a Python script for this. Below is t
2 min read
Finding IP Address using Python
An IP(Internet Protocol) address is an identifier assigned to each computer and other device(e.g., router, mobile, etc.) connected to a TCP/IP network that is used to locate and identify the node in communication with other nodes on the network. IP addresses are usually written and displayed in huma
2 min read
Menu driven Python program to execute Linux commands
Linux is one of the most popular operating systems and is a common choice for developers. However, it is difficult to remember the vast range of commands that Linux supports, and hence a Python program that can run these commands easily is demonstrated below. In this article, we will deal with a Pyt
3 min read
Python Program that Sends And Receives Message from Client
Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while the other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the serv
3 min read
Getting Saved Wifi Passwords using Python
Usually while connecting with the wifi we have to enter some password to access the network, but we are not directly able to see the password we have entered earlier i.e password of saved network. In this article, we will see how we can get all the saved WiFi name and passwords using Python, in orde
3 min read
Python program to check if the given string is IPv4 or IPv6 or Invalid
Given a string. The task is to check if the given string is IPv4 or IPv6 or Invalid. Examples: Input : "192.168.0.1" Output : IPv4 Explanation : It is a valid IPv4 address Input : "2001:0db8:85a3:0000:0000:8a2e:0370:7334" Output : IPv6 Explanation : It is a valid IPv6 address Input : "255.32.555.5"
1 min read