Python ssh需要保持多个通道对一个会话开放

时间:2017-02-24 19:02:05

标签: python ssh paramiko

我的测试设置需要在远程计算机上启动不同的应用程序来编排每个测试。远程应用程序,如Java / Selenium等。好的是这些应用程序不会被终止,除非被杀死。我使用python并尝试使用Paramiko。 Paramiko exec_command在执行命令后关闭通道,该命令在一秒钟内终止启动的进程。读取stdout反对启动进程停止脚本进度(到目前为止没有多线程)。什么可以是一个工作来保持启动的过程'活着,脚本进步。

Paramiko设备登录

            key = paramiko.SSHClient()
            key.set_missing_host_key_policy(paramiko.AutoAddPolicy())

            if testbed[name]['password_type'] == "key" :
                key.connect(testbed[name]['ipaddress'], username=testbed[name]['username'], key_filename=testbed[name]['password_key'])
            else:
                key.connect(testbed[name]['ipaddress'], username=testbed[name]['username'],password=testbed[name]['password'])

            logging.info ( "Connected to %s" % testbed[name]['ipaddress'])

        except paramiko.AuthenticationException:
            logging.info ( "Authentication failed when connecting to %s" % testbed[name]['ipaddress'])
            sys.exit(1)

在远程计算机(窗口)上启动进程 -

        command = 'javaw -jar %s' %(filename)
        logging.info(command)
        handle.exec_command(command)
        time.sleep(1)

        cmd = "wmic process where Caption='java.exe' get Processid"
        stdin, stdout, stderr = handle.exec_command(cmd)
        output = stdout.read().strip().split()
        logging.info(output)
        pid = output[1]
        logging.info("java started with PID = %s" %(pid))

我们此时获得PID,但几秒后 -

        cmd = "wmic process where Caption='java.exe' get Processid"
        stdin, stdout, stderr = handle.exec_command(cmd)
        logging.info(stdout.read() + stderr.read())

返回 - "没有可用的实例。"

使用pycharm从OSX运行程序时出现此错误。使用Iterm和python命令执行以下步骤的同一台机器,我没有看到关闭的通道(两个案例的whoami返回相同的用户配置文件) -

    >>> dev.connect("a.b.c.d",username="pqr",password="xyz")
    >>> dev.exec_command("javaw -jar selenium-server-standalone-3.1.0.jar")
    (<paramiko.ChannelFile from <paramiko.Channel 0 (open) window=262144 -> <paramiko.Transport at 0xfa06b10L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>, <paramiko.ChannelFile from <paramiko.Channel 0 (open) window=262144 -> <paramiko.Transport at 0xfa06b10L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>, <paramiko.ChannelFile from <paramiko.Channel 0 (open) window=262144 -> <paramiko.Transport at 0xfa06b10L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>)
    >>> br = webdriver.Remote(command_executor=url,desired_capabilities=options.to_capabilities())
>>> dev.exec_command("START /B C:\PROGRA~1\abc.exe")
(<paramiko.ChannelFile from <paramiko.Channel 1 (open) window=262144 -> <paramiko.Transport at 0xfa06b10L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>, <paramiko.ChannelFile from <paramiko.Channel 1 (open) window=262144 -> <paramiko.Transport at 0xfa06b10L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>, <paramiko.ChannelFile from <paramiko.Channel 1 (open) window=262144 -> <paramiko.Transport at 0xfa06b10L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>)

2 个答案:

答案 0 :(得分:0)

到目前为止工作的是 -

  1. 使用paramiko创建ssh句柄到远程机器。
  2. 使函数启动应用程序并在启动后继续读取stdout, 这些函数使用作为参数传递的ssh句柄。
  3. 从主脚本/程序 - 启动这些功能的线程。
  4. 主要执行休息任务,线程正在读取标准输出。 Main继续使用ssh句柄。
  5. 完成后,main使用ssh句柄并关闭应用程序。
  6. stdout关闭,线程连接到main。
  7. 随意批评这种做法。

答案 1 :(得分:-1)

我认为你可能需要创建可以在后台运行的Paramiko exec_command。

请检查Getting ssh to execute a command in the background on target machine是否有助于构建命令。