#!/usr/bin/env python3 from __future__ import annotations import sys import time from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) import vps_ssh import paramiko HERE = Path(__file__).resolve().parent RSC_LOCAL = HERE / "_tmp_test-enjoy.rsc" LAB_CANDIDATES = [ ("192.168.88.1", "admin", "1234"), ("192.168.88.1", "admin", ""), ("192.168.88.1", "admin", "admin"), ("192.168.88.249", "admin", "1234"), ] def wait_prompt(shell, timeout=18) -> str: buf = b"" end = time.time() + timeout while time.time() < end: time.sleep(0.12) if shell.recv_ready(): buf += shell.recv(65535) end = time.time() + 1.8 if b"> " in buf[-200:]: break return buf.decode("utf-8", "replace") def cmd(shell, line: str, timeout=25) -> str: shell.send(line + "\r") out = wait_prompt(shell, timeout) print("\n===== " + line[:120] + " =====") print(out[-4000:]) return out def fetch_rsc() -> str: remote_py = HERE / "_tmp_fetch_rsc.py" remote_py.write_text( "import urllib.request\n" "req=urllib.request.Request('http://127.0.0.1/provision/test-enjoy.rsc', headers={'Host':'wifi.otantik.cm'})\n" "data=urllib.request.urlopen(req, timeout=25).read()\n" "open('/tmp/wo-test-enjoy.rsc','wb').write(data)\n" "print('LEN', len(data))\n" "print('HEAD', data[:180])\n" "print('TAIL', data[-350:])\n", encoding="utf-8", ) vps_ssh.put(str(remote_py), "/tmp/wo-fetch-rsc.py") code, out = vps_ssh.run("python3 /tmp/wo-fetch-rsc.py") print("FETCH:\n", out) if code != 0: raise SystemExit("fetch failed") client = vps_ssh.connect() try: sftp = client.open_sftp() sftp.get("/tmp/wo-test-enjoy.rsc", str(RSC_LOCAL)) sftp.close() finally: client.close() text = RSC_LOCAL.read_text(encoding="utf-8", errors="replace") print("local bytes", len(text.encode("utf-8"))) return text def try_login(): last = None for host, user, pw in LAB_CANDIDATES: mk = paramiko.SSHClient() mk.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: print(f"try {host} user={user} pass={'*' * len(pw) if pw else '(empty)'}") mk.connect( host, username=user, password=pw, timeout=8, allow_agent=False, look_for_keys=False, banner_timeout=12, ) print("LOGIN OK", host) return mk except Exception as e: last = e print(" fail:", type(e).__name__, e) try: mk.close() except Exception: pass raise SystemExit(f"no lab login: {last}") def main() -> None: text = fetch_rsc() mk = try_login() shell = mk.invoke_shell(width=220, height=60) wait_prompt(shell, 15) cmd(shell, "/system identity print") cmd(shell, "/system resource print") sftp = mk.open_sftp() with sftp.file("wo-lab-test.rsc", "w") as f: f.write(text.replace("\r\n", "\n").replace("\r", "\n")) sftp.close() cmd(shell, "/file print where name~\"wo-lab\"") cmd(shell, "/import file-name=wo-lab-test.rsc verbose=yes", timeout=120) time.sleep(5) cmd(shell, "/interface wireguard print") cmd(shell, "/ip hotspot print") cmd(shell, "/log print where message~\"syntax\" or message~\"expected\" or message~\"failure\"") cmd(shell, "/log print without-paging where topics~\"script\"") mk.close() if __name__ == "__main__": main()