SSHトンネルが開いていることを確認するスクリプトを作成します。

SSHトンネルが開いていることを確認するスクリプトを作成します。

私のSSHトンネル設定は次のとおりです。

ssh -N -L 1105:remote.server:25 office-machine&

たとえば、1分ごとに1回(クローン操作を使用して実行することもできます)、確認を設定するスクリプトを作成したいと思います。このスクリプトはこの接続が確立されていることを確認し、そうでない場合は接続を確立します(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 + 1の両方を使用できるように、いくつかのポートNを選択し、次の-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").

おすすめ記事