expect教程

新建文件
#!/data/software/expect/expect-5.43/bin/expect -f
set a [ lindex $argv 0 ]
spawn vi $a
send "i"
send "This is a test"
send \033    # 输入esc
send ":wq\n" #注意回车,send 后面的内容分两行也可以输出回车,缺点是结果带换行符

在test行后面输入INPUT here
#!/data/software/expect/expect-5.43/bin/expect -f
set b [ lindex $argv 0 ]
spawn vi $b
send "/"
send "test\n"
send "o"
send "INPUT here"
send \033
send ":wq\n"

修改密码
#!/data/software/expect/expect-5.43/bin/expect -f
spawn passwd
expect "password:"
send "baoyu\n"
expect "password:"
send "baoyu\n"
expect eof  # 这句不能漏


for/while/foreach 循环
#!/data/software/expect/expect-5.43/bin/expect -f
for {set i 0} {$i < 10} {incr i} {  ## 每个块之间要空格
 switch $i {
  0 {puts "$i is 0"}
  4 {puts "$i is 4"}
  9 {puts "$i is 9"}
 }

}
set i 0
while {$i<10} {## 每个块之间要空格
send_user "$i  "
incr i
}

foreach i {1 2 3 } {send_user "$i "}
puts ""


函数
#!/data/software/expect/expect-5.43/bin/expect -f
set a [lindex $argv 0]
set b [lindex $argv 1]
proc baoyu {begin end} {
 for {set i $begin} {$i < $end} {incr i} {
   puts "$i "
 }
}
baoyu $a $b

数组
#!/data/software/expect/expect-5.43/bin/expect -f
set bao(1) "1"
set bao(2) "a"
set bao(30) "30"
set bao(6)  "test"
set num  [array size bao]   ## 数组大小
set ss [array startsearch bao] ## 取得搜索ID,相当于文件描述符,这里是“搜索描述符”,结果是s-1-bao
for {set i 1} {$i <= $num} {incr i} {
  set idx [array nextelement bao $ss] ## 取得数组下标
  send_user "$idx ->  $bao($idx)\n"
}
send_user "[array anymore bao $ss ]\n" ##确认遍历是否完成,返回0说明遍历已经完成

append用法
#!/data/software/expect/expect-5.43/bin/expect -f
set a "Hello"
set b " World"
append c $a $b " ni hao"
send_user "$c\n"  ## 输出 Hello World ni hao

 

send_user "[string length  $a ]\n" ## 返回$a的长度 
send_user "[string tolower $a ]\n" ## 返回将$a全部小写化的串
send_user "[string toupper $b ]\n" ## 返回将$b全部大写化的串
send_user "[string compare $a $b ]\n" ## 按照字典顺序对字符串进行比较,根据相对关系返回-1,0或者+1。 
send_user "[string first $a $b ]\n" ## 返回$b中第一次出现$a的位置,如果失败,返回-1。
send_user "[string last $a $b ]\n" ## 返回$b中最后一次出现$a的位置,如果失败,返回-1 
send_user "[string trim $a $b ]\n" ## 从$a中删除开头和结尾的出现在$b中的字符 
send_user "[string trimleft  $a $b ]\n" ## 从$a中删除开头的出现在$b中的字符
send_user "[string trimright $a $b ]\n" ## 从$a中删除结尾的出现在$b中的字符

send_user "[expr 1/4]\n" ## 输出 0
send_user "[expr 1/4.0]\n" ## 输出0.25
send_user "[expr 1/4.00]\n" ## 输出0.25

set i 100
send_user "[expr (($i+10)/5*2-10)/2 ]\n"
send_user "[expr 12%10]\n"

正则表达式
while 1 {
expect_user {
-re \\\\ {send_user "You enter \\\n
-re "\\\\" {send_user "You enter \\\n
-re {\\} {send_user "You enter \\\n" }
-re \\+ {send_user "You enter +\n" }
-re "\\+" {send_user "You enter +\n" }
-re {\+} {send_user "You enter +\n" }
-re "\\(" {send_user "You enter \(\n" }
}
}


把参数变量放到数组里去
set i 0
foreach d $argv {
set disk($i) $d
incr i
}
=====================================================================
expect的spawn了一个process, 如果结束了, 它会返回给expect一个eof信号,
expect也可以主动来关闭spawn的process, 这时候,process也会看到一个eof信号,
一般来说, process收到这个信号后, 它会主动关闭它自己

expect_user 从标准输入读取数据. expect_tty和expect_user的区别, expect_user能够被shell所重定向. 
而expect_tty则是永远指从键盘读取输入. send_tty和send_user的 情况类似.

 

Control-c:    send \003

set timeout 5:变量赋值

puts,send_user: 相当于echo ,print等输出命令
eof:
timeout:
[lindex $argv [expr $argc-1]]:[]执行命令,相当于``,$(cmd)
exp_continue
=================================bad====================================
以baoyu用户创建一个文件
#!/data/software/expect/expect-5.43/bin/expect -f
spawn su - baoyu
expect "*\$"
send "touch baoyu.txt\n"
send "echo 'This is a test' > baoyu.txt\n"
interact ## 若加这句则可以

 

expect -re "\[(.*)]:"
if {$expect_out(1,string)!="/bin/tcsh"} { ##这是什么意思
send "/bin/tcsh" }
send " "
expect eof


======================================================================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值