更新时间: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:
Channel.exit_status_ready
如果你想对命令完成进行非阻塞检查(即:池化)Channel.recv_exit_status
如果你想阻塞直到命令完成(并返回退出状态——退出状态为 0 表示正常完成).在您的特定情况下,您需要后者:
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()