- 论坛徽章:
- 0
|
以下是一个expect脚本,用于测试变量$expect_out(0,string)和$expect_out(1,string)
但是却遇到了一个问题,发现expect的内容并不是从最近的上一次的反馈结果取得,而是包括以前的反馈结果:
#!/usr/bin/expect
spawn /usr/bin/telnet 10.123.123.221
expect "login:"
send "gmcc\r"
expect "word:"
send "abcdef\r"
expect "@"
send "echo \"abc 89 def\" \r "
expect -re "abc (\[0-9\]\[0-9\]) def"
send "echo \"expect_out(0,string) in \"89\" is : $expect_out(0,string)\" \r" <---如果存在此句,则下面得不到期待的结果
sleep 1
send "echo \"expect_out(1,string) in \"89\" is : $expect_out(1,string)\" \r"
#==========================================#
expect "@"
send "echo \"abc 96 def\" \r "
expect -re "abc (\[0-9\]\[0-9\]) def"
send "echo \"expect_out(0,string) in \"96\" is : $expect_out(0,string)\" \r"
sleep 1
send "echo \"expect_out(1,string) in \"96\" is : $expect_out(1,string)\" \r"
interact
输出如下:
...
...
[gmcc@gmcc]$ echo "abc 89 def"
abc 89 def
[gmcc@gmcc]$ echo "expect_out(0,string) in "89" is : abc 89 def"
expect_out(0,string) in "89" is : abc 89 def <---不知是不是由于此行的输出abc 89 def满足expect条件,影响了后面的expect
[gmcc@gmcc]$ echo "expect_out(1,string) in "89" is : 89"
expect_out(1,string) in "89" is : 89
[gmcc@gmcc]$ echo "abc 96 def"
abc 96 def
[gmcc@gmcc]$ echo "expect_out(0,string) in "96" is : abc 89 def"<---期望得到的是abc 96 def
expect_out(0,string) in "96" is : abc 89 def
[gmcc@gmcc]$ echo "expect_out(1,string) in "96" is : 89"<---期望得到的是96
expect_out(1,string) in "96" is : 89
但是如果把上面的脚本中语句:
send "echo \"expect_out(0,string) in \"89\" is : $expect_out(0,string)\" \r"
注释掉,脚本运行则得到想要的结果:
...
...
[gmcc@gmcc]$ echo "abc 89 def"
abc 89 def
[gmcc@gmcc]$ echo "expect_out(1,string) in "89" is : 89"
expect_out(1,string) in "89" is : 89
[gmcc@gmcc]$ echo "abc 96 def"
abc 96 def
[gmcc@gmcc]$ echo "expect_out(0,string) in "96" is : abc 96 def" <---得到期望值
expect_out(0,string) in "96" is : abc 96 def
[gmcc@gmcc]$ echo "expect_out(1,string) in "96" is : 96" <---得到期望值
expect_out(1,string) in "96" is : 96
expect为什么会把以前的反馈结果都作为其输入呢,如何才能做到以最近上次的反馈结果作为其输入?
就是说,在最初的脚本中,既然最近的反馈结果abc 96 def 满足expect条件,那么应该
expect_out(0,string)应该为abc 96 def,而不是上次的abc 89 def
但是单独敲了expect指令进入expect1.1>之后,好像又不存在这种情况
请各位不吝赐教,谢谢啦! |
|