[英]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.