SSH はプロンプトを待って終了します。

SSH はプロンプトを待って終了します。

私が達成したいのは、ジャンプサーバーの背後にある2つのサーバーでSSH接続をテストすることです。要件は、実際にシェルをロード(プロンプト)してからサーバーをシャットダウン(接続)することです。最初は次のことを試しました。

ssh -A -t [email protected] \
"command1; ssh user1@server1 exit; ssh user2@server2 exit"

プロンプト(シェル)をロードする前に、SSH接続を接続してすぐに終了します。解決策が見つかりました。

ssh -A -t [email protected] \
"command1; ssh user1@server1 <<EOF
EOF
ssh user2@server2 <<EOF
EOF
"

EOFを使用すると、実際にはいくつかのコマンドが必要なのでプロンプトを待ち、プロンプトがないので終了します。

私の目標を達成するためのより良い/より良い方法はありますか?

ベストアンサー1

という代替手段があります.ssh/config

.ssh/config というファイルを作成し、次のように編集します。

Host Server-Jump-*
    User username
    IdentityFile ~/.ssh/id_rsa # -- if you want to provide key
    ForwardAgent yes
    ServerAliveInterval 60
    ServerAliveCountMax 12

Host Server1 
    Hostname 21.19.6.19
    Port 2222

Host Server2 
    Hostname Server2-IP
    Port 22
    ProxyCommand ssh -W %h:%p Server1 

このファイルを保存してください。

それでは、次のコマンドを試してください。

ssh Server2

プロキシコマンド部分の説明

-W host:port
             Requests that standard input and output on the client be forwarded to host on port over the secure channel.  Implies -N, -T, ExitOnForwardFailure and ClearAllForwardings.  Works
             with Protocol version 2 only.

`%h` Host name to connect 
`%p` Port name to connect 

おすすめ記事