背景
在日常维护cisco设备时,常用到"sh int counters"命令,但该命令不提供排序功能,不太方便.
于是采用perl写了一个类似的脚本,可以帮忙对交换机接口状态一目了然,且支持排序.
接口参数
该脚本主要是对如下参数进行采集和排序.
-
#sh int g1/0/1
-
GigabitEthernet1/0/1 is up, line protocol is up (connected)
-
| | |
-
| | +--------------------------------------1.3.6.1.4.1.9.2.2.1.1.2
-
| +------------------------------------------------------------1.3.6.1.2.1.2.2.1.8
-
+---------------------------------------------------------------------------+--Index:1.3.6.1.2.1.2.2.1.1
-
+--Type:1.3.6.1.2.1.2.2.1.3
-
+--Name:1.3.6.1.2.1.2.2.1.2
-
+--Name:1.3.6.1.2.1.31.1.1.1.1
-
Hardware is Gigabit Ethernet, address is ec30.91ca.3481 (bia ec30.91ca.3481)
-
Description: link-cisco4507r
-
MTU 1500 bytes, BW 1000000 Kbit, DLY 10 usec,
-
reliability 255/255, txload 6/255, rxload 2/255
-
Encapsulation ARPA, loopback not set
-
Keepalive not set
-
Full-duplex, 1000Mb/s, link type is auto, media type is 1000BaseSX SFP
-
input flow-control is off, output flow-control is unsupported
-
ARP type: ARPA, ARP Timeout 04:00:00
-
Last input 00:00:00, output 00:00:00, output hang never
-
Last clearing of "show interface" counters never
-
Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 1416375
-
|
-
+------------1.3.6.1.2.1.2.2.1.19
-
或1.3.6.1.4.1.9.2.2.1.1.27
-
Queueing strategy: fifo
-
Output queue: 0/0 (size/max)
-
5 minute input rate 9130000 bits/sec, 3706 packets/sec
-
| |
-
| +-------------------------+-----5分钟平均值:1.3.6.1.4.1.9.2.2.1.1.7
-
| +-----即时值(毫秒级):1.3.6.1.4.1.9.2.2.1.1.3
-
+-------------------------------------------5分钟平均值:1.3.6.1.4.1.9.2.2.1.1.6
-
5 minute output rate 25827000 bits/sec, 3713 packets/sec
-
| |
-
| +-------------------------+-----5分钟平均值:1.3.6.1.4.1.9.2.2.1.1.9
-
| +-----即时值(毫秒级):1.3.6.1.4.1.9.2.2.1.1.4
-
+-------------------------------------------5分钟平均值:1.3.6.1.4.1.9.2.2.1.1.8
-
5588544812 packets input, 1594789568486 bytes, 0 no buffer
-
| |
-
| +---------------------------------1.3.6.1.2.1.31.1.1.1.6
-
+-----------------------------------------------------------单播+广播+组播,其中单播的OID是:1.3.6.1.2.1.2.2.1.11
-
Received 32813253 broadcasts (31962960 multicasts)
-
| |
-
| +------------------------------组播:1.3.6.1.2.1.31.1.1.1.8
-
+--------------------------------------------------广播+组播,广播的OID是:1.3.6.1.2.1.31.1.1.1.9
-
23 runts, 0 giants, 0 throttles
-
23 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
-
| |
-
| +--------------------------------------------1.3.6.1.4.1.9.2.2.1.1.12
-
+-------------------------------------------------------------1.3.6.1.2.1.2.2.1.14
-
0 watchdog, 31962960 multicast, 0 pause input
-
0 input packets with dribble condition detected
-
4979391462 packets output, 3499294634681 bytes, 0 underruns
-
| |
-
| +--------------------------------1.3.6.1.2.1.31.1.1.1.10
-
+----------------------------------------------------------单播+组播+广播
-
单播:1.3.6.1.2.1.31.1.1.1.11或1.3.6.1.2.1.2.2.1.17
-
组播:1.3.6.1.2.1.31.1.1.1.12
-
广播:1.3.6.1.2.1.31.1.1.1.13
-
0 output errors, 0 collisions, 1 interface resets
-
| | |
-
| | +-----------------------------1.3.6.1.4.1.9.2.2.1.1.17
-
| +-------------------------------------------1.3.6.1.4.1.9.2.2.1.1.25
-
+--------------------------------------------------------------1.3.6.1.2.1.2.2.1.20
-
0 babbles, 0 late collision, 0 deferred
-
0 lost carrier, 0 no carrier, 0 PAUSE output
-
0 output buffer failures, 0 output buffers swapped out
使用方式:
-
Usage: view-nic Remote-Option [OID-Option] [Sort-Option] [Other-Option]
-
-
Note:只支持cisco设备,不支持串口
-
-
Remote-Options:
-
-H <hostname> 交换机管理IP
-
-c <community> SNMP的Community字串,默认为public
-
-
OID-Option
-
-m "InputRatepbm"=>"1.3.6.1.4.1.9.2.2.1.1.3" 每毫秒Input方面的包数量
-
"OutputRatepbm"=>"1.3.6.1.4.1.9.2.2.1.1.4" 每毫秒Output方面的包数量
-
-
-r "Input5minRatebps"=>"1.3.6.1.4.1.9.2.2.1.1.6" 每5分钟Input方面的字节数量
-
"Out5minRatebps"=>"1.3.6.1.4.1.9.2.2.1.1.8" 每5分钟Output方面的字节数量
-
-
-R "Input5minRatepbs"=>"1.3.6.1.4.1.9.2.2.1.1.7" 每5分钟Input方面的包数量
-
"Out5minRatepbs"=>"1.3.6.1.4.1.9.2.2.1.1.9" 每5分钟Output方面的包数量
-
-
-P "InputUcastPkts"=>"1.3.6.1.2.1.2.2.1.11" Input方向单播数量
-
"OutputUcastPkts"=>"1.3.6.1.2.1.31.1.1.1.11" Output方向单播数量
-
-
-M "InputMcastPkts"=>"1.3.6.1.2.1.31.1.1.1.8" Input方向组播数量
-
"OutputMcastPkts"=>"1.3.6.1.2.1.31.1.1.1.12" Output方向组播数量
-
-
-B "InputBcastPkts"=>"1.3.6.1.2.1.31.1.1.1.9" Input方向广播数量
-
"OutputBcastPkts"=>"1.3.6.1.2.1.31.1.1.1.13" Output方向广播数量
-
-
-b "InputBytes"=>"1.3.6.1.2.1.2.2.1.10" Input方向单播数量
-
"OutputBytes"=>"1.3.6.1.2.1.2.2.1.16" Output方向单播数量
-
-
-e "InputError"=>"1.3.6.1.2.1.2.2.1.14" Input方向错包数量
-
"OutputError"=>"1.3.6.1.2.1.2.2.1.20" Output方向错包数量
-
-
-C "InputCRC"=>"1.3.6.1.4.1.9.2.2.1.1.12" Input方向的CRC数量
-
-
-t "OutputResets"=>"1.3.6.1.4.1.9.2.2.1.1.17" Output方向的interface resets数量
-
-
-D "OutputDrop"=>"1.3.6.1.2.1.2.2.1.19" Output方向的丢包数量
-
-
-a All 选中上面所有参数
-
-
Sort-Option
-
-s <char> Only OID-Option char,more detail as below.
-
-
参数 排序档位
-
---- -------------
-
mI InputRatepbm
-
mO OutputRatepbm
-
rI Input5minRatebps
-
rO Out5minRatebps
-
RI Input5minRatepbs
-
RO Out5minRatepbs
-
MI InputMcastPkts
-
MO OutputMcastPkts
-
BI InputBcastPkts
-
BO OutputBcastPkts
-
bI InputBytes
-
bO OutputBytes
-
eI InputError
-
eO OutputError
-
C InputCRC
-
t OutputResets
-
D OutputDrop
-
-
-S 0 or 1 0 递增,1 递减
-
-
Other-Option
-
-h or -v Help script Usage
例:
[root@localhost ~]# view-nic -H 10.1.1.200 -c public -R -s RO
Index Name Input5minRatepbs Out5minRatepbs
1 Vl1 0 0
3 Vl3 14 0
10101 Gi0/1 897 2
10008 Fa0/8 1 6
10023 Fa0/23 0 891
Index Name Input5minRatepbs Out5minRatepbs
源代码
-
#!/usr/bin/perl
-
# Version : 0.1
-
# Date : 2012-04-18
-
-
# Licence : GPL - http://www.fsf.org/licenses/gpl.txt
-
-
use strict;
-
use Net::SNMP qw{ :asn1 :snmp :translate };
-
use Getopt::Std;
-
-
my $script = GetScriptName($0);
-
my $script_version = "0.1";
-
-
-
my $ipaddress = "127.0.0.1";
-
my $port = 161;
-
my $version = "snmpv2c";
-
my $community = "public";
-
my $timeout = 2;
-
my $status = 0;
-
-
my @FieldName = ( #it's crucial option
-
"Name", #0
-
"InputRatepbm", #1
-
"OutputRatepbm", #2
-
"Input5minbps", #3
-
"Out5minbps", #4
-
"Input5minpbs", #5
-
"Out5minpbs", #6
-
"InputUcastPkts", #7
-
"OutputUcastPkts", #8
-
"InputMcastPkts", #9
-
"OutputMcastPkts", #10
-
"InputBcastPkts", #11
-
"OutputBcastPkts", #12
-
"InputBytes", #13
-
"OutputBytes", #14
-
"InputError", #15
-
"OutputError", #16
-
"InputCRC", #17
-
"OutputResets", #18
-
"OutputDrop", #19
-
"Outcollisions" #20
-
);
-
-
my %InterfaceOID_orgin = ( #it's crucial option view-Option sort-char
-
$FieldName[0] => "1.3.6.1.2.1.31.1.1.1.1", #0 "Name" n/a n/a
-
$FieldName[1] => "1.3.6.1.4.1.9.2.2.1.1.3", #1 "InputRatepbm" m or a mI
-
$FieldName[2] => "1.3.6.1.4.1.9.2.2.1.1.4", #2 "OutputRatepbm" m or a mO
-
$FieldName[3] => "1.3.6.1.4.1.9.2.2.1.1.6", #3 "Input5minRatebps" r or a rI
-
$FieldName[4] => "1.3.6.1.4.1.9.2.2.1.1.8", #4 "Out5minRatebps" r or a rO
-
$FieldName[5] => "1.3.6.1.4.1.9.2.2.1.1.7", #5 "Input5minRatepbs" R or a RI
-
$FieldName[6] => "1.3.6.1.4.1.9.2.2.1.1.9", #6 "Out5minRatepbs" R or a RO
-
$FieldName[7] => "1.3.6.1.2.1.2.2.1.11", #7 "InputUcastPkts" P or a PI
-
$FieldName[8] => "1.3.6.1.2.1.31.1.1.1.11", #8 "OutputUcastPkts" P or a PO
-
$FieldName[9] => "1.3.6.1.2.1.31.1.1.1.8", #9 "InputMcastPkts" M or a MI
-
$FieldName[10] => "1.3.6.1.2.1.31.1.1.1.12", #10 "OutputMcastPkts" M or a MO
-
$FieldName[11] => "1.3.6.1.2.1.31.1.1.1.9", #11 "InputBcastPkts" B or a BI
-
$FieldName[12] => "1.3.6.1.2.1.31.1.1.1.13", #12 "OutputBcastPkts" B or a BO
-
$FieldName[13] => "1.3.6.1.2.1.31.1.1.1.6", #13 "InputBytes" b or a bI
-
$FieldName[14] => "1.3.6.1.2.1.31.1.1.1.10", #14 "OutputBytes" b or a bO
-
$FieldName[15] => "1.3.6.1.2.1.2.2.1.14", #15 "InputError" e or a eI
-
$FieldName[16] => "1.3.6.1.2.1.2.2.1.20", #16 "OutputError" e or a eO
-
$FieldName[17] => "1.3.6.1.4.1.9.2.2.1.1.12", #17 "InputCRC" C or a C
-
$FieldName[18] => "1.3.6.1.4.1.9.2.2.1.1.17", #18 "OutputResets" t or a t
-
$FieldName[19] => "1.3.6.1.2.1.2.2.1.19", #19 "OutputDrop" D or a D
-
$FieldName[20] => "1.3.6.1.4.1.9.2.2.1.1.25" #20 "Outcollisions" o or a o
-
);
-
-
-
my %InterfaceOID; #it's crucial option.
-
-
-
my $SortTable="n"; #defulte sort table is "Name",it's member of @SortName. it's crucial option.
-
my $SortType=0; #0 increase,1 decrease ,it's crucial option.
-
my %SortName; #only n,m,r,R,P,M,B,b,e,C,t,D. it's crucial option.
-
#my %SortName = (
-
#achars Sort-Field
-
#---- -------------
-
#"mI"=>$FieldName[1], #InputRatepbm
-
#"mO"=>$FieldName[2], #OutputRatepbm
-
#"rI"=>$FieldName[3], #Input5minRatebps
-
#"rO"=>$FieldName[4], #Out5minRatebps
-
#"RI"=>$FieldName[5], #Input5minRatepbs
-
#"RO"=>$FieldName[6], #Out5minRatepbs,
-
#"PI"=>$FieldName[7] #"InputUcastPkts",
-
#"PO"=>$FieldName[8] #"OutputUcastPkts",
-
#"MI"=>$FieldName[9], #"InputMcastPkts",
-
#"MO"=>$FieldName[10], #"OutputMcastPkts",
-
#"BI"=>$FieldName[11], #"InputBcastPkts",
-
#"BO"=>$FieldName[12], #"OutputBcastPkts",
-
#"bI"=>$FieldName[13], #"InputBytes",
-
#"bO"=>$FieldName[14], #"OutputBytes",
-
#"eI"=>$FieldName[15], #"InputError",
-
#"eO"=>$FieldName[16], #"OutputError",
-
#"C"=>$FieldName[17], #"InputCRC",
-
#"t"=>$FieldName[18], #"OutputResets",
-
#"D"=>$FieldName[19], #"OutputDrop",
-
#"o"=>$FieldName[20] #"Outcollisions"
-
#);
-
-
-
-
#my $InterfaceObj = { #OID List , it's crucial option.
-
# "Name"=>"Fa1/0/1", #1.3.6.1.2.1.31.1.1.1.1
-
# "InputRatepbm"=>"234", #1.3.6.1.4.1.9.2.2.1.1.3
-
# "Input5minRatebps"=>"123", #1.3.6.1.4.1.9.2.2.1.1.6
-
# "Input5minRatepbs"=>"324", #1.3.6.1.4.1.9.2.2.1.1.7
-
# "InputUcastPkts"=>"1", #1.3.6.1.2.1.2.2.1.11
-
# "InputMcastPkts"=>"1", #1.3.6.1.2.1.31.1.1.1.8
-
# "InputBcastPkts"=>"1", #1.3.6.1.2.1.31.1.1.1.9
-
# "InputBytes"=>"1", #1.3.6.1.2.1.2.2.1.10
-
# "InputError"=>"1", #1.3.6.1.2.1.2.2.1.14
-
# "InputCRC"=>"1", #1.3.6.1.4.1.9.2.2.1.1.12
-
# "OutputRatepbm"=>"234", #1.3.6.1.4.1.9.2.2.1.1.4
-
# "Out5minRatebps"=>"123", #1.3.6.1.4.1.9.2.2.1.1.8
-
# "Out5minRatepbs"=>"324", #1.3.6.1.4.1.9.2.2.1.1.9
-
# "OutputUcastPkts"=>"1", #1.3.6.1.2.1.31.1.1.1.11
-
# "OutputMcastPkts"=>"1", #1.3.6.1.2.1.31.1.1.1.12
-
# "OutputBcastPkts"=>"1", #1.3.6.1.2.1.31.1.1.1.13
-
# "OutputBytes"=>"1", #1.3.6.1.2.1.2.2.1.16
-
# "OutputError"=>"1", #1.3.6.1.2.1.2.2.1.20
-
# "OutputResets"=>"1", #1.3.6.1.4.1.9.2.2.1.1.17
-
# "OutputDrop"=>"321" #1.3.6.1.2.1.2.2.1.19
-
# "Outcollisions"=>"34" #1.3.6.1.4.1.9.2.2.1.1.25
-
#};
-
-
#my %tbNIC = (
-
# "10101" => $InterfaceObj-1,
-
# "10102" => $InterfaceObj-2,
-
# "10103" => $InterfaceObj-3,
-
# ...
-
# "90101" => $InterfaceObj-x
-
#)
-
-
-
-
if (@ARGV < 1) {
-
print "Too few arguments\n";
-
usage();
-
}
-
-
-
#######################################################################################
-
#get script option
-
#######################################################################################
-
my %opt=();
-
getopts("hvH:c:mrRPMBbeCtDs:S:aod",\%opt);
-
-
if($opt{h} || $opt{v})
-
{
-
usage();
-
exit(0);
-
}
-
-
$ipaddress = $opt{H} if defined $opt{H} ;
-
$community = $opt{c} if defined $opt{c} ;
-
-
$InterfaceOID{$FieldName[0]} = $InterfaceOID_orgin{$FieldName[0]};
-
if ((defined $opt{m}) || (defined $opt{a}))
-
{
-
$InterfaceOID{$FieldName[1]}=$InterfaceOID_orgin{$FieldName[1]};
-
$InterfaceOID{$FieldName[2]}=$InterfaceOID_orgin{$FieldName[2]};
-
$SortName{"mI"}=$FieldName[1];
-
$SortName{"mO"}=$FieldName[2];
-
}
-
if ((defined $opt{r}) || (defined $opt{a}))
-
{
-
$InterfaceOID{$FieldName[3]}=$InterfaceOID_orgin{$FieldName[3]};
-
$InterfaceOID{$FieldName[4]}=$InterfaceOID_orgin{$FieldName[4]};
-
$SortName{"rI"}=$FieldName[3];
-
$SortName{"rO"}=$FieldName[4];
-
}
-
if ((defined $opt{R}) || (defined $opt{a}))
-
{
-
$InterfaceOID{$FieldName[5]}=$InterfaceOID_orgin{$FieldName[5]};
-
$InterfaceOID{$FieldName[6]}=$InterfaceOID_orgin{$FieldName[6]};
-
$SortName{"RI"}=$FieldName[5];
-
$SortName{"RO"}=$FieldName[6];
-
}
-
if ((defined $opt{P}) || (defined $opt{a}))
-
{
-
$InterfaceOID{$FieldName[7]}=$InterfaceOID_orgin{$FieldName[7]};
-
$InterfaceOID{$FieldName[8]}=$InterfaceOID_orgin{$FieldName[8]};
-
$SortName{"PI"}=$FieldName[7];
-
$SortName{"PO"}=$FieldName[8];
-
}
-
if ((defined $opt{M}) || (defined $opt{a}))
-
{
-
$InterfaceOID{$FieldName[9]}=$InterfaceOID_orgin{$FieldName[9]};
-
$InterfaceOID{$FieldName[10]}=$InterfaceOID_orgin{$FieldName[10]};
-
$SortName{"MI"}=$FieldName[9];
-
$SortName{"MO"}=$FieldName[10];
-
}
-
if ((defined $opt{B}) || (defined $opt{a}))
-
{
-
$InterfaceOID{$FieldName[11]}=$InterfaceOID_orgin{$FieldName[11]};
-
$InterfaceOID{$FieldName[12]}=$InterfaceOID_orgin{$FieldName[12]};
-
$SortName{"BI"}=$FieldName[11];
-
$SortName{"BO"}=$FieldName[12];
-
}
-
if ((defined $opt{b}) || (defined $opt{a}))
-
{
-
$InterfaceOID{$FieldName[13]}=$InterfaceOID_orgin{$FieldName[13]};
-
$InterfaceOID{$FieldName[14]}=$InterfaceOID_orgin{$FieldName[14]};
-
$SortName{"bI"}=$FieldName[13];
-
$SortName{"bO"}=$FieldName[14];
-
}
-
if ((defined $opt{e}) || (defined $opt{a}))
-
{
-
$InterfaceOID{$FieldName[15]}=$InterfaceOID_orgin{$FieldName[15]};
-
$InterfaceOID{$FieldName[16]}=$InterfaceOID_orgin{$FieldName[16]};
-
$SortName{"eI"}=$FieldName[15];
-
$SortName{"eO"}=$FieldName[16];
-
}
-
if ((defined $opt{C}) || (defined $opt{a}))
-
{
-
$InterfaceOID{$FieldName[17]}=$InterfaceOID_orgin{$FieldName[17]};
-
$SortName{"C"}=$FieldName[17];
-
}
-
if ((defined $opt{t}) || (defined $opt{a}))
-
{
-
$InterfaceOID{$FieldName[18]}=$InterfaceOID_orgin{$FieldName[18]};
-
$SortName{"t"}=$FieldName[18];
-
}
-
if ((defined $opt{D}) || (defined $opt{a}))
-
{
-
$InterfaceOID{$FieldName[19]}=$InterfaceOID_orgin{$FieldName[19]};
-
$SortName{"D"}=$FieldName[19];
-
}
-
if ((defined $opt{o}) || (defined $opt{a}))
-
{
-
$InterfaceOID{$FieldName[20]}=$InterfaceOID_orgin{$FieldName[20]};
-
$SortName{"o"}=$FieldName[20];
-
}
-
-
if (defined $opt{s})
-
{
-
if (exists $SortName{$opt{s}})
-
{
-
$SortTable = $opt{s};
-
}
-
else
-
{
-
print "\n***********************Error*************************************\n\n";
-
my @aabb = keys(%SortName);
-
print "Options 's' valus is @aabb\n\n";
-
usage();
-
exit 0;
-
}
-
}
-
if (defined $opt{S})
-
{
-
-
if (($opt{S} eq "0") || ($opt{S} eq "1") )
-
{
-
$SortType = $opt{S};
-
}
-
else
-
{
-
print "\n***********************Error*************************************\n\n";
-
print "Options 'S' value is 0 or 1 only\n";
-
usage();
-
exit(0);
-
}
-
}
-
-
#if (defined $opt{d})
-
#{
-
# print this interface list what it'sline proctocol is down
-
#}
-
-
#test code
-
#method:1
-
my $index;
-
my $value;
-
while (($index,$value) = each(%InterfaceOID))
-
{
-
print "$index : $value\n";
-
}
-
#Method 2
-
#my @test = values(%InterfaceOID);
-
#print "OID-List:@test\n";
-
#
-
#print "Option s value(SortTable):$SortTable\n";
-
#print "Option S value(SortType):$SortType\n";
-
#my @aabb = keys(%SortName);
-
#printf "SortOption: @aabb\n";
-
-
#-------------------------End-----------------------------------------
-
-
# Create the SNMP session
-
my ($session, $error) = Net::SNMP->session(
-
-community => $community,
-
-hostname => $ipaddress,
-
-port => $port,
-
-version => $version,
-
-timeout => $timeout
-
);
-
if (!defined $session)
-
{
-
printf "ERROR: %s.\n", $error;
-
exit 1;
-
}
-
-
main();
-
-
$session->close(); # Close the session
-
exit 0; # exit this program
-
-
####################################################################
-
####################################################################
-
# sub program #
-
####################################################################
-
####################################################################
-
sub main()
-
{
-
-
#######################################################################################
-
#get interface ifIndex
-
#######################################################################################
-
-
#step1: get ifIndex of all interface.
-
my $OID_ifTable = '1.3.6.1.2.1.2.2.1.1';
-
my $result = $session->get_table( -baseoid => $OID_ifTable );
-
if (!defined $result) {
-
printf "ERROR: %s\n", $session->error();
-
$session->close();
-
exit 1;
-
}
-
my @InterFaceIndex;
-
foreach ($session->var_bind_names())
-
{
-
if (oid_base_match($OID_ifTable,$_))
-
{
-
my $v=$session->var_bind_list()->{$_};
-
push @InterFaceIndex ,$v;
-
}
-
}
-
#print "@InterFaceIndex\n";
-
-
#step2:choice interface what this status up and ifType 6/53 and routeVLAN.
-
my $IfIndex;
-
my $OIDIfName;
-
my $OIDIfType;
-
my $OIDIfStatus;
-
my $reIfName;
-
my $reIfType;
-
my $reIfStatus;
-
my @InterFaceIndexView; #it's crucial option.
-
#print "OIDIfName, reIfName, reIfType, reIfStatus\n";
-
foreach $IfIndex(@InterFaceIndex)
-
{
-
$OIDIfName="1.3.6.1.2.1.2.2.1.2.$IfIndex";
-
$OIDIfType="1.3.6.1.2.1.2.2.1.3.$IfIndex";
-
$OIDIfStatus="1.3.6.1.2.1.2.2.1.8.$IfIndex";
-
#print "$IfIndex,$OIDIfName,$OIDIfType,$OIDIfStatus\n";
-
my $re = $session->get_request(-varbindlist=>[$OIDIfName,$OIDIfType,$OIDIfStatus]);
-
$reIfName = $session->var_bind_list()->{$OIDIfName};
-
$reIfType = $session->var_bind_list()->{$OIDIfType};
-
$reIfStatus = $session->var_bind_list()->{$OIDIfStatus};
-
my $L2VLAN = substr($reIfName,0,8);
-
if (defined $opt{d})
-
{
-
#print this interface list what it'sline proctocol is down
-
if ( ($reIfStatus eq "2") && ( ($reIfType eq "6") || ($reIfType eq "53") ) && ($L2VLAN ne "unrouted") )
-
{
-
#print "$OIDIfName, $reIfName, $reIfType, $reIfStatus\n";
-
push @InterFaceIndexView ,$IfIndex;
-
}
-
}
-
else
-
{
-
#print this interface list what it'sline proctocol is up
-
if ( ($reIfStatus eq "1") && ( ($reIfType eq "6") || ($reIfType eq "53") ) && ($L2VLAN ne "unrouted") )
-
{
-
#print "$OIDIfName, $reIfName, $reIfType, $reIfStatus\n";
-
push @InterFaceIndexView ,$IfIndex;
-
}
-
}
-
}
-
#foreach (@InterFaceIndexView)
-
#{ print "$_\n";}
-
-
#######################################################################################
-
#create interface info database.
-
#######################################################################################
-
my @InterfaceOIDList;
-
my $tempIfIndex;
-
my %tbNIC; #it's crucial option.
-
foreach $tempIfIndex(@InterFaceIndexView)
-
{
-
@InterfaceOIDList = map($_ . "." . $tempIfIndex,values(%InterfaceOID));
-
#print "@InterfaceOIDList\n";
-
my $re = $session->get_request(-varbindlist=>[@InterfaceOIDList]);
-
my $InterFace; #obj save Interface info.
-
my $OIDtp;
-
-
#set obj->{"Name"}
-
#$OIDtp = $InterfaceOID{$FieldName[0]}.".".$tempIfIndex;
-
#$InterFace -> {$FieldName[0]} = $session->var_bind_list()->{$OIDtp};
-
my $keyID;
-
foreach $keyID (keys(%InterfaceOID))
-
{
-
$OIDtp = $InterfaceOID{$keyID}.".".$tempIfIndex;
-
$InterFace -> {$keyID} = $session->var_bind_list()->{$OIDtp};
-
}
-
-
#save obj to %tbNIC
-
$tbNIC{$tempIfIndex} = $InterFace;
-
}
-
-
#view : Method 1
-
my $index;
-
my $obj;
-
my $str;
-
my @FliedGroup = values(%SortName);
-
my $Tile="Index\t" . $FieldName[0] . "\t" ;
-
foreach (@FliedGroup)
-
{
-
$Tile = $Tile . "\t" . $_ ;
-
}
-
print "$Tile\n";
-
-
if ($SortTable eq "n")
-
{
-
#sort by "Index"
-
my $Key;
-
-
sub asc_sort
-
{
-
$a <=> $b;
-
}
-
-
foreach $Key (sort asc_sort(keys(%tbNIC)))
-
{
-
$str = "$Key\t" . $tbNIC{$Key}->{$FieldName[0]};
-
foreach (@FliedGroup)
-
{
-
$str = $str . "\t" . $tbNIC{$Key}->{$_};
-
}
-
print "$str\n";
-
}
-
}
-
else
-
{
-
#sort
-
#step 1 : create sort index table
-
#Process
-
# 1. find SortObj from $SortTable.
-
# 2. find Sort Field from %SortName by $SortTable
-
# 3. crate index table from %tbNIC with ifIndex and sortField;
-
# 4. sort and print
-
my $SortTBName = $SortName{$SortTable};
-
#print $SortTBName,"\n";
-
my %dbIndex;
-
my $Ifindex;
-
my $SortFileNameTP;
-
while (($Ifindex,$SortFileNameTP) = each(%tbNIC))
-
{
-
$dbIndex{$Ifindex} = $SortFileNameTP->{$SortTBName};
-
}
-
-
sub asc_sort_0
-
{
-
$dbIndex{$a} <=> $dbIndex{$b};
-
}
-
-
sub asc_sort_1
-
{
-
$dbIndex{$b} <=> $dbIndex{$a};
-
}
-
-
my $Key;
-
my $Value;
-
if ($SortType)
-
{
-
foreach $Key (sort asc_sort_1(keys(%dbIndex)))
-
{
-
$str = "$Key\t" . $tbNIC{$Key}->{$FieldName[0]};
-
foreach (@FliedGroup)
-
{
-
$str = $str . "\t" . $tbNIC{$Key}->{$_};
-
}
-
print "$str\n";
-
}
-
}
-
else
-
{
-
foreach $Key (sort asc_sort_0(keys(%dbIndex)))
-
{
-
$str = "$Key\t" . $tbNIC{$Key}->{$FieldName[0]};
-
foreach (@FliedGroup)
-
{
-
$str = $str . "\t" . $tbNIC{$Key}->{$_};
-
}
-
print "$str\n";
-
}
-
-
}
-
}
-
#print other info
-
print "$Tile\n";
-
my $index;
-
my $value;
-
while (($index,$value) = each(%InterfaceOID))
-
{
-
print "$index : $value\n";
-
}
-
#----------------------------------End-------------------------------------
-
}
-
-
-
-
sub usage {
-
print << "USAGE";
-
-
--------------------------------------------------------------------
-
$script v$script_version
-
-
View Interface status with SNMP Protocol on Cisco Switch and Route devices.
-
-
Note: not support serial interface.
-
-
-
Usage: $script Remote-Option [OID-Option] [Sort-Option] [Other-Option]
-
-
Remote-Options:
-
-H Hostname or IP address
-
-c Community (default is public)
-
-
OID-Option
-
-m $FieldName[1] $InterfaceOID_orgin{$FieldName[1]}
-
$FieldName[2] $InterfaceOID_orgin{$FieldName[2]}
-
-
-r $FieldName[3] $InterfaceOID_orgin{$FieldName[3]}
-
$FieldName[4] $InterfaceOID_orgin{$FieldName[4]}
-
-
-R $FieldName[5] $InterfaceOID_orgin{$FieldName[5]}
-
$FieldName[6] $InterfaceOID_orgin{$FieldName[6]}
-
-
-P $FieldName[7] $InterfaceOID_orgin{$FieldName[7]}
-
$FieldName[8] $InterfaceOID_orgin{$FieldName[8]}
-
-
-M $FieldName[9] $InterfaceOID_orgin{$FieldName[9]}
-
$FieldName[10] $InterfaceOID_orgin{$FieldName[10]}
-
-
-B $FieldName[11] $InterfaceOID_orgin{$FieldName[11]}
-
$FieldName[12] $InterfaceOID_orgin{$FieldName[12]}
-
-
-b $FieldName[13] $InterfaceOID_orgin{$FieldName[13]}
-
$FieldName[14] $InterfaceOID_orgin{$FieldName[14]}
-
-
-e $FieldName[15] $InterfaceOID_orgin{$FieldName[15]}
-
$FieldName[16] $InterfaceOID_orgin{$FieldName[16]}
-
-
-C $FieldName[17] $InterfaceOID_orgin{$FieldName[17]}
-
-
-t $FieldName[18] $InterfaceOID_orgin{$FieldName[18]}
-
-
-D $FieldName[19] $InterfaceOID_orgin{$FieldName[19]}
-
-
-o $FieldName[20] $InterfaceOID_orgin{$FieldName[20]}
-
-
-a All
-
-
Sort-Option
-
-s Only OID-Option char,more detail as below.
-
-
chars Sort-Field
-
---- -------------
-
mI $FieldName[1]
-
mO $FieldName[2]
-
rI $FieldName[3]
-
rO $FieldName[4]
-
RI $FieldName[5]
-
RO $FieldName[6]
-
PI $FieldName[7]
-
PO $FieldName[8]
-
MI $FieldName[9]
-
MO $FieldName[10]
-
BI $FieldName[11]
-
BO $FieldName[12]
-
bI $FieldName[13]
-
bO $FieldName[14]
-
eI $FieldName[15]
-
eO $FieldName[16]
-
C $FieldName[17]
-
t $FieldName[18]
-
D $FieldName[19]
-
o $FieldName[20]
-
-
-S 0 or 1 0 increase,1 decrease
-
-
-d view this interface what it's line proctocol is down.
-
default view up interface.
-
-
Other-Option
-
-h or -v Help script Usage
-
-
--------------------------------------------------------------------
-
-
This program is free software; you can redistribute it or modify
-
it under the terms of the GNU General Public License
-
--------------------------------------------------------------------
-
-
USAGE
-
exit 1;
-
}
-
-
-
sub GetScriptName
-
{
-
my @str = @_;
-
my $len = rindex($str[0],"/");
-
return substr($str[0],$len + 1);
-
}
安装:
将上面的代码用vi保存为view-nic,并设置可执行权限.
使用帮助:
#view-nic -v
阅读(3127) | 评论(0) | 转发(0) |