2016-06-12 65 views
0

我没有强大的硬件,因此无法同时运行多个ssh隧道,否则会导致CPU负载过高,我的目标是运行一个SSH隧道另一个连接后,如果我的SSH的一个断开连接重新连接,所以基本上是这样的:Bash脚本 - 如何在连接另一个连接后运行ssh

while true; do 
if (1st ssh isn't connected); then 
    connect the first ssh 
elif (1st ssh is finally connected); then 
    run the second ssh 
elif (2nd ssh is finally connected); then 
    run the 3rd ssh 
fi 
sleep 1 
done 

的问题是,SSH隧道的数量不断变化,有时用户想运行3个ssh隧道,有时需要5个,看起来像这样运行脚本:

mytunnel.sh -a [number of tunnels they wanna run] 

我正在考虑for loop,但我无法弄清楚如何在for loop内编写它。请帮帮我。

回答

0

这里是一个for循环中,您可以使用:

#!/usr/local/bin/bash 

LOOP=$1 
for ((c=1; c<=$LOOP; c++)) 
do 
     echo "$c " 
done 

替换echo与命令,并与任何命令行LOOP ARG你会使用。本示例读取命令行参数1(即$ 1)。

实施例执行:

enter image description here

0

棘手。不幸的是,我认为ssh在连接隧道时不会返回任何内容,也不会在连接断开时立即退出。

相反,你可能想要做的是做一个端口监视器,定期检查端口是否接受连接,并产生一个新的SSH隧道(如果不是,可能会终止旧的SSH过程)。