【发布时间】:2014-02-20 00:41:44
【问题描述】:
我是脚本新手。如何编写 Expect 脚本以 ssh 进入设备并提示用户输入密码?我们使用 pin + RSA 令牌代码作为密码,所以我无法存储密码。
#!/usr/bin/expect -f
spawn ssh device1
【问题讨论】:
我是脚本新手。如何编写 Expect 脚本以 ssh 进入设备并提示用户输入密码?我们使用 pin + RSA 令牌代码作为密码,所以我无法存储密码。
#!/usr/bin/expect -f
spawn ssh device1
【问题讨论】:
我以前使用过此代码来实现您希望实现的目标。您可以将其作为参考点来编写您自己的代码版本。
#!/usr/bin/env expect -f
set passw [lindex $argv 0]
#timeout is a predefined variable in expect which by default is set to 10 sec
set timeout 60
spawn ssh $user@machine
while {1} {
expect {
eof {break}
"The authenticity of host" {send "yes\r"}
"password:" {send "$password\r"}
"*\]" {send "exit\r"}
}
}
wait
#spawn_id is another default variable in expect.
#It is good practice to close spawn_id handle created by spawn command
close $spawn_id
来源:Expect Wiki
【讨论】:
您需要使用交互命令自己输入密码。
下面的骨架应该会让你上路:
set prompt "$"
spawn ssh user@host
set timeout 10
expect {
timeout {
puts "Unable to connect"
exit 1
}
#Authenticty Check
"*(yes/no)?" {
send "yes\r"
exp_continue
}
"assword: " {
#Hand control back to user
interact -o "\r" exp_continue
}
"$prompt" {
puts "Cool by the pool"
}
}
【讨论】:
下面的代码将要求用户输入密码,并将在需要密码的脚本中进一步使用它。就像 $pass。
stty -echo
send_user -- "Enter the password: "
expect_user -re "(.*)\n"
send_user "\n"
stty echo
set pass $expect_out(1,string)
【讨论】: