マシンAを介してマシンBに接続したいとします。私が学んだように、これを実現する方法は複数あります。たとえば、
ssh -T -J user@machineA user@machineB << END_OF_SSH_CONNECTION
# Some commands in machine B
END_OF_SSH_CONNECTION
または
ssh -T user@machineA << END_OF_MACHINE_A
ssh -T machine B << END_OF_MACHINE_B
# Some commands in machine B
END_OF_MACHINE_B
END_OF_MACHINE_A
ただし、ユーザーがマシンBに接続しているときにコマンドを入力できるインタラクティブシェルを開始する方法は私にはわかりません。たとえば、これは機能します。
ssh user@machineA -t 'bash -l -c "bash"'
そしてこれも同様に機能します:
ssh -J user@machineA user@machineB -t 'bash -l -c "bash"'
ただし、以下は機能しません。
ssh -J user@machineA user@machineB -t 'sudo apt-get update; bash -l -c "bash"'
E: Command line option 'l' [from -l] is not understood in combination with the other options.
Connection to machineB closed.
そして、次のどちらでもありません。
ssh -J user@machineA user@machineB -t 'if [ 1 -eq 1 ]; then bash -l -c "bash"; fi'
/usr/local/sbin/start_ttylog.sh: line 67: exec: if: not found
Connection to machineB closed.
私もこれを試しました:
ssh -T -J user@machineA user@machineB << EOF
# Some commands on machineB
# Check whether apache2 has been correctly installed
if [ $(dpkg-query -W -f='${Status}' apache2 2>/dev/null | grep -c "ok installed") -eq 1 ];
then
printf "done\n";
else
printf "fail\nStarting an interactive shell: enter exit to quit\n";
bash -l -c "bash";
fi
EOF
この最後のバージョンはほぼ機能します(つまり、エラーは返されません)が、ユーザーが期待どおりにコマンドを入力できる対話型シェルは起動しません。
誰かが私を助けてくれることを願っています。前もって感謝します。
助けてくれてありがとう、@ attie。提案されたコードを少し変更しました。これを以下に報告します
#!/usr/bin/env bash ssh -T -J user@machineA user@machineB 'cat >/tmp/myscript' <<"EOF" if dpkg-query -W -f='${Status}' apache2 2>/dev/null | grep -q 'ok installed'; then echo "done"; else echo "fail, starting an interactive shell"; bash -l -c "bash"; fi EOF ssh -t -J user@machineA user@machineB "bash '/tmp/myscript'"
これに、外部ファイルに書き込む必要さえありません。
#!/usr/bin/env bash
ssh -T -J user@machineA user@machineB << "EOF"
if dpkg-query -W -f='${Status}' apache2 2>/dev/null | grep -q "ok installed"; then
echo "done";
else
echo "fail, starting an interactive shell";
bash -l -c "bash";
fi
EOF
私は基本的に、EOF
フィールドを拡張する試みを回避するために使用される、周りの引用符を見逃していました${Status}
。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加