0

I want to run a ssh port forwarding command (that should open inside a new terminal window or an equivalent) from inside a docker container. I have a python script that does this perfectly on my local ubuntu.

import os
command = "ssh -4 -N -L 322:localhost:322 toing@localhost -p 654"
os.system("gnome-terminal -e 'bash -c \"" + command + ";bash\"'")

However when I try this command inside a docker container, I get the following error:

Option “-e” is deprecated and might be removed in a later version of gnome-terminal.

Use “-- ” to terminate the options and put the command line to execute after it.

Couldn't connect to accessibility bus: Failed to connect to socket /tmp/dbus-XETYD1whMB: Connection refused

Failed to load module "canberra-gtk-module"

Failed to load module "canberra-gtk-module"

I am running the docker image with the following command (the display is really for another process in the script):

docker run -it  -e DISPLAY=$DISPLAY  -v /tmp/.X11-unix:/tmp/.X11-unix -p 8090:8090 xxxxxxx

I also tried running the same command inside the python terminal inside the container when it is running but got the following response:

>>> os.system("gnome-terminal -e 'bash -c \"" + command + ";bash\"'")
# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.
# Couldn't connect to accessibility bus: Failed to connect to socket /tmp/dbus-XETYD1whMB: Connection refused
# Failed to load module "canberra-gtk-module"
# Failed to load module "canberra-gtk-module"
# Error constructing proxy for org.gnome.Terminal:/org/gnome/Terminal/Factory0: Failed to execute child process “dbus-launch” (No such file or directory)
256

I need this open terminal to be running in the background, How can I achieve this?

toing_toing
  • 2,334
  • 1
  • 37
  • 79
  • 1
    I'm a little puzzled. Why run your port forwarding in a terminal? Couldn't yet just run it in the background? Anyway, what if you put together a short command like "echo foo" to run under gnome-terminal, and if that works then slowly build up from there to the command you ultimately want. – dstromberg Mar 17 '20 at 19:33
  • 1
    Why do you want to run this from inside a Docker container at all? What happens if `command` includes punctuation like quotes or semicolons? – David Maze Mar 17 '20 at 23:33
  • There is a vpn configured inside the container that is needed for the SSH connection.I solved this using subprocess. Will update the anwser soon – toing_toing Mar 17 '20 at 23:36

1 Answers1

0

So, instead of opening a terminal, I ran it in the background using subprocess as @dstromberg proposed. I initially wanted to use the terminal approach because the SSH connection was demanding a yes/no anwser to add the fingerprints, and then the password too.After all that the SSH connection didn't use the default port.

However, this is all possible using subprocess. First you need to have sshpass installed using

sudo apt-get install sshpass

Afterwards,

import subprocess
command = ['sshpass', '-p', 'imnottellingyou', 'ssh', '-oStrictHostKeyChecking=accept-new',  '-4', '-N', '-L',  '55:localhost:55', 'toing@localhost', '-p', '654',]
proc = subprocess.Popen(command, close_fds=True)
# now do whatever you want (the code, please)

And then do your stuff, and then when you want to kill the port forwarding:

proc.kill()

As kill() doesn't work sometimes, you can also try:

import signal
import os

p_id = os.getpgid(proc.pid)
os.killpg(p_id, signal.SIGTERM)

As for the -4 option in the SSH, it forces IPv4. Et voila! No need for complex background terminal stuff.

toing_toing
  • 2,334
  • 1
  • 37
  • 79