我有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"显示脚本进入循环,然后等待另一个"读" param。
我如何解决这个问题?谢谢
---- upd ---- ( @barmar)
set -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
冻结有一些麻烦
SSH_ASKPASS If ssh needs a passphrase, it will read the passphrase from the current terminal if it was run from a terminal. If ssh does not have a terminal associated with it but DISPLAY and SSH_ASKPASS are set, it will execute the program specified by SSH_ASKPASS and open an X11 window to read the passphrase. This is particularly useful when calling ssh from a .xsession or related script. (Note that on some machines it may be necessary to redirect the input from /dev/null to make this work.)
与:
有关(请注意,在某些机器上可能有必要重定向/dev/null的输入以进行此工作。)
这很模糊,请在发出ssh命令
来到这种情况下寻找类似情况:SSH调用包含setsid
调用的远程脚本,并观察SSH永不终止。
ssh remotehost start-daemon.sh
start-daemon.sh
#!/bin/bash
...
setsid $daemon $args &
exit 0
SSH将悬挂,必须给出 ^C才能停止。修复程序(如https://serverfault.com/questions/364689评论中所示)是重定向setSID的stdout:
setsid $daemon $args > /dev/null &
然后SSH按预期停止。在我的测试中,stderr不需要相同的治疗方法。
由于功能请求将注释标记为答案的请求仍然拒绝,因此我在此处复制上述解决方案。
当您执行ssh_askpass = $ 0时,这意味着"为了获取密码,请再次运行此脚本。如果脚本确实在开始时读取通行证,它将再次完成。您需要根据是否已将密码保存在临时文件中为条件。 - Barmar