#!/usr/bin/env python3 """Inspect a MikroTik via SSH. Default: 192.168.88.1 admin empty password.""" from __future__ import annotations import sys import time import paramiko HOST = sys.argv[1] if len(sys.argv) > 1 else "192.168.88.1" USER = "admin" PASSWORD = sys.argv[2] if len(sys.argv) > 2 else "" 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=18) -> str: shell.send(line + "\r") out = wait_prompt(shell, timeout) print("\n===== " + line + " =====") print(out[-2800:]) return out def main() -> None: mk = paramiko.SSHClient() mk.set_missing_host_key_policy(paramiko.AutoAddPolicy()) mk.connect( HOST, username=USER, password=PASSWORD, timeout=12, allow_agent=False, look_for_keys=False, banner_timeout=15, ) print("LOGIN OK", HOST) shell = mk.invoke_shell(width=200, height=50) wait_prompt(shell, 15) cmd(shell, "/system identity print") cmd(shell, "/system resource print") cmd(shell, "/ip address print") cmd(shell, "/ping 172.30.0.1 count=2") mk.close() if __name__ == "__main__": main()