#!/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 JUMP = ("100.80.255.113", "otantik", "DeararbsFr2@") MK = ("172.30.0.1", "admin", "demopass") def wait_prompt(shell, timeout=30) -> str: buf = b"" end = time.time() + timeout while time.time() < end: time.sleep(0.15) if shell.recv_ready(): buf += shell.recv(65535) end = time.time() + 2.2 if b"> " in buf[-250:]: break return buf.decode("utf-8", "replace") def cmd(shell, line: str, timeout=50) -> str: shell.send(line + "\r") out = wait_prompt(shell, timeout) print("\n===== " + line[:90] + " =====") print(out[-5000:]) return out def get_bootstrap() -> str: py = HERE / "_tmp_fetch_boot.py" py.write_text( "import urllib.request\n" "req=urllib.request.Request('http://127.0.0.1/provision/test-enjoy.boot.rsc', headers={'Host':'wifi.otantik.cm'})\n" "data=urllib.request.urlopen(req, timeout=20).read()\n" "open('/tmp/wo-boot.rsc','wb').write(data)\n" "print(data.decode('utf-8','replace'))\n", encoding="utf-8", ) vps_ssh.put(str(py), "/tmp/wo-fetch-boot.py") code, out = vps_ssh.run("python3 /tmp/wo-fetch-boot.py") print("BOOT FILE:\n", out) # also print PHP one-liner style from live rsc endpoint meta via shell return out def main() -> None: get_bootstrap() # fetch one-liner from wifi_otantik by simulating what UI shows - use ros7 bootstrap from known pattern boot_cmd = ( '/tool fetch address=83.147.38.107 src-path="/provision/test-enjoy.rsc" mode=http dst-path=wo.rsc; ' ':delay 2s; :do { /system scheduler remove [find name="wo-setup"] } on-error={}; ' '/system scheduler add name=wo-setup interval=5s policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon ' 'on-event=":do { /import file-name=wo.rsc } on-error={}; /system scheduler remove [find name=wo-setup]"' ) print("BOOTSTRAP LEN", len(boot_cmd)) jump = paramiko.SSHClient() jump.set_missing_host_key_policy(paramiko.AutoAddPolicy()) jump.connect(JUMP[0], username=JUMP[1], password=JUMP[2], timeout=25, allow_agent=False, look_for_keys=False) chan = jump.get_transport().open_channel("direct-tcpip", (MK[0], 22), ("127.0.0.1", 0)) mk = paramiko.SSHClient() mk.set_missing_host_key_policy(paramiko.AutoAddPolicy()) mk.connect(MK[0], username=MK[1], password=MK[2], sock=chan, timeout=25, allow_agent=False, look_for_keys=False) shell = mk.invoke_shell(width=240, height=60) wait_prompt(shell, 20) cmd(shell, "/system identity print") print("\n*** PASTE BOOTSTRAP ONE-LINER ***") out = cmd(shell, boot_cmd, timeout=60) time.sleep(12) cmd(shell, "/system scheduler print") cmd(shell, "/file print where name~\"wo\"") cmd(shell, "/log print without-paging where topics~\"script\" or message~\"syntax\" or message~\"expected\"") cmd(shell, "/interface wireguard print") mk.close() jump.close() if __name__ == "__main__": main()