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 명령은 작동 하지만 두 번째 명령은 작동하지 않습니다. 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

제 질문은 이 문제를 어떻게 정확하게 진단하고 해결할 수 있느냐는 것입니다.

미리 감사드립니다.

관련 정보