使用Python Paramiko执行命令时不要等待命令完成 [英] Do not wait for commands to finish when executing them with Python Paramiko

查看:56
本文介绍了使用Python Paramiko执行命令时不要等待命令完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Paramiko库运行一系列远程shell命令.

I am running a series of remote shell commands leveraging the Paramiko library.

最后一条命令需要用户在终端设备上输入,而我不需要等待执行完成;但是,直到最后一条命令完成,我才能继续.

The final command requires user input at the end device and I do not need to wait for execution to complete; however, I cannot proceed until the last command completes.

代码:

k = paramiko.RSAKey.from_private_key_file("....", password="....")
conn = paramiko.SSHClient()
conn.connect(hostname=ip_addr, username="root", pkey=k, password="....")

commands = ["chmod 755 /tmp/evtool", "nohup /tmp/test.sh >/dev/null 2>&1"]

for command in commands:
    stdin, stdout, stderr = conn.exec_command(command)
conn.close()

有问题的命令是:

nohup /tmp/test.sh >/dev/null 2>&1

无论我如何尝试运行它:

No matter how I attempt to run it :

/tmp/test.sh
/tmp/test.sh&

应用程序等待该过程完成;但是,完成过程可能要花费数小时,并且通过/失败会显示在远程单元上,我不需要等待结果.

The application waits for the process to complete; however the completion could take hours and the pass / fail is displayed on the remote unit, I do not need to wait for a result.

我有数十个远程单元,并且从循环访问该单元的ip地址并进行连接/运行测试的循环中调用上述功能.

I have dozens of remote units and the above function is called from a loop that iterates through the unit ip addresses and makes the connection / runs the test.

有什么主意如何不等待最后一个过程完成?

Any ideas how not to wait for the last process to complete?

谢谢,丹.

推荐答案

这实际上不是Python或Paramiko问题.这是一个Linux shell问题.

This is not really a Python or Paramiko question. It's a Linux shell question.

根据在目标计算机的后台获取ssh以在后台执行命令,看来您同时需要两个 nohup & .

According to Getting ssh to execute a command in the background on target machine, it seems that you need both nohup and &.

尽管从您的评论来看,您似乎确实想等待命令完成.您可能只想并行运行多个命令.

Though from your comments, it looks like you actually want to wait for the command to complete. You just maybe want to run multiple command in parallel.

实际上 SSHClient.exec_command 不会等待.因此,我不确定您的问题到底是什么.

Actually the SSHClient.exec_command does not wait. So I'm not sure exactly what your question is.

但是,确实如此,如果您想要的是并行等待多个命令,同时又收集它们的输出,那并不是一件容易的事.像这样的作品:

But indeed, waiting for multiple commands in parallel while collecting their output, if that's what you want, is not that trivial. Something like this works:

commands = [
    "for i in {1..10}; do echo \"0 o:$i\"; sleep 1; echo \"0 e:$i\" >&2; sleep 1; done",
    "for i in {1..10}; do echo \"1 o:$i\"; sleep 1; echo \"1 e:$i\" >&2; sleep 1; done",
    "for i in {1..10}; do echo \"2 o:$i\"; sleep 1; echo \"2 e:$i\" >&2; sleep 1; done",
]

channels = []
for command in commands:
    print(f"starting {len(channels)}")
    stdin, stdout, stderr = ssh.exec_command(command)
    channels.append(stdout.channel)

while any(x is not None for x in channels):
    for i in range(len(channels)):
        channel = channels[i]
        if channel is not None:
            # To prevent losing output at the end, first test for exit, then for output
            exited = channel.exit_status_ready()
            while channel.recv_ready():
                s = channel.recv(1024).decode('utf8')
                print(f"#{i} stdout: {s}")
            while channel.recv_stderr_ready():
                s = channel.recv_stderr(1024).decode('utf8')
                print(f"#{i} stderr: {s}")
            if exited:
                print(f"#{i} done")
                channels[i] = None
    time.sleep(0.1)

这篇关于使用Python Paramiko执行命令时不要等待命令完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆