Port Scaner | Script with python to Scan ports in Network

Amr Hamdy
1 min readMay 4, 2023

--

By Amr Hamdy

Python script that allows you to enter the target IP address and specifies the port range to scan:

import socket

target = input("Enter target IP address: ") # Get IP address from user input
ports = range(1, int(input("Enter port range to scan: "))) # Get port range from user input

def scan_ports(ip):
for port in ports:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((ip, port))
print(f"Port {port} is open")
except:
pass
s.close()

scan_ports(target)

This script:

1- Prompts the user to enter the target IP address and port range to scan

2- Gets the IP address and port range as input and stores in variables

3- The ports range is now set from 1 to the max port number entered by the user

4- The rest of the logic remains the same — it scans the target IP for open ports in the given range

5- Prints any open ports found

So now when you run the script, you can enter the IP and port range to customize the scan

--

--

Amr Hamdy
Amr Hamdy

Written by Amr Hamdy

Ethical Hacker | Cyber Security | Coding | Py 💻. Bug Hunter Cyber Crime Investigator . Info Sec Trainer

No responses yet