diff --git a/BasicPythonScripts/Port Scanner/Images/output.png b/BasicPythonScripts/Port Scanner/Images/output.png new file mode 100644 index 000000000..9435f42fd Binary files /dev/null and b/BasicPythonScripts/Port Scanner/Images/output.png differ diff --git a/BasicPythonScripts/Port Scanner/README.md b/BasicPythonScripts/Port Scanner/README.md new file mode 100644 index 000000000..600dcc9f2 --- /dev/null +++ b/BasicPythonScripts/Port Scanner/README.md @@ -0,0 +1,29 @@ +# Package/Script Name +Port Scanner +## Short description of package/script + +- Use nmap to scan and detect the specified server port +- You can also use this script to customize it to achieve the effect you want. + +## Setup instructions +1. First you need to install nmap, centos as an example. + +``` +yum install nmap +``` + +2. Installing NAMP's Python library + +``` +pip3 install python-nmap +``` + +## Output +images link : +Images/output.png + +![preview](https://github.com/Ethan-622/Awesome_Python_Scripts/blob/main/BasicPythonScripts/Port%20Scanner/Images/output.png) + + +## Author(s) +Ethan diff --git a/BasicPythonScripts/Port Scanner/port_scanner.py b/BasicPythonScripts/Port Scanner/port_scanner.py new file mode 100644 index 000000000..33ab51234 --- /dev/null +++ b/BasicPythonScripts/Port Scanner/port_scanner.py @@ -0,0 +1,46 @@ +import sys +import nmap + +def main(): + input_data = input("Please input hosts and ports (e.g., '192.168.1.1 80,443'): ") + scan_row = input_data.strip().split() + + if len(scan_row) != 2: + print("Error! Please provide both hosts and ports.") + sys.exit(1) + + hosts = scan_row[0] + ports = scan_row[1] + + try: + nm = nmap.PortScanner() + except nmap.PortScannerError as e: + print(e) + print("Nmap not found! Ensure that Nmap is installed.") + sys.exit(1) + except Exception as e: + print(f"Unexpected error: {str(e)}") + sys.exit(1) + + try: + print(f"Scanning {hosts} on ports {ports}...") + nm.scan(hosts=hosts, arguments=f'-v -sS -p {ports}') + except Exception as e: + print(f"Scan error: {str(e)}") + sys.exit(1) + + for host in nm.all_hosts(): + print('-------------------------------------------') + print(f'Host : {host} ({nm[host].hostname()})') + print(f'State : {nm[host].state()}') + + for proto in nm[host].all_protocols(): + print('-------------------') + print(f'Protocol : {proto}') + ports = sorted(nm[host][proto].keys()) + for port in ports: + state = nm[host][proto][port]['state'] + print(f'Port : {port}\tState : {state}') + +if __name__ == "__main__": + main() diff --git a/BasicPythonScripts/Port Scanner/requirements.txt b/BasicPythonScripts/Port Scanner/requirements.txt new file mode 100644 index 000000000..16798c6a1 --- /dev/null +++ b/BasicPythonScripts/Port Scanner/requirements.txt @@ -0,0 +1 @@ +python-nmap=>0.7.1 \ No newline at end of file