转自:http://hi.baidu.com/yk103/blog/item/23cf312acbfa9f395243c123.html(待验)
expcet是一个可以根据预先编好的脚本来实现与另外一个应用程序的交互。
而autoexpect则相当于一个录制脚本的程序(有点像winword里的宏),
能够记录你输入的命令,下次只要运行该脚本就能够“昨日重现“,很方便!
比如录制xrgsu_cracked()
最首先有必要了解的一句的含义:
autoexpect -p -f xrgsu.exp xrgsu_cracked -p选项使autoexpect只找寻最后的输出,
这个选项对于有大量输出特别有用(你肯定不希望这个脚本上mb吧)
-f选项定制录制脚本的文件名为
"xrgsu.exp"程序执行全过程:
autoexpect -p -f xrgsu.exp xrgsu_cracked
autoexpect started, file is xrgsu.exp
xrgsupplicant 1.1.1
ruijie network 凯发app官方网站 copyright 2004-2005
please input your user name:
myusernameplease input your password:
输入了密码,看不见的use dhcp,1-use,0-unuse(default: 0):
you have 2 nic:
0. eth0 desc: (null)
1. eth1 desc: (null)
please select which nic will be used(0-1,default:0)
use default auth parameter,0-use 1-unuse(default: 0):
searching server...
connecting server...
authenticating...
authenticate successfully!
please input 'unauth' to logoff:unauth
xrgsu exit!
autoexpect done, file is xrgsu.exp
这里连上之后就马上输入了
unauth退出了xrgsu_cracked,只有autoexpect才会执行文毕。
得到的脚本如下:
#!/usr/bin/expect -f
#
# this expect script was generated by autoexpect on tue may 8 18:04:56 2007
# expect and autoexpect were both written by don libes, nist.
#
# note that autoexpect does not guarantee a working script. it
# necessarily has to guess about certain things. two reasons a script
# might fail are:
#
# 1) timing - a surprising number of programs (rn, ksh, zsh, telnet,
# etc.) and devices discard or ignore keystrokes that arrive "too
# quickly" after prompts. if you find your new script hanging up at
# one spot, try adding a short sleep just before the previous send.
# setting "force_conservative" to 1 (see below) makes expect do this
# automatically - pausing briefly before sending each character. this
# pacifies every program i know of. the -c flag makes the script do
# this in the first place. the -c flag allows you to define a
# character to toggle this mode off and on.
set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}
#
# 2) differing output - some programs produce different output each time
# they run. the "date" command is an obvious example. another is
# ftp, if it produces throughput statistics at the end of a file
# transfer. if this causes a problem, delete these patterns or replace
# them with wildcards. an alternative is to use the -p flag (for
# "prompt") which makes expect only look for the last line of output
# (i.e., the prompt). the -p flag allows you to define a character to
# toggle this mode off and on.
#
# read the man page for more info.
#
# -don
set timeout -1
spawn xrgsu_cracked
match_max 100000
expect -exact "please input your user name:"
send -- "myusername\r"
expect -exact "please input your password:"
send -- "mypassword\r"
expect -exact "use dhcp,1-use,0-unuse(default: 0):"
send -- "\r"
expect -exact "please select which nic will be used(0-1,default:0)"
send -- "\r"
expect -exact "use default auth parameter,0-use 1-unuse(default: 0):"
send -- "\r"
expect -exact "please input 'unauth' to logoff:"
send -- "unauth\r"
expect eof
记得要把倒数第二句
send -- "unauth\r" 删了(除非你想连上网就断开)
ok,终于录制完了,那怎么执行呢,千万别用sh xrgsu.exp偶就犯了这个错误,
正确的执行命令是./xrgsu.exp~~想想为什么吧