我有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)
ok时设置-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
有一些冻结的麻烦
答案 0 :(得分:0)
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命令之前将输入重定向到/ dev / null
答案 1 :(得分:0)
找到类似的情况: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的标准输出:
setsid $daemon $args > /dev/null &
然后ssh按预期停止。在我的测试中,stderr不需要相同的治疗。
答案 2 :(得分:-1)
由于对mark a comment as an answer的功能请求仍然被拒绝,我在此处复制上述解决方案。
当您执行SSH_ASKPASS = $ 0时,它表示&#34;为了获取密码,请再次运行此脚本&#34;。如果脚本在开头读取了传递,它将再次执行。您需要以密码是否已保存在临时文件中为条件。 - 巴马尔