
下面的命令在我的本機上運作良好,並且得到了所需的輸出。見下文
$ ssh -o ConnectTimeout=7 -o BatchMode=yes [email protected] "echo WORKS;exit"
Output:
WORKS
test.sh
現在,我將此命令放在具有足夠執行權限的腳本文件中
$ cat test.sh
segment=$(ssh -o ConnectTimeout=7 -o BatchMode=yes [email protected] "echo WORKS;exit")
echo "SEGMENT $segment"
我沒有得到想要的輸出。事實上我收到以下錯誤:
$ ./test.sh
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
SEGMENT
我正期待著
SEGMENT WORKS
現在,權限被拒絕錯誤消失了,但是
您能建議可能是什麼問題嗎?
答案1
問題是我使用了別名
alias ssh=ssh -i /app/private_key
儲存在使用者設定檔中。但是,此設定不會在 shell 腳本中生效(請參閱這裡例如),所以我必須明確地調用
ssh -i app/private_key
在腳本中。這解決了這個問題。