我的 ssh 隧道设置如下:
ssh -N -L 1105:remote.server:25 office-machine&
我想编写一个脚本来设置检查,比如说每分钟一次(也许我可以使用 cron 作业来完成),它将检查此连接是否已建立,否则它将建立连接(如果 VPN 已打开) )。
我该如何执行 ssh 隧道检查?
提前致谢。
PS:我试过了
autossh -N -L 1105:remote.server:25 office-machine&
但它退出了。我怀疑选项是正确的。事实上,联机帮助页/帮助表明没有 -N 和 -L 选项,而只有 -f 和 -M。
答案1
您可以用于autossh
此目的。引用Ubuntu 联机帮助页:
autossh is a program to start a copy of ssh and monitor it, restarting it as necessary
should it die or stop passing traffic.
The original idea and the mechanism were from rstunnel (Reliable SSH Tunnel). With version
1.2 of autossh the method changed: autossh uses ssh to construct a loop of ssh forwardings
(one from local to remote, one from remote to local), and then sends test data that it
expects to get back. (The idea is thanks to Terrence Martin.)
因此必须指定一个额外的监控端口autossh
。选择一些端口 N,使 N 和 N+1 都空闲,并将其与选项一起使用-M
,例如:
autossh -M 20000 -N -L 1105:remote.server:25 office-machine
您还可以使用 禁用监控-M 0
。在基于 Debian 的系统上,autossh
可以自动选择一个空闲端口进行监控。
如果您的 VPN 连接中断,autossh
应通过监控端口检测 SSH 连接不再有效,然后尝试重新启动它。在那之后:
Continued failures
If the ssh connection fails and attempts to restart it fail in quick succession, autossh
will start delaying its attempts to restart, gradually backing farther and farther off up to
a maximum interval of the autossh poll time (usually 10 minutes). autossh can be "prodded"
to retry by signalling it, perhaps with SIGHUP ("kill -HUP").