大家都说好记性不如烂blog

博客园 首页 新随笔 联系 订阅 管理

若在Linux下用ET实现基本的SU的功能,也可以用expect实现哈,多谢小鱼提醒。。。

转一篇expect相关的帖子,实践后再贴。

expect是一个可以根据预先编好的脚本来实现与另外一个应用程序的交互。
而autoexpect则相当于一个录制脚本的程序(有点像Winword里的宏),
能够记录你输入的命令,下次只要运行该脚本就能够“昨日重现“,很方便!
比如录制xrgsu_cracked(xrgsu的破解版,参见这里


最首先有必要了解的一句的含义:
autoexpect -p -f xrgsu.exp 

-p选项使autoexpect只找寻最后的输出,
这个选项对于有大量输出特别有用(你肯定不希望这个脚本上Mb吧)
-f选项定制录制脚本的文件名为"xrgsu.exp"程序执行全过程:


 <dracula>: autoexpect 会记录之后的所有操作,直到用"exit"退出程序。


autoexpect -p -f xrgsu.exp xrgsu_cracked 
autoexpect started, file is xrgsu.exp
XRGSupplicant 1.1.1
Ruijie Network CopyRight 2004-2005
Please input your user name:myusername
Please 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~~想想为什么吧

posted on 2010-12-15 10:03  leqiangw  阅读(523)  评论(0编辑  收藏  举报