期望脚本正则表达式不起作用 [英] expect script regex not working

查看:280
本文介绍了期望脚本正则表达式不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此脚本无法按预期工作:

This script is failing to work as expected:

#!/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块永远不会被命中,从而使其超时并发送退出命令.

After issuing the show mac command above, the output contains one line that looks like the one commented below it. but the following expect -re block is never hit, making it time out and send the exit command.

样本输出: 生成ssh user @ host 密码:

Sample output: spawn ssh user@host Password:

================= 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.

推荐答案

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

Your regex is not correct and inside braces substitutions won't happen. i.e. $mac won't be substituted as it is inside braces.

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

这篇关于期望脚本正则表达式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆