我是脚本新手,有人可以帮我编写一个期望脚本来ssh到一个设备并提示用户输入密码。我们使用pin + rsa令牌代码作为密码,因此我无法存储密码。
感谢您的帮助
#!/usr/bin/expect -f
spawn ssh device1
答案 0 :(得分:2)
您需要使用interact命令自行输入密码。 骨架下方可以帮助您:
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"
}
}
答案 1 :(得分:0)
我之前使用过此代码来实现您希望实现的目标。您可以将其作为参考点来编写自己的代码版本。
#!/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
答案 2 :(得分:0)
以下代码将要求用户输入密码,并将在要求输入密码的脚本中进一步使用该密码。就像$ pass。
stty -echo
send_user -- "Enter the password: "
expect_user -re "(.*)\n"
send_user "\n"
stty echo
set pass $expect_out(1,string)