如何保持SSH隧道活着

美好的一天,我有shell脚本做了很多事情,包括打开SSH隧道到特定的服务器和服务。 没有问题,设置隧道,testing,closures隧道。 但我有一个保持隧道活着的问题。 不幸的是,我不能改变服务器上的configuration,所以我需要在我身边实施某种“保持活动的机制”。 这是打开隧道的function:

ssh_tunnel_up () { if lsof -Pi :${local_port} -sTCP:LISTEN -t > /dev/null ; then echo "Port is in use, please check with netstat -anlp | grep ${local_port}. Exiting..." else /usr/bin/ssh -24 -fN ${username}@${node_dev} -L ${server_local_port}:${server_dev}:${server_remote_port} if [[ $? -eq 0 ]]; then echo "Tunnel to Server UI created successfully" else echo "An error occurred creating a tunnel RC was $?" fi fi 

}

这是终止隧道的function:

 ssh_tunnel_down () { if lsof -Pi :${local_port} -sTCP:LISTEN -t >/dev/null ; then while lsof -t -i:${local_port} > /dev/null ; do echo "Port is in use. Closing the port" kill -9 $( lsof -t -i:${local_port} ) sleep 1 done else echo "Port is already free. Nothing to do. Exiting..." fi } 

我的问题是,如何使隧道保持活跃状态​​,以及如何在终止隧道时终止“保活机制”。 我想我必须使用, 而做完循环,但我不知道如何实现这一点。

亚历克斯,非常感谢

在(HOMEDIR)/。ssh / config中添加:

 Host * ServerAliveInterval 60