退出SSH隧道而不关闭它

时间:2016-01-05 20:37:39

标签: bash ssh tunnel

我正在尝试编写一个设置一堆端口转发的脚本。那部分工作正常,我遇到的麻烦是弄清楚如何建立一个SSH隧道,然后(基本上)从我的本地分离而不关闭隧道?

#!/bin/bash


# Text (Foreground) Colors
norm_text='\e[0m'
black_text='\e[30m'
red_text='\e[31m'
green_text='\e[32m'
yellow_text='\e[33m'
blue_text='\e[34m'
magenta_text='\e[35m'
cyan_text='\e[36m'
lgray_text='\e[37m'
dgray_text='\e[90m'
lred_text='\e[91m'
lgreen_text='\e[92m'
lyellow_text='\e[93m'
lblue_text='\e[94m'
lmagenta_text='\e[95m'
lcyan_text='\e[96m'
white_text='\e[97m'

# Text (Background) Colors
norm_back='\e[49m'
black_back='\e[40m'
red_back='\e[41m'
green_back='\e[42m'
yellow_back='\e[43m'
blue_back='\e[44m'
magenta_back='\e[45m'
cyan_back='\e[46m'
lgray_back='\e[47m'
dgray_back='\e[100m'
lred_back='\e[101m'
lgreen_back='\e[102m'
lyellow_back='\e[103m'
lblue_back='\e[104m'
lmagenta_back='\e[105m'
lcyan_back='\e[106m'
white_back='\e[107m'
#-------------------------------

function menu() {
echo -e "\t==============="
echo -e "\t${yellow_text}Port Forwarding${norm_text}"
echo -e "\t==============="
echo -e "${green_text}"
echo -e "1.) Start Port Forwarding"
echo -e "2.) Stop Port Forwarding"
echo -e "${norm_text}"
echo -e "${red_text}5.) Exit${norm_text}"
echo -e ""
echo -en "Please enter an option: "
}
menu
read -n1 OPTION
case "$OPTION" in
    "1")
        echo -e ""
        echo -e ""
        read -p "What is your  z-id?" USER
        USER="$USER"
        ssh -N -L 6111:remote.server.org:8080 -L 6112:remote.server.org:8080 -L 6121:remote.server.org:8080 -L 6122:remote.server.org:8080 -L 6211:remote.server.org:8080 -L 6212:remote.server.org:8080 -L 6214:remote.server.org:8080 -L 6215:remote.server.org:8080 -L 6221:remote.server.org:8080 -L 6222:remote.server.org:8080 -L 6224:remote.server.org:8080 -L 6225:remote.server.org:8080 -L 6311:remote.server.org:8080 -L 6312:remote.server.org:8080 -L 6314:remote.server.org:8080 -L 6315:remote.server.org:8080 -L 6321:remote.server.org:8080 -L 6322:remote.server.org:8080 -L 6324:remote.server.org:8080 -L 6325:remote.server.org:8080 -L 6411:remote.server.org:8080 -L 6412:remote.server.org:8080 -L 6421:remote.server.org:8080 -L 6422:remote.server.org:8080 -L 7111:remote.server.org:8080 -L 7112:remote.server.org:8080 -L 7113:remote.server.org:8080 -L 7121:remote.server.org:8080 -L 7122:remote.server.org:8080 -L 7123:remote.server.org:8080 -L 7211:remote.server.org:8080 -L 7212:remote.server.org:8080 -L 7213:remote.server.org:8080 -L 7221:remote.server.org:8080 -L 7222:remote.server.org:8080 -L 7223:remote.server.org:8080 -L 7311:remote.server.org:8080 -L 7312:remote.server.org:8080 -L 7313:remote.server.org:8080 -L 7321:remote.server.org:8080 -L 7322:remote.server.org:8080 -L 7323:remote.server.org:8080 -L 7411:remote.server.org:8080 -L 7412:remote.server.org:8080 -L 7413:remote.server.org:8080 -L 7421:remote.server.org:8080 -L 7422:remote.server.org:8080 -L 7423:remote.server.org:8080 -L 7511:remote.server.org:8080 -L 7512:remote.server.org:8080 -L 7513:remote.server.org:8080 -L 7521:remote.server.org:8080 -L 7522:remote.server.org:8080 -L 7523:remote.server.org:8080 -L 7611:remote.server.org:8080 -L 7612:remote.server.org:8080 -L 7613:remote.server.org:8080 -L 7621:remote.server.org:8080 -L 7622:remote.server.org:8080 -L 7623:remote.server.org:8080 -L 8111:remote.server.org:7034 -L 8121:remote.server.org:7034 -L 8211:remote.server.org:7034 -L 8221:remote.server.org:7034 -L 8311:remote.server.org:7034 -L 8314:remote.server.org:7034 -L 8321:remote.server.org:7034 -L 8324:remote.server.org:7034 -L 8411:remote.server.org:7034 -L 8421:remote.server.org:7034 "${USER}"@tunnel.server.org
        echo -e ""
        echo -e "Local Port 6111 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6112 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6121 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6122 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6211 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6212 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6214 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6215 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6221 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6222 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6224 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6225 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6311 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6312 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6314 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6315 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6321 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6322 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6324 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6325 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6411 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6412 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6421 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 6422 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7111 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7112 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7113 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7121 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7122 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7123 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7211 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7212 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7213 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7221 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7222 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7223 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7311 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7312 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7313 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7321 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7322 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7323 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7411 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7412 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7413 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7421 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7422 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7423 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7511 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7512 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7513 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7521 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7522 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7523 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7611 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7612 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7613 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7621 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7622 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 7623 is now resolving content from \"remote.server.org:8080\""
        echo -e "Local Port 8111 is now resolving content from \"remote.server.org:7034\""
        echo -e "Local Port 8111 is now resolving content from \"remote.server.org:7034\""
        echo -e "Local Port 8121 is now resolving content from \"remote.server.org:7034\""
        echo -e "Local Port 8211 is now resolving content from \"remote.server.org:7034\""
        echo -e "Local Port 8221 is now resolving content from \"remote.server.org:7034\""
        echo -e "Local Port 8311 is now resolving content from \"remote.server.org:7034\""
        echo -e "Local Port 8314 is now resolving content from \"remote.server.org:7034\""
        echo -e "Local Port 8321 is now resolving content from \"remote.server.org:7034\""
        echo -e "Local Port 8324 is now resolving content from \"remote.server.org:7034\""
        echo -e "Local Port 8411 is now resolving content from \"remote.server.org:7034\""
        echo -e "Local Port 8421 is now resolving content from \"remote.server.org:7034\""
        echo -e ""
        echo -e "Port Forwarding ${red_back}${yellow_text}Active${norm_text}${norm_back}:"
    ;;
esac

截至目前,它提示您输入密码,端口转发隧道效果很好,但我的脚本挂起......我希望有一个交互式菜单,例如"关闭隧道"等等。

我知道我有" -N"现在选项," 执行无命令" ...我已将其删除并尝试了类似

的简单操作
remote.server.org:7034 -L 8421:remote.server.org:7034  "${USER}"@tunnel.server.org "sleep 10 &"

暂停然后将工作作为背景,但这是在远程系统上进行后台处理,而不是在这里...但它无论如何都会立即崩溃。

我研究了一堆,发现了" -S -O" ...虽然我已经阅读了MAN页面上的SSH,但我仍然没有牢牢掌握如何正确实施它,或者甚至是我真正想要的东西对

我希望这是有道理的。非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

典型用例是在命令中添加-NTf个开关。来自ssh的手册页:

  

<强> -f   请求ssh在命令执行之前转到后台。 [...]

     

<强> -N   不要执行远程命令。这对于仅转发端口非常有用(仅限协议版本2)。

     

<强> -T   禁用伪tty分配。