【问题标题】:Saved format output in columns [grep, sed, awk or ?]在列中保存格式输出 [grep、sed、awk 或 ?]
【发布时间】:2015-06-04 07:43:57
【问题描述】:

我创建了一个可以远程登录到多个 +200 交换机的 Expect 脚本文件。

我的代码:

#!/usr/bin/expect -f
#Slurp up the input file
set fp [open "ip.txt" r]
# To avoid empty lines, 'nonewline' flag is used
set file_data [read -nonewline $fp]
close $fp
set prompt ">"
log_file -noappend switch_port_status.txt
foreach ip [split $file_data "\n"] {
puts "Switch $ip Interface Status"
spawn telnet $ip
expect "Username:"
send "MyUsername\r"
expect "assword:"
send "MyPassword\r"
expect $prompt
# To avoid sending 'Enter' key on huge configurations
send "show ip int br\r"
expect {
-ex "--More--" { send -- " "; exp_continue }
"*>" { send "exit\r" }
}
#expect $prompt
set timeout 1; # Reverting to default timeout
# Sending 'exit' at global level prompt will close the connection
expect eof

对于 Expect Script "log_file -noappend switch_port_status.txt",我保存了一个输出文件如下:

Trying 192.168.0.1...
Connected to 192.168.0.1 (192.168.0.1).
Escape character is '^]'.

Authorized access only
This system is the property of Company Name
Disconnect IMMEDIATELY if you are not an authorized users!


User Access Verification

Username: MyUsername
Password:

Switch-hostname1>show ip int br
Interface              IP-Address  OK? Method          Status                 Protocol
Vlan1                  unassigned  YES  NVRAM          administratively down  down
Vlan308                192.168.0.1 YES  NVRAM          up                     up
Vlan603                unassigned  YES  NVRAM          administratively down  down
GigabitEthernet0/1     unassigned  YES  unset          up                     up
GigabitEthernet0/2     unassigned  YES  unset          down                   down
LongReachEthernet0/1   unassigned  YES   unset         administratively down  down
LongReachEthernet0/2   unassigned  YES   unset         administratively down  down
LongReachEthernet0/3   unassigned  YES   unset         administratively down  down
LongReachEthernet0/4   unassigned  YES   unset         administratively down  down
LongReachEthernet0/5   unassigned  YES   unset         administratively down  down
LongReachEthernet0/6   unassigned  YES   unset         up                     up
LongReachEthernet0/7   unassigned  YES   unset         up                     up
LongReachEthernet0/8   unassigned  YES   unset         administratively down  down
LongReachEthernet0/9   unassigned  YES   unset         administratively down  down
LongReachEthernet0/10 unassigned  YES   unset         up                     up
LongReachEthernet0/11 unassigned  YES   unset         administratively down  down
LongReachEthernet0/12 unassigned  YES   unset         administratively down  down
LongReachEthernet0/13 unassigned  YES   unset         administratively down  down
LongReachEthernet0/14 unassigned  YES   unset         administratively down  down
LongReachEthernet0/15 unassigned  YES   unset         administratively down  down
LongReachEthernet0/16 unassigned  YES   unset         up                     up
LongReachEthernet0/17 unassigned  YES   unset         administratively down  down
LongReachEthernet0/18 unassigned  YES   unset         administratively down  down
LongReachEthernet0/19 unassigned  YES   unset         administratively down  down
LongReachEthernet0/20 unassigned  YES   unset         up                     up
LongReachEthernet0/21 unassigned  YES   unset         administratively down  down
LongReachEthernet0/22 unassigned  YES   unset         administratively down  down
LongReachEthernet0/23 unassigned  YES   unset         administratively down  down
LongReachEthernet0/24 unassigned  YES   unset         administratively down  down
Switch-hostname1>exit
Connection closed by foreign host.
Trying 192.168.0.2...
Connected to 192.168.0.2 (192.168.0.2).
Escape character is '^]'.

Authorized access only
This system is the property of Company Name
Disconnect IMMEDIATELY if you are not an authorized users!


User Access Verification

Username: MyUsername
Password:

Switch-hostname2>show ip int br
Interface             IP-Address  OK? Method   Status                Protocol
Vlan1                 unassigned  YES NVRAM    administratively down down
Vlan308               192.168.0.2 YES NVRAM    up                    up
Vlan603               unassigned  YES NVRAM    administratively down down
GigabitEthernet0/1    unassigned  YES unset    up                    up
GigabitEthernet0/2    unassigned  YES unset    down                  down
LongReachEthernet0/1  unassigned  YES unset    administratively down down
LongReachEthernet0/2  unassigned  YES unset    administratively down down
LongReachEthernet0/3  unassigned  YES unset    administratively down down
LongReachEthernet0/4  unassigned  YES unset    down                  down
LongReachEthernet0/5  unassigned  YES unset    administratively down down
LongReachEthernet0/6  unassigned  YES unset    administratively down down
LongReachEthernet0/7  unassigned  YES unset    administratively down down
LongReachEthernet0/8  unassigned  YES unset    administratively down down
LongReachEthernet0/9  unassigned  YES unset    administratively down down
LongReachEthernet0/10 unassigned YES unset     administratively down down
LongReachEthernet0/11 unassigned YES unset     administratively down down
LongReachEthernet0/12 unassigned YES unset     administratively down down
LongReachEthernet0/13 unassigned YES unset     administratively down down
LongReachEthernet0/14 unassigned YES unset     administratively down down
LongReachEthernet0/15 unassigned YES unset     administratively down down
LongReachEthernet0/16 unassigned YES unset     administratively down down
LongReachEthernet0/17 unassigned YES unset     administratively down down
LongReachEthernet0/18 unassigned YES unset     up up
LongReachEthernet0/19 unassigned YES unset     administratively down down
LongReachEthernet0/20 unassigned YES unset     administratively down down
LongReachEthernet0/21 unassigned YES unset     administratively down down
LongReachEthernet0/22 unassigned YES unset     up up
LongReachEthernet0/23 unassigned YES unset     administratively down down
LongReachEthernet0/24 unassigned YES unset     administratively down down
Switch-hostname2>Trying 192.168.0.3...
Connected to 192.168.0.3 (192.168.0.3).
Escape character is '^]'.

Authorized access only
This system is the property of Company Name
Disconnect IMMEDIATELY if you are not an authorized users!


User Access Verification

Username: MyUsername
Password:

Switch-hostname3#show ip int br
Interface            IP-Address  OK? Method     Status Protocol
Vlan1                192.168.0.3 YES NVRAM      up     up
FastEthernet0/1      unassigned  YES unset      up     up
FastEthernet0/2      unassigned  YES unset      up     up
FastEthernet0/3      unassigned  YES unset      up     up
FastEthernet0/4      unassigned  YES unset      up     up
FastEthernet0/5      unassigned  YES unset      up     up
FastEthernet0/6      unassigned  YES unset      up     up
FastEthernet0/7      unassigned  YES unset      up     up
FastEthernet0/8      unassigned  YES unset      down   down
FastEthernet0/9      unassigned  YES unset      down   down
FastEthernet0/10    unassigned  YES unset       down   down
FastEthernet0/11    unassigned  YES unset       down   down
FastEthernet0/12    unassigned  YES unset       down   down
FastEthernet0/13    unassigned  YES unset       down   down
FastEthernet0/14    unassigned  YES unset       down   down
FastEthernet0/15    unassigned  YES unset       down   down
FastEthernet0/16    unassigned  YES unset       down   down
FastEthernet0/17    unassigned  YES unset       down   down
FastEthernet0/18    unassigned  YES unset       down   down
FastEthernet0/19    unassigned  YES unset       down   down
FastEthernet0/20    unassigned  YES unset       down   down
FastEthernet0/21    unassigned  YES unset       down   down
FastEthernet0/22    unassigned  YES unset       up     up
FastEthernet0/23    unassigned  YES unset       down   down
FastEthernet0/24    unassigned  YES unset       down   down
GigabitEthernet0/1  unassigned  YES unset       up     up
GigabitEthernet0/2  unassigned  YES unset       down   down
Switch-hostname4>exit
Connection closed by foreign host.

Trying 192.168.0.4...
Connected to 192.168.0.4 (192.168.0.4).
Escape character is '^]'.

Authorized access only
This system is the property of Company Name
Disconnect IMMEDIATELY if you are not an authorized users!


User Access Verification

Username: MyUsername
Password:

Switch-hostname4#show ip int br

............等等等等。

这可以用 Linux 中的 awk/sed/grep 实用程序来完成吗?

所需的输出格式:

192.168.0.1
Switch-hostname1
Vlan1,administratively down
Vlan308,up 
Vlan603,administratively down
GigabitEthernet0/1,up 
GigabitEthernet0/2 ,down
LongReachEthernet0/1,administratively down
LongReachEthernet0/2,administratively down
LongReachEthernet0/3,administratively down
LongReachEthernet0/4,administratively down
LongReachEthernet0/5,administratively down
LongReachEthernet0/6,up
LongReachEthernet0/7,up
LongReachEthernet0/8,administratively down
LongReachEthernet0/9,administratively down
LongReachEthernet0/10,up
LongReachEthernet0/11,administratively down
LongReachEthernet0/12,administratively down
LongReachEthernet0/13,administratively down
LongReachEthernet0/14,administratively down
LongReachEthernet0/15,administratively down
LongReachEthernet0/16,up
LongReachEthernet0/17,administratively down
LongReachEthernet0/18,administratively down
LongReachEthernet0/19,administratively down
LongReachEthernet0/20,up
LongReachEthernet0/21,administratively down
LongReachEthernet0/22,administratively down
LongReachEthernet0/23,administratively down
LongReachEthernet0/24,administratively down

192.168.0.2
Switch-hostname2
Vlan1,administratively
Vlan308,up
Vlan603,administratively
GigabitEthernet0/1,up
GigabitEthernet0/2,down
LongReachEthernet0/1,administratively
LongReachEthernet0/2,administratively
LongReachEthernet0/3,administratively
LongReachEthernet0/4,down
LongReachEthernet0/5,administratively
LongReachEthernet0/6,administratively
LongReachEthernet0/7,administratively
LongReachEthernet0/8,administratively
LongReachEthernet0/9,administratively
LongReachEthernet0/10,administratively
LongReachEthernet0/11,administratively
LongReachEthernet0/12,administratively
LongReachEthernet0/13,administratively
LongReachEthernet0/14,administratively
LongReachEthernet0/15,administratively
LongReachEthernet0/16,administratively
LongReachEthernet0/17,administratively
LongReachEthernet0/18,up
LongReachEthernet0/19,administratively
LongReachEthernet0/20,administratively
LongReachEthernet0/21,administratively
LongReachEthernet0/22,up
LongReachEthernet0/23,administratively
LongReachEthernet0/24,administratively

192.168.0.3
Switch-hostname3
Vlan1,up
FastEthernet0/1,up
FastEthernet0/2,up
FastEthernet0/3,up
FastEthernet0/4,up
FastEthernet0/5,up
FastEthernet0/6,up
FastEthernet0/7,up
FastEthernet0/8,down
FastEthernet0/9,down
FastEthernet0/10,down
FastEthernet0/11,down
FastEthernet0/12,down
FastEthernet0/13,down
FastEthernet0/14,down
FastEthernet0/15,down
FastEthernet0/16,down
FastEthernet0/17,down
FastEthernet0/18,down
FastEthernet0/19,down
FastEthernet0/20,down
FastEthernet0/21,down
FastEthernet0/22,up
FastEthernet0/23,down
FastEthernet0/24,down
GigabitEthernet0/1,up
GigabitEthernet0/2,down

192.168.0.4
Switch-hostname4

............等等等等。

提前感谢您的帮助。

【问题讨论】:

  • 非常详尽,但对于简单重新格式化来说有点庞大(信息量)
  • 您的输出是完整的或过滤的,例如没有退出 192.168.0.2 但对于其他井,....换句话说,您的标准是什么来重新格式化它?跨度>
  • 输出是满的,它会自动像这样输出。如果我们假设有一个出口,我们应该省略下面的词:『正在尝试 IP 地址......连接到 IP 地址(IP 地址)。转义字符是 '^]'。仅限授权访问 如果您不是授权用户,该系统是公司名称的财产 立即断开连接!用户访问验证用户名:MyUsername 密码:』 『>show ip int br』 『>exit Connection closed by foreign host.』
  • 只有 IP、主机名、接口和状态才能获得我的帖子中提到的输出文件条件。我可以编辑期望脚本以将输出文件转换为上面所需的格式输出吗?还是我需要打开另一个 perl 文件?

标签: regex perl awk sed expect


【解决方案1】:

至于 perl 的实现,我建议这样做:

#!/usr/bin/perl -nl

print "\n$1" if (/Connected to (\S+)/);
if ($line_num = (/^(\S+?)(?:>|#)show ip int br/ ... /^.*?(?:>|#)/)) {
    if ($line_num == 1) {
        print $1;
    } elsif ($line_num > 2 && /^(\S+)\s+\S+\s+\S+\s+\S+\s+(.*)\s+\S+$/)  {
        print "$1;$2";
    } elsif ($line_num > 2 && /^\s*$/)  {   # retian empty lines after 
                                            # 'show ip int br' if there are any
        print;
    }
}

用法:squeeze.pl your_swicth_status_script_output.txt 其中squeeze.pl 是上述脚本的名称。

【讨论】:

  • 我已更改脚本以考虑各种提示字符并可能缺少“退出”命令
  • 您提供的脚本可以工作,但还有一个问题。当我使用发布的脚本副本返回交换机时,我可以使用您提供的 perl 获得预期的结果,如图所示。但是,如果我使用相同的 perl 在上一个开关中运行输出文件,则在“show ip interface brief”之后这些行不会分开。我尝试编辑但失败,请建议我如何进行修改。非常感谢。
  • 【格式为我的帖子】:(i109.photobucket.com/albums/n73/CyberLuk/…)【脚本工作】:(i109.photobucket.com/albums/n73/CyberLuk/…)【原始格式进入开关】:(i109.photobucket.com/albums/n73/CyberLuk/…)【只显示IP&主机名】:( i109.photobucket.com/albums/n73/CyberLuk/…) [原始输出文件下载链接]: (drive.google.com/file/d/0B0tUlNEULMxqZkt2WXZwcnVWcWc/…)
  • 嗨@DannyLuk!如果有空行,您需要在接口表中保留空行吗?如果是这样,请使用答案中的更新脚本版本。否则请您详细说明您的要求吗?
  • 非常感谢!我的帖子中所需的输出格式。当我使用我发布的脚本副本返回交换机时,这是我的帖子格式问题。不使用“$”解决的问题。
【解决方案2】:

@Dmitry Egorov 非常感谢!
我的帖子中所需的输出格式。当我使用我发布的脚本副本返回交换机时,这是我的帖子格式问题。不使用“$”解决的问题。

你之前的代码:

#!/usr/bin/perl -nl

print "\n$1" if (/Connected to (\S+)/);
if ($line_num = (/^(\S+?)(?:>|#)show ip int br/ ... /^.*?(?:>|#)/)) {
    if ($line_num == 1) {
        print $1;
    } elsif ($line_num > 2 && /^(\S+)\s+\S+\s+\S+\s+\S+\s+(.*)\s+\S+$/) {
        print "$1;$2";
    }
}

我当前的代码:

#!/usr/bin/perl -nl

print "\n$1" if (/Connected to (\S+)/);
if ($line_num = (/^(\S+?)(?:>|#)show ip int br/ ... /^.*?(?:>|#)/)) {
    if ($line_num == 1) {
        print $1;
    } elsif ($line_num > 2 && /^(\S+)\s+\S+\s+\S+\s+\S+\s+(.*)\s+\S+/)  {
        print "$1;$2";
    }
}

【讨论】:

    猜你喜欢
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 2018-08-23
    • 2017-08-28
    相关资源
    最近更新 更多