如何在脚本中使用ssh等待在远程计算机上启动的进程?

时间:2014-11-27 15:40:54

标签: python ssh cross-platform paramiko

我有一个脚本,在特定情况下需要在远程计算机上运行非常耗时的脚本。我目前这样做:

if specific_case:
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect('10.0.0.1', username='username', password='password')
    stdin, stdout, stderr = client.exec_command('tmux new-session -d -s session_name \'python /home/username/flamethrower.py'\'')

现在 - 到目前为止一直很好..我怎么能在最初启动它的脚本中等待python /home/username/flamethrower.py并且可能有简约的反馈?

运行在远程计算机上启动flamethrower.py的脚本的计算机是Windows 8.1,运行flamethrower.py的计算机是Debian wheezy。

伪编码:

[...] # as above
while not stdin, stdout, stderr = client.exec_command('tmux new-session -d -s session_name \'python /home/username/flamethrower.py'\''):

    #while it's not done, print a dot every minute
    sys.stdout.print('.')
    sys.flush()
    sleep(60)

1 个答案:

答案 0 :(得分:2)

将远程脚本输出到某个日志文件,包括完成后,然后每隔一段时间轮询一次远程连接以获得完成输出,(并获取剩余的输出)。