import socket
import sys
import datetime

target = socket.gethostbyname(sys.argv[1])

today = datetime.datetime.now()
title = f"{target}_{today.year}_{today.month}_{today.day}_{today.hour}-{today.minute}.md"

print(f"Scanning target: {target}")
print(f"Output file: {title}\n")

with open(title, "a") as f:
    for port in range(1, 65535):  
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.settimeout(1)

            result = s.connect_ex((target, port))

            if result == 0:
                print(f"[+] Port {port} is open")

                banner = ""
                try:
                    # Only send HTTP request to common web ports
                    if port in [80, 8080, 8000]:
                        s.send(b"GET / HTTP/1.1\r\nHost: test\r\n\r\n")

                    banner = s.recv(1024).decode(errors="ignore").strip()
                except:
                    banner = "No banner"

                line = f"[+] {target}:{port} → {banner}\n"
                print(line)
                f.write(line)

            s.close()

        except KeyboardInterrupt:
            print("\nScan interrupted.")
            sys.exit()

        except Exception:
            pass