"""Minimal non-interactive SSH runner (password auth). Usage: python ssh_run.py "" ["" ...] Password is read from the AH_SSH_PASS environment variable. """ import os import sys import paramiko for _stream in (sys.stdout, sys.stderr): try: _stream.reconfigure(encoding="utf-8", errors="replace") except Exception: # noqa: BLE001 pass DEFAULT_HOST = "root@172.20.32.38" def main(): args = sys.argv[1:] target = DEFAULT_HOST if args and "@" in args[0]: target = args[0] args = args[1:] user, host = target.split("@", 1) port = int(os.environ.get("AH_SSH_PORT", "22")) password = os.environ.get("AH_SSH_PASS") if not password: raise SystemExit("AH_SSH_PASS is not set") commands = args or ["hostname", "uptime"] client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect( hostname=host, port=port, username=user, password=password, timeout=20, auth_timeout=25, banner_timeout=25, look_for_keys=False, allow_agent=False, ) print(f"CONNECTED {user}@{host}:{port}") for cmd in commands: print(f"\n===== $ {cmd}") try: _, stdout, stderr = client.exec_command(cmd, timeout=90) out = stdout.read().decode("utf-8", "replace").rstrip() err = stderr.read().decode("utf-8", "replace").rstrip() code = stdout.channel.recv_exit_status() if out: print(out) if err: print("[stderr]", err) print(f"[exit {code}]") except Exception as exc: # noqa: BLE001 print(f"[command failed] {type(exc).__name__}: {exc}") client.close() print("\nDISCONNECTED") if __name__ == "__main__": main()