【发布时间】:2019-03-09 06:49:30
【问题描述】:
我有来自远程设备的输入,我想捕获并基于它执行一些命令 - 脚本工作正常!但有一个问题 - 期望循环遍历每一行(使用 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 是变量,但例如我放内容:
【问题讨论】:
-
你实际上得到了什么?
-
我补充说:在我的尝试结果是:
-
如果我可以重新解释一下,我会说:我需要将期望命令的输出写入变量或文件 - 然后通过管道 (|) 这个输出,例如使用“|uniq”,然后到在其他变量或文件中返回结果(棘手的部分是在一个 telnet 会话中将结果发送到设备而不是两个 :D)