Expect-regex 正面回顾

Expect-regex positive lookbehind

所以我尝试使用 expect 来自动登录到交换机并获取与 mac 地址关联的接口,但是每当我尝试使用正后向正则表达式时它就会出错。 这是输入的样子:

VLAN ID  MAC Address         Interface  IfIndex  Status
-------  ------------------  ---------  -------  ------------
100      13:3N:K2:98:33:09   0/2        2        Static
100      52:0L:H9:74:6B:GG   0/8        8        Static
100      85:2F:E7:02:25:74   0/10       10       Static

这是代码:

expect -c "
...
send \"show mac-addr-table\r\";
expect -re {(?<=85:2F:E7:02:25:74).*(0\/..)}
set output $expect_out(buffer)
puts \"Result : $output\"
"

这是我希望的输出:

Result : 0/10

可悲的是,每当我尝试这个时,我们都会遇到错误:

couldn't compile regular expression pattern: quantifier operand invalid
    while executing
"expect -re {(?<=85:2F:E7:02:25:74).*(0\/..)}"

我还应该补充一点,试图逃跑?不输出任何东西。

Tcl(所以 Expect)不支持 (?<=re) 语法(来自 Perl?)。您可以这样尝试:

expect -re {85:2F:E7:02:25:74.*(0/..)}
set output $expect_out(1,string)

这来自 expect 的手册:

Upon matching a pattern (or eof or full_buffer), any matching and previously unmatched output is saved in the variable expect_out(buffer). Up to 9 regexp substring matches are saved in the variables expect_out(1,string) through expect_out(9,string).