expect_out

http://wiki.tcl.tk/17378


Elements    Significance

expect_out(0,start)     Index of the first character of the string that matched the entire expression
expect_out(0,end)     Index of the last character of the string that matched the entire expression
expect_out(0,string)    String that matched the entire expression
expect_out(1..9,start)    Index of the first character of the string that matched the pattern enclosed in the 1st - 9th set of parentheses
expect_out(1..9,end)    Index of the last character of the string that matched the pattern enclosed in the 1st - 9th set of parentheses
expect_out(1..9,string)    String that matched the pattern enclosed in the 1st - 9th set of parentheses
expect_out(buffer)    Entire contents of the buffer when a match was found
expect_out(spawn_id)    Spawn id of the process which produced the matching pattern


几次脚本就自动停止在那边。看看脚本没有出错。怎么就已经找到匹配的地方了?

后来看了下 从expect 打印的log来看,应该是expect_out(buffer)在捣乱, 每次在匹配下个string的时候,buffer并不会清空,可能是使用到previous 的数据来匹配。那么最好是找到一个办法能够在做关键匹配的时候,先清空一下buffer,避开前面的数据来捣乱。 


Also, note that $expect_out(buffer) doesn't really hold what people want; it typically needs to be filtered down at least to eliminate the prompt.


但是看其他blog,使用 expect * 来返回 空,来取代 buffer,

在expect 脚本里加上 expect -re ”\*“ , 匹配空。


My experience has been this is hardly ever useful - it means "match on anything - including nothing" and usually returns nothing. But it does it very quickly. RJ


expect version 5.38.0
argv[0] = /usr/bin/expect  argv[1] = -d  argv[2] = ./1.sh  
set argc 0
set argv0 "./1.sh"
set argv ""
executing commands from command file ./1.sh
spawn ssh root@10.250.168.40
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {7914}

expect: does "" (spawn_id exp4) match glob pattern "password"? no
root@10.250.168.40's password:
expect: does "root@10.250.168.40's password: " (spawn_id exp4) match glob pattern "password"? yes
expect: set expect_out(0,string) "password"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "root@10.250.168.40's password"
send: sending "root\r" to { exp4 }

expect: does ": " (spawn_id exp4) match glob pattern "~#"? no


expect: does ": \r\n" (spawn_id exp4) match glob pattern "~#"? no
Last login: Sun Apr  1 06:29:07 2012 from 10.250.168.89
Linux xxx 2.6.32.39.TEK.V7.11.3.1643b5 #1 SMP PREEMPT Tue Oct 4 13:59:19 CDT 2011 i686 GNU/Linux

expect: does ": \r\nLast login: Sun Apr  1 06:29:07 2012 from 10.250.168.89\r\r\nLinux xxxxx 2.6.32.39.TEK.V7.11.3.1643b5 #1 SMP PREEMPT Tue Oct 4 13:59:19 CDT 2011 i686 GNU/Linux\r\n" (spawn_id exp4) match glob pattern "~#"? no
root@xxx:~#
expect: does ": \r\nLast login: Sun Apr  1 06:29:07 2012 from 10.250.168.89\r\r\nLinux xxx 2.6.32.39.TEK.V7.11.3.1643b5 #1 SMP PREEMPT Tue Oct 4 13:59:19 CDT 2011 i686 GNU/Linux\r\nroot@xxx:~# " (spawn_id exp4) match glob pattern "~#"? yes



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值