#!/usr/bin/expect
##通过索引的方式获取所传的参数
set ip [ lindex $argv 0 ]
set password [ lindex $argv 1 ]
##开启一个进程
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r"; }
"password" { send "$password\n" }
}
#持久连接远程服务器
interact
填好对应内容后,需要注意的是,需要将脚本增加可执行权限
对于首次登录的机器:
#!/usr/bin/expect
set timeout 20
set passwd "****"
set passwderror 0
spawn ssh xxx@***.***.***.***
expect {
"*assword:*" {
if { $passwderror == 1 } {
puts "passwd is error"
exit 2
}
set timeout 1000
set passwderror 1
send "$passwd\r"
interact
}
"*es/no)?*" {
send "yes\r"
exp_continue
}
timeout {
puts "connect is timeout"
exit 3
}
}
interact