0

我正在建立一个反向隧道,$ ssh -fN -R 19999:localhost:22 -i aws-mycert.pem ubuntu@my.dyndns.com并且需要确保它即使在服务器重置之后也能保持正常运行。如何在 cron 脚本中检查连接,然后在需要时自动重新建立连接?

4

1 回答 1

2

一个简单的方法是使用 Netcat。该命令nc -z localhost 19999将检查本地端口 19999 中是否有东西在监听,因此您可以nc -z localhost 19999 || ssh -fN -R 19999:localhost:22 -i aws-mycert.pem ubuntu@my.dyndns.com在需要时使用:重新创建隧道。

但是,这仅检查隧道是否已启动,但它可能已过时。最好的解决方案是使用autossh。只需将其安装在您的机器上并使用:

autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -fN -R 19999:localhost:22 -i aws-mycert.pem ubuntu@my.dyndns.com

然后你只需要在服务器启动时运行这个命令,这取决于你的发行版。

您可以在https://www.everythingcli.org/ssh-tunnelling-for-fun-and-profit-autossh/找到有关使用 autossh 的更多详细信息。

于 2018-08-31T13:41:11.553 回答