可能的重复项:
使用期望将密码传递给 ssh
我想与远程机器建立 ssh 连接,而不是使用 ssh 命令以及机器地址和密码,我只想编写一个小函数,该函数向机器执行 ssh 命令并在服务器询问后输入传递。我可以编写 ssh 部分,但是当主机要求时,我怎样才能使进入的脚本也通过?
您可以使用
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 "yesn"
expect "*assword:" { send "$passwordn"}
}
"*assword:"
{
send "$passwordn"
}
}
您可以使用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 "yesr"}
"password:" {send "$passwordr"}
"*]" {send "exitr"}
"bash" {send "$command"}
}
}
wait
close $spawn_id
只需根据需要解决它