0

The goal is to establish the reverse ssh connection from machine-a to machine-b, so I could ssh connect from machine-b to machine-a after the reverse connection is established.

After importing os module I go ahead and use os.system to ssh connect to a remote server using ssh -R 2210:localhost:22 [email protected] & command. Please note that the command line ends with & character because I want to place the ssh process in the background:

import os 
result = os.system("ssh -R 2210:localhost:22 [email protected] &")
print("...started ssh connection", result)

But I am getting the error and as a result the ssh connection is not established:

Pseudo-terminal will not be allocated because stdin is not a terminal.

I have tried to run it with subprocess hoping it will work:

import subprocess
cmd = ["ssh", "-R", "2210:localhost:22", "[email protected]", "&"]
result = subprocess.Popen(cmd, shell=False)

but it throws another error:

bash: -c: line 0: syntax error near unexpected token `&'

Is there a way to run ssh connection from Python placing it at the background?

15
  • Do not use os.system. Never. Not under any circumstances. The limitations it adds to subprocess.Popen are all for the worse: can't use shell=False to avoid injection attacks, can't pass arguments out-of-band from code to run, can't configure file descriptor initialization -- and those limitations actively prevent good security practices from being followed. Commented Jul 29, 2021 at 16:15
  • Anyhow, you don't need & on a subprocess.Popen command, because Python already starts your process in the background (which is to say, without an implicit wait). If you wanted a foreground process you'd have to use something like subprocess.call() instead, or explicitly invoke a blocking function like communicate() or wait() on the returned object. Commented Jul 29, 2021 at 16:15
  • So the & command is parsed and recognized by the shell before running the actual process. As you can see, this doesn't work when invoking a subprocess in Python (it likely calls something lower than the shell, such as execv). Regardless, Popen technically already opens the process in the background. You can check on the process status with result in your case. Commented Jul 29, 2021 at 16:15
  • 2
    Can't post answers, the question is closed. Commented Jul 29, 2021 at 17:05
  • 1
    Also, we have a lot of duplicates already in the knowledgebase that cover this ground, and cover it better than I could whipping something out in 30 seconds. I've added a few of them to the duplicate list now that you clarified the question to make it clear that it's about setting up port forwarding; both the ones I added have both answers showing how to do this with OpenSSH (using the -N argument), and ones that show how to do it with paramiko. Commented Jul 29, 2021 at 17:09

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.