【问题标题】:expect script regex not working期望脚本正则表达式不起作用
【发布时间】:2015-08-25 18:01:04
【问题描述】:

此脚本未能按预期工作:

#!/usr/bin/expect
set timeout 2
set server [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
set mac [lindex $argv 3]
set interface "po1"
spawn ssh $user@$server
expect "Password:"
send -- "$password\n"
expect "*>"
send "show mac address-table address $mac\n"
# 100      1cc1.de65.441c   dynamic ip,ipx,assigned,other Port-channel43
expect -re { (\d+) *($mac) *(dynamic|static) *(.*) *(.*)} {
        set interface $expect_out(5,string)
        expect "*>"
        send "show interface $interface status\n"
}
send "exit\n"
interact

发出上面的 show mac 命令后,输出包含一行看起来像下面注释的那一行。但是下面的 expect -re 块永远不会被命中,使其超时并发送退出命令。

示例输出: 生成 ssh 用户@host 密码:

================= Host login banner  =================
host>show mac address-table address 1cc1.de65.441c
Unicast Entries
 vlan     mac address     type        protocols               port
---------+---------------+--------+---------------------+-------------------------
 100      1cc1.de65.441c   dynamic ip,ipx,assigned,other Port-channel43


host>exit
Connection to host closed.

【问题讨论】:

    标签: regex expect


    【解决方案1】:

    您的正则表达式不正确,不会发生大括号内的替换。即$mac 不会被替换,因为它在大括号内。

    expect {
        -re "\\d+\\s+$mac\\s+(dynamic|static)\\s+\\S+\\s+(\\S+)" { set interface $expect_out(2,string); # Add your further code}
    }
    

    【讨论】:

    • 如果我们只需要提取端口信息,那么其他项我们可以简单地与\S匹配。目前,我假设您只需要端口信息。如果您愿意,可以自定义它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多