0

My test setup requires to start different applications on remote machine to orchestrate each test. Remote applications like Java/Selenium etc. Good thing is these applications won't terminate unless killed. I'm using python and tried with Paramiko. Paramiko exec_command closes the channel after execution of command, which kills initiated process within a second. Reading stdout against started process stalls script progress (so far no multi-thread). What can be a work around to keep initiated process' alive and script to progress.

Paramiko device login

            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)

To launch process on remote machine (windows) -

        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))

We get PID at this point, but after few seconds -

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

returns - "No Instance(s) Available."

This error when the program is run from OSX using pycharm. Same machine using Iterm and python command following steps are executed, i don't see the channel closed (whoami on both cases returns same user profile) -

    >>> 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))>>>)
shamik
  • 653
  • 1
  • 7
  • 13

2 Answers2

0

What worked so far is -

  1. Create ssh handle using paramiko to remote machine.
  2. Make functionsto start the applications and keeps reading stdout after launch, these functions uses ssh handle passed as argument.
  3. From main script/program - launch threads for those functions.
  4. Main performs rest tasks, threads are reading stdout. Main keeps using ssh handle.
  5. Once done, main uses ssh handle and close application.
  6. stdout closes, thread joins to main.

Feel free to criticize this approach.

shamik
  • 653
  • 1
  • 7
  • 13
-1

I think you may need to create Paramiko exec_command which can run in background.

Please check if Getting ssh to execute a command in the background on target machine can help to build the command.

Community
  • 1
  • 1
Sandip
  • 9
  • 2