我有用于 ssh 登录的脚本,而不是期望:
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 "$@"
setsid "$@"
rm "$TMP_PWD"
}
如果我使用如下命令:
echo "my_password" | do_auth_connect ssh use@domain "uname -a"
一切正常。但如果我使用:
read password
echo "$password" | do_auth_connect ssh use@domain "uname -a"
脚本冻结。
“set -x”表示脚本进入循环,并等待另一个“读取”参数。
我该如何解决这个问题?谢谢
---UPD---(对于@Barmar)
正常时设置 -x 输出(以 scp 为例):
+ echo 'user_password'
+ do_auth_connect scp -P 444 user@domain:/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 user@domain:/home/user/dbg.log /home/user/dbg.log
+ echo 'user_password'
+ do_auth_connect scp -P 444 user@domain:/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 user@domain:/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 user@domain:/home/user/dbg.log /home/user/dbg.log
+ read -p '> ' pass
这就是全部,^C 不起作用:)
---UPD2---
我发现任何“读取”都会破坏脚本例如:
read null
echo "user_password" | do_auth_connect scp -P 444 user@domain:/home/user/dbg.log ~/dbg.log
有一些冻结问题