|
| 1 | +import concurrent.futures |
| 2 | +import socket |
| 3 | +import sys |
| 4 | +import ipaddress |
| 5 | + |
| 6 | +if len(sys.argv) < 2: |
| 7 | + print("Not enough commandline arguments.") |
| 8 | + sys.exit() |
| 9 | + |
| 10 | +try: |
| 11 | + ipaddress.ip_address(sys.argv[1]) |
| 12 | +except ValueError: |
| 13 | + try: |
| 14 | + ipaddress.ip_address(socket.gethostbyname(sys.argv[1])) |
| 15 | + except (ValueError, socket.gaierror): |
| 16 | + print("Invalid IP address or domain name.") |
| 17 | + sys.exit() |
| 18 | + else: |
| 19 | + target_IP = socket.gethostbyname(sys.argv[1]) |
| 20 | +else: |
| 21 | + target_IP = sys.argv[1] |
| 22 | + |
| 23 | +custom_ports = [] |
| 24 | +if len(sys.argv) > 2: |
| 25 | + custom_ports = sys.argv[2:] |
| 26 | + |
| 27 | +def scan_port(target_IP, port): |
| 28 | + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 29 | + socket.setdefaulttimeout(1) |
| 30 | + |
| 31 | + result = s.connect_ex((target_IP, port)) |
| 32 | + |
| 33 | + if result == 0: |
| 34 | + print(f"Port {port}: \033[92mOPEN\033[0m") |
| 35 | + return port |
| 36 | + |
| 37 | + print(f"Port {port}: \033[91mCLOSED\033[0m") |
| 38 | + return None |
| 39 | + |
| 40 | +open_ports = [] |
| 41 | + |
| 42 | +with concurrent.futures.ThreadPoolExecutor() as executor: |
| 43 | + |
| 44 | + if len(custom_ports) == 0: |
| 45 | + future_to_port = { |
| 46 | + executor.submit(scan_port, target_IP, port): port for port in range(65535) |
| 47 | + } |
| 48 | + else: |
| 49 | + future_to_port = { |
| 50 | + executor.submit(scan_port, target_IP, int(port)): port for port in custom_ports |
| 51 | + } |
| 52 | + |
| 53 | + for future in concurrent.futures.as_completed(future_to_port): |
| 54 | + port = future_to_port[future] |
| 55 | + try: |
| 56 | + open_port = future.result() |
| 57 | + except Exception as e: |
| 58 | + print(f"Exception:{e}") |
| 59 | + else: |
| 60 | + if open_port is not None: |
| 61 | + open_ports.append(open_port) |
| 62 | + |
| 63 | +print(f"Open Ports:{open_ports}") |
0 commit comments