2

可能重复:
使用期望将密码传递给 ssh

我想与远程机器建立 ssh 连接,而不是使用 ssh 命令以及机器地址和密码,我只想编写一个小函数,对机器执行 ssh 命令并在服务器询问后输入密码。我可以编写 ssh 部分,但是当主机要求它时,我怎样才能制作进入通行证的脚本?

4

2 回答 2

4

您可以使用expect脚本。您可以从 cmd 行传递参数。我写的示例代码:

#!/usr/bin/expect

set timeout 100
set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set command [lindex $argv 3]

spawn ssh $username@$host $command
#puts $command
expect {
    "(yes/no)?"
        {
            send "yes\n"
            expect "*assword:" { send "$password\n"}
        }
    "*assword:"
        {
            send "$password\n"
        }
    }
于 2012-12-10T13:09:01.120 回答
1

您可以使用 它正是您需要的:Expect tool

编辑

#!/usr/bin/expect
set timeout 60
set user "yourName"
set machine "nameOfYourMachine"
set password "yourPassword"
set command "command that you want execute via ssh"
spawn ssh $user@$machine 
while {1} {
expect {
      eof                          {break}
      "The authenticity of host"   {send "yes\r"}
      "password:"                  {send "$password\r"}
      "*\]"                        {send "exit\r"}
      "bash"                        {send "$command"}
}
}
wait
close $spawn_id

只需根据需要解决它

于 2012-12-10T13:06:34.587 回答