期望 - expect_out比较

问题描述 投票:0回答:1

我从远程设备输入我想要捕获并基于它来执行一些命令 - 脚本工作正常!但是有一个问题 - 期望循环遍历每一行(使用exp_continue),当找到一个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是变量但是我举例说明了内容:

tcl expect
1个回答
0
投票

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

实际上这很容易。

...
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
© www.soinside.com 2019 - 2024. All rights reserved.