Tcl expect-expect\u out比较

Tcl expect-expect\u out比较,tcl,expect,Tcl,Expect,我有来自远程设备的输入,我想捕获并基于它执行一些命令-脚本工作正常!但是有一个问题-expect在每一行中循环(exp_continue),当find REGEX匹配时,它会根据匹配执行一些命令($expect_out)。。很好,但实际上是基于每一行(我希望以某种方式将$expect_out(n,string)与其他先前和将来的匹配进行比较,并基于唯一匹配只执行一个命令 举个例子可能更简单: 输入包含:“1/1/1”和“1/1/2”重复字符串,但在不同的行上,我捕获它们并基于它们执行重复命令:(

我有来自远程设备的输入,我想捕获并基于它执行一些命令-脚本工作正常!但是有一个问题-expect在每一行中循环(exp_continue),当find REGEX匹配时,它会根据匹配执行一些命令($expect_out)。。很好,但实际上是基于每一行(我希望以某种方式将$expect_out(n,string)与其他先前和将来的匹配进行比较,并基于唯一匹配只执行一个命令

举个例子可能更简单:

输入包含:“1/1/1”和“1/1/2”重复字符串,但在不同的行上,我捕获它们并基于它们执行重复命令:(

输入:

VL.  Interface
-----------------------
10   vlanIf:1/1/1:10
20   vlanIf:1/1/3:20
30   vlanIf:1/1/2:30
40   vlanIf:1/1/4:40
50   vlanIf:1/1/2:50
60   vlanIf:1/1/1:60
70   vlanIf:1/1/1:70
脚本:

  #!/usr/bin/expect -f
  telnet ...
  ---snipped--
  set prompt "#"

# cmd1
  expect "$prompt" { send "show interface vlan\r" ; set is_ok "cmd1" }
    if { $is_ok != "cmd1" } { send_user "\n## #----- 9 Exit on executing command3\n" ; exit }

# cmd2 ... n
  expect -re "(vlanIf:)(\\d+/\\d+/\\d+):(\\d\{1,4\})" {
    set secondMatch "$expect_out(2,string)" 
    send "show test1 $secondMatch\r"
    send "show test2 $secondMatch\r"
    send "show test3 $secondMatch\r"
    exp_continue
  }
在我的尝试中,结果是:

  send "show test1 1/1/1\r"
  send "show test2 1/1/1\r"
  send "show test3 1/1/1\r"

  send "show test1 1/1/3\r"
  send "show test2 1/1/3\r"
  send "show test3 1/1/3\r"

  send "show test1 1/1/2\r"
  send "show test2 1/1/2\r"
  send "show test3 1/1/2\r"

  send "show test1 1/1/4\r"
  send "show test2 1/1/4\r"
  send "show test3 1/1/4\r"

  send "show test1 1/1/2\r"
  send "show test2 1/1/2\r"
  send "show test3 1/1/2\r"

  send "show test1 1/1/1\r"
  send "show test2 1/1/1\r"
  send "show test3 1/1/1\r"

  send "show test1 1/1/1\r"
  send "show test2 1/1/1\r"
  send "show test3 1/1/1\r"
结果应该是:

  send "show test1 1/1/1\r"
  send "show test2 1/1/1\r"
  send "show test3 1/1/1\r"

  send "show test1 1/1/3\r"
  send "show test2 1/1/3\r"
  send "show test3 1/1/3\r"

  send "show test1 1/1/2\r"
  send "show test2 1/1/2\r"
  send "show test3 1/1/2\r"

  send "show test1 1/1/4\r"
  send "show test2 1/1/4\r"
  send "show test3 1/1/4\r"

*1/1/1、1/1/3、1/1/2、1/1/4是变量,但对于示例,我放了内容:

我找到了一个对我有用的答案:)

其实很简单

...
if [regexp $secondMatch $secondMatchSTACKED] {
  exp_continue
} else {
...
send "show test1 $secondMatch\r"
send "show test2 $secondMatch\r"
send "show test3 $secondMatch\r"
set secondMatchSTACKED "$secondMatchSTACKED$secondMatch "
exp_continue

你到底得到了什么?我补充道:在我的尝试中,结果是:如果我可以重新解释,我会说:我需要将expect命令的输出写入变量或文件,然后使用“|uniq”将该输出写入管道(|),然后在其他变量或文件中返回结果(最棘手的部分是在一次telnet会话中与设备进行通信,而不是两次:D)