2014-03-04 95 views
0

如何将$ interface添加到列表$ interfaces中?期望追加到列表

expect -re "(.*) ago, via (.*)\r" { lappend interfaces $expect_out(2,string) } 

foreach interface $interfaces { 
     puts "$interface" 
} 

值2成正则表达式存在,但我的代码返回:

can't read "interfaces": no such variable 
    while executing 
"foreach interface $interfaces { 

回答

1
  1. 你确定模式匹配?与expect -d运行您的程序,确保
  2. 保护自己免受错误:

    if {[info exists interfaces]} { 
        foreach intf $interfaces {puts $intf} 
    } else { 
        puts "error: no interfaces found" 
    } 
    

至于要提取了很多接口,你需要某种形式的循环中。试试这个:

send "command to produce interface names\r" 
expect { 
    -re {via ([^\r]+)} { 
     lappend interfaces $expect_out(1,string) 
     exp_continue 
    } 
    "prompt> " 
} 

阿尔特“提示>”,无论你需要你就大功告成了抢夺接口名时的期望。

+0

好的。如果我有更多的线路可以做这样的事情? 而... { 期待-re “(。*)前,通过(。*)\ r”{lappend接口$ expect_out(2,字符串)}} 我想多抢线(更$接口)。 非常感谢你 – user476918

+0

我无法找到一个例子。我想解析更多的行并将匹配的值放入列表中以使用foreach显示。似乎很复杂与TCL :) – user476918

+0

我非常感谢您的帮助。 使用最新的mofidy代码返回:“无法读取”接口“:没有这样的变量” 该命令产生的缓冲区为: * 1.1.1.1,从2.2.2.2,00.00:00之前,通过Int1 3.3.3.3,从4.4.4.4,00:00:00以前,通过Int2 并单独正确解析正则表达式:( – user476918