我想从一个码头容器中运行一个ssh端口转发命令(应该在一个新的终端窗口中打开)。我有一个python script,它在本地的ubuntu上做得很好。
import os
command = "ssh -4 -N -L 322:localhost:322 toing@localhost -p 654"
os.system("gnome-terminal -e 'bash -c \"" + command + ";bash\"'")但是,当我在docker容器中尝试这个命令时,我会得到以下错误:
选项“-e”被废弃,并可能在稍后版本的gnome终端中删除。
使用“--”终止选项,并将命令行放在之后执行。
无法连接到可访问性总线:未能连接到套接字/tmp/dbus-XETYD1whMB:连接被拒绝
加载模块“canberra-gtk-模块”失败
加载模块“canberra-gtk-模块”失败
我使用以下命令运行docker映像(该显示实际上用于脚本中的另一个进程):
docker run -it -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -p 8090:8090 xxxxxxx在容器运行时,我还尝试在python终端中运行相同的命令,但得到了以下响应:
>>> 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我需要这个开放的终端在后台运行,我怎样才能做到这一点?
发布于 2020-03-18 20:23:05
因此,我没有打开终端,而是使用@dstromberg建议的子进程在后台运行它。我最初想使用终端方法,因为SSH连接要求一个是/否的分析器来添加指纹,然后密码too.After -- SSH连接没有使用默认端口。
然而,这都是可能的使用子进程。首先,需要使用以下方法安装sshpass
sudo apt-get install sshpass之后,
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)然后做你的东西,然后当你想要杀死端口转发:
proc.kill()由于sometimes不起作用,您也可以尝试:
import signal
import os
p_id = os.getpgid(proc.pid)
os.killpg(p_id, signal.SIGTERM)至于SSH中的-4选项,它强制使用IPv4。看哪!不需要复杂的背景终端。
https://stackoverflow.com/questions/60728878
复制相似问题