问题描述
我必须在远程机器上完成一项工作(使用我的网络服务器),大约需要 10 分钟.
I have to do a job (using my web server) on a remote machine that takes about 10 minutes.
我在 python 中使用了 pxssh 模块,但它给了我超时错误"(非阻塞).
I have used pxssh module in python for the same but it gives me "timeout error"(non blocking).
现在,我正在使用 paramiko,但是一旦它给出指令就会返回.
Now, I am using paramiko but that comes back as soon as it gives the instruction.
我希望 Web 服务器等待作业完成.是否有任何可用的 python SSH 模块.
I want the web server to wait till the job is complete. Is there any python SSH module available for this.
或者
我们是否可以通过更改 pxssh
或 paramiko
的任何配置设置来实现相同的效果?
Can we achieve the same by changing any configuration settings of pxssh
or paramiko
?
推荐答案
您可以使用 recv_exit_status
方法在 Channel 上等待命令完成:
You can use the recv_exit_status
method on the Channel to wait for the command to complete:
recv_exit_status(self)
从服务器上的进程返回退出状态.这对于检索 exec_command 的结果非常有用.如果命令还没有完成,这个方法将等待它完成,或者直到通道关闭.如果服务器没有提供退出状态,则返回-1.
Return the exit status from the process on the server. This is mostly useful for retrieving the reults of an exec_command. If the command hasn't finished yet, this method will wait until it does, or until the channel is closed. If no exit status is provided by the server, -1 is returned.
例如:
ssh = paramiko.SSHClient()
ssh.connect(remote, username=username, password=password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("some command")
exit_status = ssh_stdout.channel.recv_exit_status()
这篇关于python的SSH模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!