#!/usr/bin/env python3 """SSH exec on the first production box (192.168.88.1). Not the RB4011.""" from __future__ import annotations import sys import paramiko HOST = "192.168.88.1" USER = "admin" PASSWORD = "" def connect() -> paramiko.SSHClient: mk = paramiko.SSHClient() mk.set_missing_host_key_policy(paramiko.AutoAddPolicy()) mk.connect( HOST, username=USER, password=PASSWORD, timeout=25, banner_timeout=30, allow_agent=False, look_for_keys=False, ) return mk def run(cmd: str, timeout: int = 60) -> str: mk = connect() try: stdin, stdout, stderr = mk.exec_command(cmd, timeout=timeout) out = stdout.read().decode("utf-8", "replace") err = stderr.read().decode("utf-8", "replace") return (out + ("\n" + err if err.strip() else "")).strip() finally: mk.close() def import_rsc(name: str, content: str, timeout: int = 120) -> str: mk = connect() try: sftp = mk.open_sftp() with sftp.file(name, "w") as f: f.write(content) sftp.close() stdin, stdout, stderr = mk.exec_command("/import file-name=" + name, timeout=timeout) out = stdout.read().decode("utf-8", "replace") err = stderr.read().decode("utf-8", "replace") return (out + ("\n" + err if err.strip() else "")).strip() finally: mk.close() if __name__ == "__main__": cmd = sys.argv[1] if len(sys.argv) > 1 else "/system identity print" print(run(cmd, timeout=int(sys.argv[2]) if len(sys.argv) > 2 else 60))