繁体   English   中英

保存的格式输出在[grep,sed,awk或?]列中

[英]Saved format output in columns [grep, sed, awk or ?]

我已经创建了一个Telnet到多个+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脚本“ 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

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

感谢您的帮助。

至于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是上面脚本的名称。

@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";
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM