import socket
import ipaddress
import asyncio
import concurrent.futures
def scan_port(ip, port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1) # Adjust time-out for needs
result = sock.connect_ex((ip, port))
if result == 0:
print(f"Port {port} is open")
sock.close()
except socket.error as e:
print(f"Socket error: {e}")
except TimeoutError:
print("Timeout error")
async def scan_ports(ip, start_port=1, end_port=100): # Adjust number of ports
loop = asyncio.get_running_loop()
tasks = []
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
for port in range(start_port, end_port + 1):
task = loop.run_in_executor(executor, scan_port, ip, port)
tasks.append(task)
await asyncio.gather(*tasks)
def main():
ip = input("Enter the IP address to scan: ")
try:
ipaddress.ip_address(ip)
except ValueError:
print("Invalid IP address")
return
print(f"Starting port scan on {ip}...")
asyncio.run(scan_ports(ip))
print("Port scan complete!")
if __name__ == "__main__":
main()
This Python script performs basic vulnerability scanning by checking for open ports on a specified IP address.
scan_port(ip, port):
This function takes an IP address and a port number as input and attempts to establish a TCP connection to the specified port on the IP address. If the connection is successful, it prints that the port is open.
scan_ports(ip, start_port=1, end_port=100):
This asynchronous function scans a range of port numbers on the specified IP address. It uses a thread pool executor to run the scan_port
function concurrently for each port in the range.
main():
This function prompts the user to enter an IP address, validates the IP address, and calls the scan_ports
function with the provided IP address.
The scan_ports
function iterates through a range of port numbers and attempts to establish a TCP connection to each port using the scan_port
function. If a connection is successful (connect_ex
returns 0), it prints that the port is open.
The main
function prompts the user to enter an IP address and validates it using the ipaddress
library.
When the script is executed, it prompts the user to enter an IP address in the terminal. After validation, it scans for open ports using the scan_ports
function and prints the status of each port.
The output shows the IP address being scanned and the status of each port within the specified range. If any ports are open, it prints the port numbers. This script helps identify open ports on a specified IP address, providing basic insight into potential vulnerabilities.