Simple port scanner
A port scanner is designed to examine a server or host machine for open ports. It helps the attackers to identify the service running on the host machine and exploit the vulnerabilities, if there are any.
Getting ready
We can write a simple port scanner with Python using the socket
module. The socket
module is the default low-level networking interface in Python.
How to do it...
We can create a simple port scanner with the socket
module, following are the steps:
- Create a new file called
port-scanner.py
and open it in your editor. - Import the required modules, as follows:
import socket,sys,os
Import the socket
module along with the sys
and os
modules
- Now we can define the variables for our scanner:
host = 'example.com'
open_ports =[]
start_port = 1
end_port = 10
Here we define the starting and ending ports that we plan to scan
- Get the IP from the domain name:
ip = socket.gethostbyname(host)
Here we use the gethostbyname
method in the socket
module. This will return the IP of the domain...