Paramiko 运行 脚本并退出

Paramiko run script and exit

我正在尝试使 paramiko 运行 成为外部机器上的脚本并退出,但在使其成为 运行 脚本时遇到问题。有谁知道为什么它不 运行ning 脚本?我已经尝试 运行 VM 上的命令手册并且成功了。

command = "/home/test.sh > /dev/null 2>&1 &"

def start_job(host):

    try:
        client = paramiko.SSHClient()
        client.load_system_host_keys()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        client.connect(hostname, port=22, username=username, password=password)
        return client.exec_command(command)

    finally:
        client.close()

start_job(hostname)

首先,start_job 函数中的参数或 client.connect() 的第一个参数存在错字。除此之外,关闭连接前 return stdout/stderr 内容:

def start_job(hostname):

try:
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    client.connect(hostname, port=22, username=username, password=password)
    (_, stdout, stderr) = client.exec_command(command)
    return (stdout.read(), stderr.read())

finally:
    client.close()

在你的情况下,命令将 stderr 重定向到 stdout 并在后台 运行 所以我不希望只有两个空字符串被 return 回退:

start_job(hostname)
('', '')