我有ssh登錄腳本,而不期望:慶典setsid&讀凍結
function do_auth_connect(){
if [ -n "$SSH_ASKPASS_TMPFILE" ]; then
cat "$SSH_ASKPASS_TMPFILE"
exit 0
elif [ $# -lt 1 ]; then
echo "Usage: echo password | $0 <ssh command line options>" >&2
exit 1
fi
sighandler() {
rm "$TMP_PWD"
}
TMP_PWD=$(mktemp)
chmod 600 "$TMP_PWD"
trap 'sighandler' SIGHUP SIGINT SIGQUIT SIGABRT SIGKILL SIGALRM SIGTERM
export SSH_ASKPASS=$0
export SSH_ASKPASS_TMPFILE=$TMP_PWD
[ "$DISPLAY" ] || export DISPLAY=dummydisplay:0
read password
echo $password >> "$TMP_PWD"
# use setsid to detach from tty
#exec setsid "[email protected]"
setsid "[email protected]"
rm "$TMP_PWD"
}
如果我使用像命令:
echo "my_password" | do_auth_connect ssh [email protected] "uname -a"
一切工作的罰款。但如果我使用:
read password
echo "$password" | do_auth_connect ssh [email protected] "uname -a"
腳本凍結。
「set -x」表示腳本進入循環,並等待另一個「讀取」參數。
我該如何解決這個問題?感謝
--- --- UPD(對於@Barmar)
設置-x輸出時確定(與SCP例如):
+ echo 'user_password'
+ do_auth_connect scp -P 444 [email protected]:/home/user/dbg.log /home/user/dbg.log
+ '[' -n '' ']'
+ '[' 5 -lt 1 ']'
++ mktemp
+ TMP_PWD=/tmp/tmp.X8TKTIchq6
+ chmod 600 /tmp/tmp.X8TKTIchq6
+ trap sighandler SIGHUP SIGINT SIGQUIT SIGABRT SIGKILL SIGALRM SIGTERM
+ export SSH_ASKPASS=./1.sh
+ SSH_ASKPASS=./1.sh
+ export SSH_ASKPASS_TMPFILE=/tmp/tmp.X8TKTIchq6
+ SSH_ASKPASS_TMPFILE=/tmp/tmp.X8TKTIchq6
+ '[' :0.0 ']'
+ read password
+ echo 'user_password'
+ setsid scp -P 444 [email protected]:/home/user/dbg.log /home/user/dbg.log
+ echo 'user_password'
+ do_auth_connect scp -P 444 [email protected]:/home/user/dbg.log /home/user/dbg.log
+ '[' -n /tmp/tmp.X8TKTIchq6 ']'
+ cat /tmp/tmp.X8TKTIchq6
+ exit 0
+ rm /tmp/tmp.X8TKTIchq6
+ exit
並且當失敗:
+ read pass
> user_password
+ echo 'user_password'
+ do_auth_connect scp -P 444 [email protected]:/home/user/dbg.log /home/user/dbg.log
+ '[' -n '' ']'
+ '[' 5 -lt 1 ']'
++ mktemp
+ TMP_PWD=/tmp/tmp.7usdmtHgqt
+ chmod 600 /tmp/tmp.7usdmtHgqt
+ trap sighandler SIGHUP SIGINT SIGQUIT SIGABRT SIGKILL SIGALRM SIGTERM
+ export SSH_ASKPASS=./1.sh
+ SSH_ASKPASS=./1.sh
+ export SSH_ASKPASS_TMPFILE=/tmp/tmp.7usdmtHgqt
+ SSH_ASKPASS_TMPFILE=/tmp/tmp.7usdmtHgqt
+ '[' :0.0 ']'
+ read password
+ echo 'user_password'
+ setsid scp -P 444 [email protected]:/home/user/dbg.log /home/user/dbg.log
+ read -p '> ' pass
這就是全部,^ C不起作用:)
--- UPD2 ---
我發現任何 「讀」 打破了腳本 例如:
read null
echo "user_password" | do_auth_connect scp -P 444 [email protected]:/home/user/dbg.log ~/dbg.log
有一些麻煩與凍結
我想不出任何東西 - 你可以將'set -x'的輸出添加到問題中嗎? – Barmar
爲什麼不使用公鑰認證和鑰匙串應用而不是手動輸入密碼? – Barmar
@Barmar done,thx –