"""Verify the current OLT registry endpoint speaks SSH; no credentials used."""
import asyncio
import socket

from app.core.database import AsyncSessionLocal
from app.models.models import OLTDevice


async def main():
    async with AsyncSessionLocal() as db:
        olt = await db.get(OLTDevice, 1)
        if not olt:
            raise RuntimeError("OLT target missing")
        host, port = olt.host, olt.api_port
        protocol = (olt.management_protocol or "").lower()
    try:
        with socket.create_connection((host, port), timeout=8) as sock:
            sock.settimeout(8)
            banner = sock.recv(255)
    except OSError as exc:
        print(f"endpoint=unreachable|protocol={protocol}|reason={type(exc).__name__}")
        return
    normalized = banner.lstrip()
    if normalized.startswith(b"SSH-"):
        print(f"endpoint=ssh_banner_valid|protocol={protocol}")
    elif normalized:
        print(f"endpoint=non_ssh_banner|protocol={protocol}")
    else:
        print(f"endpoint=no_banner|protocol={protocol}")


asyncio.run(main())
