This post will walkthrough the creation of a python script for scanning open ports on a remote host using Kali Linux.
To begin, open a command terminal in Linux
Create a file for the python script. This can be done using VIM and an appropriate file name ending in .py
Once inside the newly created file, the first lines of the script will be used to recruit pre-existing functions to make this script operational as seen below.
Now, for organizational purposes, comment out “Blank your screen” and follow it with the command below to do just that
The next two lines instruct the script to ask for an input of the IP or host to be scanned and provides a location from which to retrieve the host information.
For the sake of visibility, the following lines in the script will create a banner with a message to indicate the process is running.
Next, a variable for the time and date the scan started will be assigned as “t1” (seen below)
In the next screenshot, a loop argument is used to repeatedly run the process to determine the output for open ports detected within a given range.
Continuing, the next three blocks provide for error handling when the port scan is unsuccessful. Scenarios in which the user stops the scan, the hostname cannot be resolved or there is a failure to connect to the server, these blocks will print the appropriate error message to the terminal.
The last few entries in this script will define a second time variable, calculate the difference between the two time variables and print a completion message with time information when the scan is complete.
Now, the script can be saved and run from the command terminal.
If everything is working correctly, the script will request a host to be scanned
Type the host of choice and hit enter to watch the scan work as seen below
Ports 53 and 80 are open on this host and the scan took about thirty seconds to complete.