CentOS expect example to ssh multiple hosts
#!/usr/bin/expect
set timeout 30
#example of getting arguments passed from command line..
#not necessarily the best practice for passwords though...
set server [lindex $argv 0]
set command " date "
set pass "password"
# connect to server via ssh, login, and su to root
send_user "connecting to $server\n"
spawn ssh -o "StrictHostKeyChecking no" user@$server
#login handles cases:
# login with keys (no user/pass)
# user/pass
# login with keys (first time verification)
#login handles cases:
# login with keys (no user/pass)
# user/pass
# login with keys (first time verification)
expect {
-re ".*Are.*.*yes.*no.*" {
send "yes\n"
exp_continue
#look for the password prompt
}
"*?assword:*" {
send $pass
send "\n"
exp_continue
#The expect command will now return
}
"*$ " {
send "sudo su -\n"
exp_continue
#The expect command will now return
}
"*password for *" {
send $pass
send "\n"
exp_continue
}
"*# " {
send $command
send "\r"
expect "*#"
#interact
}
exp_close
}
send -- "exit\r"
Nhận xét
Đăng nhận xét