Page MenuHomeVyOS Platform

scanport3.py

Authored By
Unknown Object (User)
Mar 28 2024, 2:20 AM
Size
1 KB
Referenced Files
None
Subscribers
None

scanport3.py

#!/usr/bin/env python3
import nmap
import sys
def read_ports_from_file(file_name):
ports = {}
try:
with open(file_name, 'r') as file:
for line in file:
line = line.strip()
if line and not line.startswith("#"): # Skip comment lines
parts = line.split('#')
port_number = parts[0].strip()
port_description = parts[1].strip() if len(parts) > 1 else ""
ports[port_number] = port_description
except FileNotFoundError:
print(f"File {file_name} not found.")
return ports
def scan_ports(remote_host, ports):
if not ports:
print("No ports to scan.")
return
open_ports = []
closed_ports = []
nm = nmap.PortScanner()
arguments = f'-T4 -p{",".join(ports)}'
nm.scan(hosts=remote_host, arguments=arguments)
for host in nm.all_hosts():
for proto in nm[host].all_protocols():
lport = nm[host][proto].keys()
for port in lport:
state = nm[host][proto][port]['state']
if state == 'open':
open_ports.append((port, ports.get(str(port), "No description available")))
else:
closed_ports.append((port, ports.get(str(port), "No description available")))
print("\nScanning completed.")
print("Open ports:")
print("Port\tDescription")
for port, description in open_ports:
print(f"{port}\t{description}")
print("\nClosed ports:")
print("Port\tDescription")
for port, description in closed_ports:
print(f"{port}\t{description}")
def main():
remote_host = input("Enter remote host: ")
ports = read_ports_from_file('ports.txt')
scan_ports(remote_host, ports)
if __name__ == "__main__":
main()

File Metadata

Mime Type
text/x-python
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
454514
Default Alt Text
scanport3.py (1 KB)

Event Timeline