2013-07-16 38 views
0

我正在寻找一种使用Ant通过SSH连接输入密码的方法。Ant SSH输入多个密码

从我已阅读,Ant有SSHExec和SSHSession我可以用它来打开ssh连接,但不输入提供一种方式,通过ssh连接运行的命令的密码。

这个过程通常是由手工完成我已经建立了一个ant脚本到目前为止自动化。总体来说什么,我试图做的是:

ssh [email protected] 
Password:password 
someCommand parameter 
Password: [?] 
moreCommands 

通常我会手动输入密码这里,但我不能找到一种方法,通过Ant或猛砸做到这一点。有没有办法与Ant做到这一点?

这是我在很长一段时间的第一篇文章,对不起,如果我不清楚,生病是在网上回应或澄清。

回答

1

我不知道Ant,但既然你问参考猛砸,可能我建议使用期望?

下面的例子是建成一个功能,但展示了如何完成你想要......这是什么假设你运行一个命令(如踢了远程脚本)的SSH命令行上。

exp_comm() 
{ 
    # Remotely execute an SSH command on another server 
    SVR="$1" 
    USR="$2" 
    PSW="$3" 
    TGT="$4" 
    ARG="$5" 

    /usr/bin/expect <<- EOF 1>>stdout.out 2>>stderr.err 
    set timeout 60 
    spawn ssh ${USR}@${SVR} '${TGT}' ${ARG} 
    expect "*assword:" 
    send -- "${PSW}\r" 
    expect eof 
    EOF 
    return $? 
} 

这样称呼它:

exp_comm "server" "userid" "password" "/tmp/testscript.sh" "'One Big Arg1'" 
exp_comm "server" "userid" "password" "/tmp/testscript.sh" "Arg1 Arg2 Arg3" 

您可以修改运行多个命令,并进入更复杂的“智能”的命令,通过改变产卵SSH线移除目标脚本和参数,然后使用期望做更多的事情。例如:

/usr/bin/expect <<- EOF 1>>stdout.out 2>>stderr.err 
    set timeout 60 
    spawn ssh ${USR}@${SVR} 
    expect "*assword:" 
    send -- "${PASS}\r" 
    expect "*>" 
    send -- "command1\r" 
    expect "*>" 
    send -- "command2\r" 
    expect eof 
    EOF 

希望这会有所帮助。