http://wiki.tcl.tk/17378
Elements Significance
expect_out(0,start) Index of the first character of the string that matched the entire expressionexpect_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