Paramiko的exec_command在完成命令执行之前退出

时间:2020-07-30 12:03:47

标签: python-3.x

我有一个正在使用的curl命令执行以下步骤:

  1. 从开发存储库中下载远程主机上的“ application.bin”文件。
  2. 使用该“ application.bin”文件开始安装。
  3. 作为安装的一部分,它将下载jre文件和其他2个依赖于应用程序的文件。

以上所有活动仅通过触发curl命令即可发生。

现在,我想使用python paramiko库连接到远程linux主机并在其上执行curl命令以完全安装该应用程序。

当我执行以下命令时,它正在执行步骤1和步骤2,然后退出。它没有执行步骤3。

我在这里错过了什么吗?

import time
def execute_cmd(self, command, timeout=120):
    try:
        stdin, stdout, stderr = ssh_client.exec_command(command=command, timeout=timeout)
    counter = 0 

        flag = stdout.channel.exit_status_ready()

        while not flag:
            counter = counter + 5
            time.sleep(5)
            flag = stdout.channel.exit_status_ready()
            if counter > timeout:
                break

        if flag is True:
            rc = stdout.channel.recv_exit_status()
            if rc == 0:
                output = stdout.read()
                return output
            else:
                raise Exception(“Command not executed properly.”)
        else:
            raise Exception("Command not executed properly.")
    except socket.timeout as e:
        print(e)
    except paramiko.SSHException:
        print(“Command not executed properly.”)

0 个答案:

没有答案