2013-07-16 36 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 

希望這會有所幫助。