31 lines
1.0 KiB
Python
31 lines
1.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Run RouterOS commands over SSH. Creds from env: ROS_HOST, ROS_PORT, ROS_USER, ROS_PASS.
|
|
Commands: one per line on stdin, or via --cmd. Prints output per command."""
|
|
import os, sys, paramiko
|
|
|
|
host = os.environ["ROS_HOST"]
|
|
port = int(os.environ.get("ROS_PORT", "22"))
|
|
user = os.environ["ROS_USER"]
|
|
pw = os.environ["ROS_PASS"]
|
|
|
|
cmds = []
|
|
if "--cmd" in sys.argv:
|
|
cmds = [sys.argv[sys.argv.index("--cmd") + 1]]
|
|
else:
|
|
cmds = [l.rstrip("\n") for l in sys.stdin if l.strip()]
|
|
|
|
cli = paramiko.SSHClient()
|
|
cli.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
cli.connect(host, port=port, username=user, password=pw,
|
|
look_for_keys=False, allow_agent=False, timeout=20)
|
|
|
|
for c in cmds:
|
|
print(f"\n===== CMD: {c}")
|
|
stdin, stdout, stderr = cli.exec_command(c, timeout=30)
|
|
out = stdout.read().decode("utf-8", "replace")
|
|
err = stderr.read().decode("utf-8", "replace")
|
|
sys.stdout.write(out)
|
|
if err.strip():
|
|
sys.stdout.write("--- stderr ---\n" + err)
|
|
cli.close()
|