首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >expect - expect_out比较

expect - expect_out比较
EN

Stack Overflow用户
提问于 2019-03-02 07:19:31
回答 1查看 69关注 0票数 1

我有来自远程设备的输入,我想要捕获并基于它来执行一些命令-脚本工作得很好!但是有一个问题- expect循环遍历每一行(使用exp_continue),并且当查找REGEX匹配时执行一些基于匹配的命令($expect_out)。运行良好..,但字面上基于每一行!:(并且我希望以某种方式将$expect_out(n,string)与其他以前和将来的匹配进行比较,并根据唯一匹配仅执行一个命令。

也许给出这个例子更简单:

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

输入:

代码语言:javascript
复制
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

脚本:

代码语言:javascript
复制
  #!/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
  }

我的尝试结果是:

代码语言:javascript
复制
  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"

结果应该是:

代码语言:javascript
复制
  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是变量,但对于示例,我将内容:

EN

回答 1

Stack Overflow用户

发布于 2019-03-09 06:49:30

我找到了一个适合我的答案:)

实际上,这很容易。

代码语言:javascript
复制
...
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54953625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档