3

我知道 RSA 身份验证,但出于我的目的,我想使用 heredoc 来指定密码。我想要类似下面的东西,但我无法让它工作。这甚至可能吗?

#!/bin/bash
echo -n "Enter Password: "
read -s password
ssh myhost << EOL
$password
echo "I'm logged onto myhost"
EOL
echo done

这是我尝试时得到的:

$ ./testssh 
Enter Password: 
Pseudo-terminal will not be allocated because stdin is not a terminal.
user@myhost's password: 
Warning: No xauth data; using fake authentication data for X11 forwarding.
Warning: no access to tty (Bad file descriptor).
Thus no job control in this shell.
mypassword: Command not found.
I'm logged onto myhost
done

编辑:

根据 bmargulies 的回答,我重新编写了我的脚本并提出了以下内容:

#!/bin/bash
echo -n "Enter the Host: "
read HOST
echo -n "Enter Username: "
read USER
echo -n "Enter Password: "
read -s PASS
VAR=$(expect -c "
spawn ssh $USER@$HOST
expect \"password:\"
send \"$PASS\r\"
expect \">\"
send \"ls\r\"
send \"echo 'I\'m on $HOST'\r\"
expect -re \"stuff\"
send \"logout\"
")
echo -e "\n\n\n========"
echo VAR = "$VAR"
echo done
4

2 回答 2

4

读取密码的程序通常专门打开 /dev/tty 来阻止重定向。在这种情况下,您需要的工具是“expect”,它将在伪 tty 后面运行一个。

于 2010-09-26T02:21:47.103 回答
0

如果你在 w/perl 中混合,你可以做一些“干净”的事情(从不需要引用的视图),如下所示:

#!/bin/bash
cmd="ssh myhost << EOL"
echo -n "Enter Password: "
read -s password
# answer password prompt
#   note we use ctl-A as our quote delimeter around the password so we run
#   no risk of it escaping quotes
script='
use Expect;
use ysecure;
my $exp = new Expect;
$exp->raw_pty(1);
$exp->spawn(q|<CMD>|);
$exp->expect(30,">");
$exp->send(q^A<PASSWORD>^A . "\n");
$exp->soft_close();
$exp->exitstatus() && die;
'

script=${script//<CMD>/$cmd}
script=${script//<PASSWORD>/$password}

perl -e "$script"
于 2011-01-11T18:53:05.487 回答