且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

等待任务通过 Python 在远程机器上完成

更新时间:2023-12-05 14:30:34

这确实是 paramiko SSH exec_command(shell script) 在完成之前返回,但是那里的答案不是很详细.所以...

This is indeed a duplicate of paramiko SSH exec_command(shell script) returns before completion, but the answer there is not terribly detailed. So...

如您所见,exec_command 是一个非阻塞调用.因此,您必须使用以下任一方法等待远程命令完成:

As you noticed, exec_command is a non-blocking call. So you have to wait for completion of the remote command by using either:

在您的特定情况下,您需要后者:

In your particular case, you need the later:

stdin, stdout, stderr = client.exec_command(filedelete)  # Non-blocking call
exit_status = stdout.channel.recv_exit_status()          # Blocking call
if exit_status == 0:
    print ("File Deleted")
else:
    print("Error", exit_status)
client.close()