首页 > 代码库 > expect 命令脚本语言实战
expect 命令脚本语言实战
一.expect 实现自动填入密码
1.vi ssh.exp.201
#!/usr/bin/expect
spawn ssh -p 20000 oldboy@192.168.1.201 /sbin/ifconfig eth0
set timeout 60
expect "*password:"
send "abcdef\n"
expect eof
exit
2.chmod 700 ssh.exp.201 #注。要是没有权限执行,也可以直接expect,700权限。防止其它用户查看脚本中的密码
3.执行效果:
./ssh.exp.201
spawn ssh -p 20000 oldboy@192.168.1.201 /sbin/ifconfig eth0
oldboy@192.168.1.201‘s password:
eth0 Link encap:Ethernet HWaddr 00:0C:29:31:2B:A0
inet addr:192.168.1.201 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2417 errors:0 dropped:0 overruns:0 frame:0
TX packets:2419 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:229270 (223.8 KiB) TX bytes:186485 (182.1 KiB)
二.expect语法:
exp_continue 的原理,当要提示输入时,进行匹配,当匹配成功,重新进入expect,有点象for 循环中的continue,要是没有exp_continue的话,则expect中的内容,只会对一次输入匹配一次,不论你有几个关键字
论证:
#!/usr/bin/expect
spawn ssh -p 20000 oldboy@192.168.1.201 /sbin/ifconfig eth0
set timeout 60
expect {
-timeout 60
"yes/no" {exp_send "yes\r"; exp_continue}
"*password:" {exp_send "abcdef\r"}
timeout {puts "expect was time out "; return}
}
expect eof
exit
本文出自 “蜘蛛侠” 博客,请务必保留此出处http://txidc.blog.51cto.com/9138217/1562807
expect 命令脚本语言实战