2017-02-24 29 views
0

我的測試設置需要在遠程計算機上啓動不同的應用程序來編排每個測試。遠程應用程序,如Java/Selenium等好事是這些應用程序不會終止,除非被殺死。我正在使用Python,並嘗試與Paramiko。 Paramiko exec_command在命令執行後關閉通道,這會在一秒鐘內終止啓動的進程。讀取stdout反對啓動進程停止腳本進度(迄今爲止沒有多線程)。什麼可以是一個解決方案,以保持啓動過程'活着和腳本進展。Python ssh需要保持多個通道開放到一個會話

的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以及後面的步驟蟒蛇命令同一臺機器上執行,我看不到通道關閉(在這兩種情況下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))>>>) 
+0

請編輯您的問題,以包含您啓動這些遠程進程的相關源代碼。 – Kenster

+0

@Kenster:請讓我知道這是否有幫助。 – shamik

回答

0

什麼工作至今 -

  1. 創建使用的paramiko到遠程機器的ssh手柄。
  2. 使函數啓動應用程序並在啓動後保持讀取stdout, 這些函數使用作爲參數傳遞的ssh句柄。
  3. 從主腳本/程序 - 啓動這些功能的線程。
  4. 主要執行休息任務,線程正在讀取stdout。 Main繼續使用ssh句柄。
  5. 完成後,主要使用ssh句柄並關閉應用程序。
  6. stdout關閉,線程連接到main。

隨意批評這種方法。