SSH コマンドはコンソールでは機能しますが、bash スクリプト内では機能しません。どのように診断すればよいですか?

SSH コマンドはコンソールでは機能しますが、bash スクリプト内では機能しません。どのように診断すればよいですか?

複数の ssh コマンドを実行する bash スクリプトがあり、問題が発生しました。スクリプトに自動 ssh キー生成を追加する作業を行っていましたが、コンソールから手動で入力すると ssh コマンドが機能するのに、スクリプト内では機能しないという奇妙なエラーが発生しています。

コマンドの一連の流れは以下のとおりです。(キー生成 > リモートスクリプトの確認 > リモートスクリプトの実行)

mkdir -p ~/.ssh
echo "StrictHostKeyChecking no" > ~/.ssh/config
ssh-keygen -q -t rsa -N '' <<< ""$'\n'"y" 2>&1 >/dev/null
sshpass -f password.txt ssh-copy-id [email protected]
ssh [email protected] "test -e /home/user/script.sh"
ssh [email protected] "echo password | sudo -S /home/user/script.sh > log.txt"

これはコンソールでは問題なく動作しますが、突然スクリプトでは動作しなくなります。唯一の違いは、スクリプトにはログにデータを追加するためのエコーがいくつかあることですが、これは ssh コマンドには影響しません。

最初の ssh コマンドは機能しますが、2 番目のコマンドは機能しません。通常は、6 回接続拒否を受け取り、終了コード 255 を返します。以下は、-v フラグを使用して実行した場合のサンプル ログです。ssh [email protected] "test -e /home/user/script.sh"ssh [email protected] "echo password | sudo -S /home/user/script.sh > log.txt"

OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /root/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to x.x.x.x [x.x.x.x] port 22.
debug1: connect to address x.x.x.x port 22: Connection refused
ssh: connect to host x.x.x.x port 22: Connection refused
OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /root/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to x.x.x.x [x.x.x.x] port 22.
debug1: connect to address x.x.x.x port 22: Connection refused
ssh: connect to host x.x.x.x port 22: Connection refused
OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /root/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to x.x.x.x [x.x.x.x] port 22.
debug1: connect to address x.x.x.x port 22: Connection refused
ssh: connect to host x.x.x.x port 22: Connection refused
OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /root/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to x.x.x.x [x.x.x.x] port 22.
debug1: connect to address x.x.x.x port 22: Connection refused
ssh: connect to host x.x.x.x port 22: Connection refused
OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /root/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to x.x.x.x [x.x.x.x] port 22.
debug1: connect to address x.x.x.x port 22: Connection refused
ssh: connect to host x.x.x.x port 22: Connection refused
OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /root/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to x.x.x.x [x.x.x.x] port 22.
debug1: connect to address x.x.x.x port 22: Connection refused
ssh: connect to host x.x.x.x port 22: Connection refused

そして、実行されたときの PS AUX。

ps aux | grep ssh
root      1287  0.0  0.3  65504  6500 ?        Ss   Sep18   0:02 /usr/sbin/sshd -D
root      4409  0.0  0.0  14220   888 pts/0    S+   11:14   0:00 grep --color=auto ssh
root     20680  0.0  0.3  94860  6900 ?        Ss   09:09   0:00 sshd: user [priv]
user    20760  0.0  0.1  94860  3540 ?        S    09:09   0:00 sshd: user@notty
user    20761  0.0  0.1  12876  2000 ?        Ss   09:09   0:00 /usr/lib/openssh/sftp-server
root     21024  0.0  0.3  94860  7052 ?        Ss   08:13   0:00 sshd: user [priv]
user    21133  0.0  0.2  94860  4636 ?        R    08:13   0:04 sshd: user@pts/0

そこで質問なのですが、この問題を正確に診断して修正するにはどうすればいいのでしょうか?

よろしくお願いします。

関連情報