JumpSSH icon indicating copy to clipboard operation
JumpSSH copied to clipboard

Can't execute background process on remote server

Open bradhvr opened this issue 2 years ago • 1 comments

  • Python 3.9.1
  • jumpssh==1.6.5
  • paramiko==2.9.2

When I try to run a remote SSH command as a background process, it doesn't work. The command returns successfully, but the response is blank and it is as if the command was never run. When I run the same command with Paramiko, it works.

Here's a sample script that connects to the given ip and writes the date to a log file as a background process. If you run it, you'll see it works using Paramiko but no log file is created with JumpSSH.

import os
import paramiko
from jumpssh import SSHSession

HOST = '<server ip>'
USER = '<server username>'
ID_FILE = '<path to ssh key>'
CMD = 'date > {}.log &'


print("Using Paramiko\n=========================")
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

private_key = paramiko.RSAKey.from_private_key_file(os.path.expanduser(ID_FILE))
ssh_client.connect(hostname=HOST, username=USER, pkey=private_key)
stdin, stdout, stderr = ssh_client.exec_command(CMD.format('paramiko'))

out = stdout.read().decode().strip()
error = stderr.read().decode().strip()

ssh_client.close()
print("Out: {}\nError: {}".format(out, error))

print("\nUsing jumpssh\n=========================")
gateway_session = SSHSession(HOST, USER, private_key_file=os.path.expanduser(ID_FILE)).open()
output = gateway_session.get_cmd_output(CMD.format('jumpssh'))
print("Out: {}".format(output))

I modified session.py and commented out the get_pty() line at https://github.com/AmadeusITGroup/JumpSSH/blob/51ca8ee726a0ac9aad4887f438c6f1ca7a178211/jumpssh/session.py#L318

Once I did this the test script above started working, and the background process successfully created the log file:

-rw-rw-r-- 1 bhoover bhoover     29 Jan 13 12:19 paramiko.log
-rw-rw-r-- 1 bhoover bhoover     29 Jan 13 12:19 jumpssh.log

Any idea why this doesn't work with get_pty() included? The Paramiko docs look like they discourage its use when running a single command with exec_command, but I'm assuming it was included here for a reason.

bradhvr avatar Jan 13 '22 18:01 bradhvr

@bradhvr thank you for reporting this behaviour! We'll verify it on our side and will keep you posted in regards to a possible fix.

wherka-ama avatar Jan 23 '22 09:01 wherka-ama