python远程服务器并在服务器输入命令

import paramiko

# SSH连接配置
hostname = "172.16.13.122"
username = "root"
password = "Hnrs@2023"
# 创建SSH客户端
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
    # 连接服务器
    client.connect(hostname, username=username, password=password)
    # 执行命令
    command = "smidump -f python -k SANGFOR-GENERAL-MIB.mib > output.py"

    stdin, stdout, stderr = client.exec_command(command)

    # 获取命令执行结果
    output = stdout.read().decode("utf-8")
    error = stderr.read().decode("utf-8")

    if error:
        print(f"命令执行失败:{error}")
    else:
        # 处理output,比如保存为文件
        with open("output.py", "w") as f:
            f.write(output)
finally:
    # 关闭SSH连接
    client.close()

  目录