我有来自远程设备的输入,我想要捕获并基于它来执行一些命令-脚本工作得很好!但是有一个问题- expect循环遍历每一行(使用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是变量,但对于示例,我将内容:
发布于 2019-03-09 06:49:30
我找到了一个适合我的答案:)
实际上,这很容易。
...
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_continuehttps://stackoverflow.com/questions/54953625
复制相似问题