expect实现ssh的自动登录服务器脚本。

使用示例 :

>./login.exp

1

--------------------------------------------------------------------

login.exp代码如下

#! /usr/bin/expect -f
set timeout 30
expect_user -re "(.*)\n"
set hostIP $expect_out(1,string)
switch $hostIP {
  1 {set hostLongIP 172.168.1.101; set user user1}
  2 {set hostLongIP 172.168.1.102; set user user2}
  3 {set hostLongIP 172.168.1.103; set user user3}
  default {puts "ERROR!!";exit}
}
send_user "ooooooooooooooooo $hostLongIP ooooooooooooooooooo\n"
spawn ssh -l $user $hostLongIP
expect "password:"
send "password\n"
interact


另一个版本

使用示例:

>./login.exp 1

#! /usr/bin/expect -f
set timeout 30
set hostIP [lindex $argv 0]
switch $hostIP {
  1 {set hostLongIP 172.168.1.101; set user user1}
  2 {set hostLongIP 172.168.1.102; set user user2}
  3 {set hostLongIP 172.168.1.103; set user user3}
  default {puts "ERROR!!";exit}
}
send_user "ooooooooooooooooo $hostLongIP ooooooooooooooooooo\n"
spawn ssh -l $user $hostLongIP
expect "password:"
send "password\n"
interact


expect学习资料

http://wiki.tcl.tk/expect

http://linux.die.net/man/1/expect