import subprocess
import os

keys = [
    r"C:\Users\P3\.ssh\id_rsa",
    r"C:\Users\P3\.ssh\genesis_elestio",
    r"E:\genesis-system\.ssh\sunaiva_deploy",
]

print("=== SSH Key Validation ===")
for key in keys:
    if not os.path.exists(key):
        print(f"MISSING: {key}")
        continue
    r = subprocess.run(
        ["ssh-keygen", "-l", "-f", key],
        capture_output=True, text=True
    )
    print(f"\nKey: {key}")
    print(f"  stdout: {r.stdout.strip()}")
    print(f"  stderr: {r.stderr.strip()}")
    print(f"  rc: {r.returncode}")

# Check known_hosts for 91.99.89.7
print("\n=== Known hosts for 91.99.89.7 ===")
known_hosts = r"C:\Users\P3\.ssh\known_hosts"
if os.path.exists(known_hosts):
    with open(known_hosts) as f:
        for line in f:
            if "91.99.89.7" in line or "sunaiva-prod" in line:
                print(f"  {line.strip()[:120]}")
else:
    print("  known_hosts not found")

# Also try netcat-style connection test
print("\n=== SSH Banner Test ===")
import socket
try:
    s = socket.socket()
    s.settimeout(5)
    s.connect(("91.99.89.7", 22))
    banner = s.recv(256).decode("utf-8", errors="replace").strip()
    s.close()
    print(f"  SSH Banner: {banner}")
except Exception as e:
    print(f"  Error: {e}")
