Chinaunix首页 | 论坛 | 博客
  • 博客访问: 182377
  • 博文数量: 16
  • 博客积分: 170
  • 博客等级: 入伍新兵
  • 技术积分: 753
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 10:57
文章分类

全部博文(16)

文章存档

2017年(3)

2016年(2)

2015年(3)

2014年(1)

2013年(2)

2012年(5)

我的朋友

分类: 网络与安全

2012-04-19 10:17:13

背景
 
    在日常维护cisco设备时,常用到"sh int counters"命令,但该命令不提供排序功能,不太方便.
    于是采用perl写了一个类似的脚本,可以帮忙对交换机接口状态一目了然,且支持排序.
 
接口参数
    该脚本主要是对如下参数进行采集和排序.

点击(此处)折叠或打开

  1. #sh int g1/0/1
  2. GigabitEthernet1/0/1 is up, line protocol is up (connected)
  3.         | | |
  4.         | | +--------------------------------------1.3.6.1.4.1.9.2.2.1.1.2
  5.         | +------------------------------------------------------------1.3.6.1.2.1.2.2.1.8
  6.         +---------------------------------------------------------------------------+--Index:1.3.6.1.2.1.2.2.1.1
  7.                                                                                     +--Type:1.3.6.1.2.1.2.2.1.3
  8.                                                                                     +--Name:1.3.6.1.2.1.2.2.1.2
  9.                                                                                     +--Name:1.3.6.1.2.1.31.1.1.1.1
  10.   Hardware is Gigabit Ethernet, address is ec30.91ca.3481 (bia ec30.91ca.3481)
  11.   Description: link-cisco4507r
  12.   MTU 1500 bytes, BW 1000000 Kbit, DLY 10 usec,
  13.      reliability 255/255, txload 6/255, rxload 2/255
  14.   Encapsulation ARPA, loopback not set
  15.   Keepalive not set
  16.   Full-duplex, 1000Mb/s, link type is auto, media type is 1000BaseSX SFP
  17.   input flow-control is off, output flow-control is unsupported
  18.   ARP type: ARPA, ARP Timeout 04:00:00
  19.   Last input 00:00:00, output 00:00:00, output hang never
  20.   Last clearing of "show interface" counters never
  21.   Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 1416375
  22.                                                                         |
  23.                                                                         +------------1.3.6.1.2.1.2.2.1.19
  24.                                                                                      或1.3.6.1.4.1.9.2.2.1.1.27
  25.   Queueing strategy: fifo
  26.   Output queue: 0/0 (size/max)
  27.   5 minute input rate 9130000 bits/sec, 3706 packets/sec
  28.                          | |
  29.                          | +-------------------------+-----5分钟平均值:1.3.6.1.4.1.9.2.2.1.1.7
  30.                          | +-----即时值(毫秒级):1.3.6.1.4.1.9.2.2.1.1.3
  31.                          +-------------------------------------------5分钟平均值:1.3.6.1.4.1.9.2.2.1.1.6
  32.   5 minute output rate 25827000 bits/sec, 3713 packets/sec
  33.                          | |
  34.                          | +-------------------------+-----5分钟平均值:1.3.6.1.4.1.9.2.2.1.1.9
  35.                          | +-----即时值(毫秒级):1.3.6.1.4.1.9.2.2.1.1.4
  36.                          +-------------------------------------------5分钟平均值:1.3.6.1.4.1.9.2.2.1.1.8
  37.      5588544812 packets input, 1594789568486 bytes, 0 no buffer
  38.          | |
  39.          | +---------------------------------1.3.6.1.2.1.31.1.1.1.6
  40.          +-----------------------------------------------------------单播+广播+组播,其中单播的OID是:1.3.6.1.2.1.2.2.1.11
  41.      Received 32813253 broadcasts (31962960 multicasts)
  42.                   | |
  43.                   | +------------------------------组播:1.3.6.1.2.1.31.1.1.1.8
  44.                   +--------------------------------------------------广播+组播,广播的OID是:1.3.6.1.2.1.31.1.1.1.9
  45.      23 runts, 0 giants, 0 throttles
  46.      23 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
  47.       | |
  48.       | +--------------------------------------------1.3.6.1.4.1.9.2.2.1.1.12
  49.       +-------------------------------------------------------------1.3.6.1.2.1.2.2.1.14
  50.      0 watchdog, 31962960 multicast, 0 pause input
  51.      0 input packets with dribble condition detected
  52.      4979391462 packets output, 3499294634681 bytes, 0 underruns
  53.          | |
  54.          | +--------------------------------1.3.6.1.2.1.31.1.1.1.10
  55.          +----------------------------------------------------------单播+组播+广播
  56.                                                                     单播:1.3.6.1.2.1.31.1.1.1.11或1.3.6.1.2.1.2.2.1.17
  57.                                                                     组播:1.3.6.1.2.1.31.1.1.1.12
  58.                                                                     广播:1.3.6.1.2.1.31.1.1.1.13
  59.      0 output errors, 0 collisions, 1 interface resets
  60.      | | |
  61.      | | +-----------------------------1.3.6.1.4.1.9.2.2.1.1.17
  62.      | +-------------------------------------------1.3.6.1.4.1.9.2.2.1.1.25
  63.      +--------------------------------------------------------------1.3.6.1.2.1.2.2.1.20
  64.      0 babbles, 0 late collision, 0 deferred
  65.      0 lost carrier, 0 no carrier, 0 PAUSE output
  66.      0 output buffer failures, 0 output buffers swapped out


 

使用方式:


 

点击(此处)折叠或打开

  1. Usage: view-nic Remote-Option [OID-Option] [Sort-Option] [Other-Option]

  2. Note:只支持cisco设备,不支持串口

  3. Remote-Options:
  4.          -H <hostname> 交换机管理IP
  5.          -c <community> SNMP的Community字串,默认为public

  6. OID-Option
  7.         -m "InputRatepbm"=>"1.3.6.1.4.1.9.2.2.1.1.3" 每毫秒Input方面的包数量
  8.             "OutputRatepbm"=>"1.3.6.1.4.1.9.2.2.1.1.4" 每毫秒Output方面的包数量

  9.         -r "Input5minRatebps"=>"1.3.6.1.4.1.9.2.2.1.1.6" 每5分钟Input方面的字节数量
  10.              "Out5minRatebps"=>"1.3.6.1.4.1.9.2.2.1.1.8" 每5分钟Output方面的字节数量

  11.         -R "Input5minRatepbs"=>"1.3.6.1.4.1.9.2.2.1.1.7" 每5分钟Input方面的包数量
  12.              "Out5minRatepbs"=>"1.3.6.1.4.1.9.2.2.1.1.9" 每5分钟Output方面的包数量

  13.         -P "InputUcastPkts"=>"1.3.6.1.2.1.2.2.1.11" Input方向单播数量
  14.              "OutputUcastPkts"=>"1.3.6.1.2.1.31.1.1.1.11" Output方向单播数量

  15.         -M "InputMcastPkts"=>"1.3.6.1.2.1.31.1.1.1.8" Input方向组播数量
  16.              "OutputMcastPkts"=>"1.3.6.1.2.1.31.1.1.1.12" Output方向组播数量

  17.         -B "InputBcastPkts"=>"1.3.6.1.2.1.31.1.1.1.9" Input方向广播数量
  18.              "OutputBcastPkts"=>"1.3.6.1.2.1.31.1.1.1.13" Output方向广播数量

  19.         -b "InputBytes"=>"1.3.6.1.2.1.2.2.1.10" Input方向单播数量
  20.              "OutputBytes"=>"1.3.6.1.2.1.2.2.1.16" Output方向单播数量

  21.         -e "InputError"=>"1.3.6.1.2.1.2.2.1.14" Input方向错包数量
  22.              "OutputError"=>"1.3.6.1.2.1.2.2.1.20" Output方向错包数量

  23.         -C "InputCRC"=>"1.3.6.1.4.1.9.2.2.1.1.12" Input方向的CRC数量

  24.         -t "OutputResets"=>"1.3.6.1.4.1.9.2.2.1.1.17" Output方向的interface resets数量

  25.         -D "OutputDrop"=>"1.3.6.1.2.1.2.2.1.19" Output方向的丢包数量

  26.         -a All 选中上面所有参数

  27. Sort-Option
  28.          -s <char> Only OID-Option char,more detail as below.
  29.                                 
  30.                                 参数 排序档位
  31.                                 ---- -------------
  32.                                 mI InputRatepbm
  33.                                 mO OutputRatepbm
  34.                                 rI Input5minRatebps
  35.                                 rO Out5minRatebps
  36.                                 RI Input5minRatepbs
  37.                                 RO Out5minRatepbs
  38.                                 MI InputMcastPkts
  39.                                 MO OutputMcastPkts
  40.                                 BI InputBcastPkts
  41.                                 BO OutputBcastPkts
  42.                                 bI InputBytes
  43.                                 bO OutputBytes
  44.                                 eI InputError
  45.                                 eO OutputError
  46.                                 C InputCRC
  47.                                 t OutputResets
  48.                                 D OutputDrop

  49.          -S 0 or 1 0 递增,1 递减

  50. Other-Option
  51.          -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

 


源代码


点击(此处)折叠或打开

  1. #!/usr/bin/perl
  2. # Version : 0.1
  3. # Date : 2012-04-18
  4. # Licence : GPL - http://www.fsf.org/licenses/gpl.txt

  5. use strict;
  6. use Net::SNMP qw{ :asn1 :snmp :translate };
  7. use Getopt::Std;

  8. my $script = GetScriptName($0);
  9. my $script_version = "0.1";


  10. my $ipaddress = "127.0.0.1";
  11. my $port = 161;
  12. my $version = "snmpv2c";
  13. my $community = "public";
  14. my $timeout = 2;
  15. my $status = 0;

  16. my @FieldName = ( #it's crucial option
  17.     "Name", #0
  18.     "InputRatepbm", #1
  19.     "OutputRatepbm", #2
  20.     "Input5minbps", #3
  21.     "Out5minbps", #4
  22.     "Input5minpbs", #5
  23.     "Out5minpbs", #6
  24.     "InputUcastPkts", #7
  25.     "OutputUcastPkts", #8
  26.     "InputMcastPkts", #9
  27.     "OutputMcastPkts", #10
  28.     "InputBcastPkts", #11
  29.     "OutputBcastPkts", #12
  30.     "InputBytes", #13
  31.     "OutputBytes", #14
  32.     "InputError", #15
  33.     "OutputError", #16
  34.     "InputCRC", #17
  35.     "OutputResets", #18
  36.     "OutputDrop", #19
  37.     "Outcollisions" #20
  38. );

  39. my %InterfaceOID_orgin = ( #it's crucial option view-Option sort-char
  40.      $FieldName[0] => "1.3.6.1.2.1.31.1.1.1.1", #0 "Name" n/a n/a
  41.      $FieldName[1] => "1.3.6.1.4.1.9.2.2.1.1.3", #1 "InputRatepbm" m or a mI
  42.      $FieldName[2] => "1.3.6.1.4.1.9.2.2.1.1.4", #2 "OutputRatepbm" m or a mO
  43.      $FieldName[3] => "1.3.6.1.4.1.9.2.2.1.1.6", #3 "Input5minRatebps" r or a rI
  44.      $FieldName[4] => "1.3.6.1.4.1.9.2.2.1.1.8", #4 "Out5minRatebps" r or a rO
  45.      $FieldName[5] => "1.3.6.1.4.1.9.2.2.1.1.7", #5 "Input5minRatepbs" R or a RI
  46.      $FieldName[6] => "1.3.6.1.4.1.9.2.2.1.1.9", #6 "Out5minRatepbs" R or a RO
  47.      $FieldName[7] => "1.3.6.1.2.1.2.2.1.11", #7 "InputUcastPkts" P or a PI
  48.      $FieldName[8] => "1.3.6.1.2.1.31.1.1.1.11", #8 "OutputUcastPkts" P or a PO
  49.      $FieldName[9] => "1.3.6.1.2.1.31.1.1.1.8", #9 "InputMcastPkts" M or a MI
  50.      $FieldName[10] => "1.3.6.1.2.1.31.1.1.1.12", #10 "OutputMcastPkts" M or a MO
  51.      $FieldName[11] => "1.3.6.1.2.1.31.1.1.1.9", #11 "InputBcastPkts" B or a BI
  52.      $FieldName[12] => "1.3.6.1.2.1.31.1.1.1.13", #12 "OutputBcastPkts" B or a BO
  53.      $FieldName[13] => "1.3.6.1.2.1.31.1.1.1.6", #13 "InputBytes" b or a bI
  54.      $FieldName[14] => "1.3.6.1.2.1.31.1.1.1.10", #14 "OutputBytes" b or a bO
  55.      $FieldName[15] => "1.3.6.1.2.1.2.2.1.14", #15 "InputError" e or a eI
  56.      $FieldName[16] => "1.3.6.1.2.1.2.2.1.20", #16 "OutputError" e or a eO
  57.      $FieldName[17] => "1.3.6.1.4.1.9.2.2.1.1.12", #17 "InputCRC" C or a C
  58.      $FieldName[18] => "1.3.6.1.4.1.9.2.2.1.1.17", #18 "OutputResets" t or a t
  59.      $FieldName[19] => "1.3.6.1.2.1.2.2.1.19", #19 "OutputDrop" D or a D
  60.      $FieldName[20] => "1.3.6.1.4.1.9.2.2.1.1.25" #20 "Outcollisions" o or a o
  61. );


  62. my %InterfaceOID; #it's crucial option.


  63. my $SortTable="n"; #defulte sort table is "Name",it's member of @SortName. it's crucial option.
  64. my $SortType=0; #0 increase,1 decrease ,it's crucial option.
  65. my %SortName; #only n,m,r,R,P,M,B,b,e,C,t,D. it's crucial option.
  66. #my %SortName = (
  67. #achars Sort-Field
  68. #---- -------------
  69. #"mI"=>$FieldName[1], #InputRatepbm
  70. #"mO"=>$FieldName[2], #OutputRatepbm
  71. #"rI"=>$FieldName[3], #Input5minRatebps
  72. #"rO"=>$FieldName[4], #Out5minRatebps
  73. #"RI"=>$FieldName[5], #Input5minRatepbs
  74. #"RO"=>$FieldName[6], #Out5minRatepbs,
  75. #"PI"=>$FieldName[7] #"InputUcastPkts",
  76. #"PO"=>$FieldName[8] #"OutputUcastPkts",
  77. #"MI"=>$FieldName[9], #"InputMcastPkts",
  78. #"MO"=>$FieldName[10], #"OutputMcastPkts",
  79. #"BI"=>$FieldName[11], #"InputBcastPkts",
  80. #"BO"=>$FieldName[12], #"OutputBcastPkts",
  81. #"bI"=>$FieldName[13], #"InputBytes",
  82. #"bO"=>$FieldName[14], #"OutputBytes",
  83. #"eI"=>$FieldName[15], #"InputError",
  84. #"eO"=>$FieldName[16], #"OutputError",
  85. #"C"=>$FieldName[17], #"InputCRC",
  86. #"t"=>$FieldName[18], #"OutputResets",
  87. #"D"=>$FieldName[19], #"OutputDrop",
  88. #"o"=>$FieldName[20] #"Outcollisions"
  89. #);



  90. #my $InterfaceObj = { #OID List , it's crucial option.
  91. # "Name"=>"Fa1/0/1", #1.3.6.1.2.1.31.1.1.1.1
  92. # "InputRatepbm"=>"234", #1.3.6.1.4.1.9.2.2.1.1.3
  93. # "Input5minRatebps"=>"123", #1.3.6.1.4.1.9.2.2.1.1.6
  94. # "Input5minRatepbs"=>"324", #1.3.6.1.4.1.9.2.2.1.1.7
  95. # "InputUcastPkts"=>"1", #1.3.6.1.2.1.2.2.1.11
  96. # "InputMcastPkts"=>"1", #1.3.6.1.2.1.31.1.1.1.8
  97. # "InputBcastPkts"=>"1", #1.3.6.1.2.1.31.1.1.1.9
  98. # "InputBytes"=>"1", #1.3.6.1.2.1.2.2.1.10
  99. # "InputError"=>"1", #1.3.6.1.2.1.2.2.1.14
  100. # "InputCRC"=>"1", #1.3.6.1.4.1.9.2.2.1.1.12
  101. # "OutputRatepbm"=>"234", #1.3.6.1.4.1.9.2.2.1.1.4
  102. # "Out5minRatebps"=>"123", #1.3.6.1.4.1.9.2.2.1.1.8
  103. # "Out5minRatepbs"=>"324", #1.3.6.1.4.1.9.2.2.1.1.9
  104. # "OutputUcastPkts"=>"1", #1.3.6.1.2.1.31.1.1.1.11
  105. # "OutputMcastPkts"=>"1", #1.3.6.1.2.1.31.1.1.1.12
  106. # "OutputBcastPkts"=>"1", #1.3.6.1.2.1.31.1.1.1.13
  107. # "OutputBytes"=>"1", #1.3.6.1.2.1.2.2.1.16
  108. # "OutputError"=>"1", #1.3.6.1.2.1.2.2.1.20
  109. # "OutputResets"=>"1", #1.3.6.1.4.1.9.2.2.1.1.17
  110. # "OutputDrop"=>"321" #1.3.6.1.2.1.2.2.1.19
  111. # "Outcollisions"=>"34" #1.3.6.1.4.1.9.2.2.1.1.25
  112. #};

  113. #my %tbNIC = (
  114. # "10101" => $InterfaceObj-1,
  115. # "10102" => $InterfaceObj-2,
  116. # "10103" => $InterfaceObj-3,
  117. # ...
  118. # "90101" => $InterfaceObj-x
  119. #)



  120. if (@ARGV < 1) {
  121.      print "Too few arguments\n";
  122.      usage();
  123. }


  124. #######################################################################################
  125. #get script option
  126. #######################################################################################
  127. my %opt=();
  128. getopts("hvH:c:mrRPMBbeCtDs:S:aod",\%opt);

  129. if($opt{h} || $opt{v})
  130. {
  131.     usage();
  132.     exit(0);
  133. }

  134. $ipaddress = $opt{H} if defined $opt{H} ;
  135. $community = $opt{c} if defined $opt{c} ;

  136. $InterfaceOID{$FieldName[0]} = $InterfaceOID_orgin{$FieldName[0]};
  137. if ((defined $opt{m}) || (defined $opt{a}))
  138. {
  139.    $InterfaceOID{$FieldName[1]}=$InterfaceOID_orgin{$FieldName[1]};
  140.    $InterfaceOID{$FieldName[2]}=$InterfaceOID_orgin{$FieldName[2]};
  141.    $SortName{"mI"}=$FieldName[1];
  142.    $SortName{"mO"}=$FieldName[2];
  143. }
  144. if ((defined $opt{r}) || (defined $opt{a}))
  145. {
  146.    $InterfaceOID{$FieldName[3]}=$InterfaceOID_orgin{$FieldName[3]};
  147.    $InterfaceOID{$FieldName[4]}=$InterfaceOID_orgin{$FieldName[4]};
  148.    $SortName{"rI"}=$FieldName[3];
  149.    $SortName{"rO"}=$FieldName[4];
  150. }
  151. if ((defined $opt{R}) || (defined $opt{a}))
  152. {
  153.    $InterfaceOID{$FieldName[5]}=$InterfaceOID_orgin{$FieldName[5]};
  154.    $InterfaceOID{$FieldName[6]}=$InterfaceOID_orgin{$FieldName[6]};
  155.    $SortName{"RI"}=$FieldName[5];
  156.    $SortName{"RO"}=$FieldName[6];
  157. }
  158. if ((defined $opt{P}) || (defined $opt{a}))
  159. {
  160.    $InterfaceOID{$FieldName[7]}=$InterfaceOID_orgin{$FieldName[7]};
  161.    $InterfaceOID{$FieldName[8]}=$InterfaceOID_orgin{$FieldName[8]};
  162.    $SortName{"PI"}=$FieldName[7];
  163.    $SortName{"PO"}=$FieldName[8];
  164. }
  165. if ((defined $opt{M}) || (defined $opt{a}))
  166. {
  167.    $InterfaceOID{$FieldName[9]}=$InterfaceOID_orgin{$FieldName[9]};
  168.    $InterfaceOID{$FieldName[10]}=$InterfaceOID_orgin{$FieldName[10]};
  169.    $SortName{"MI"}=$FieldName[9];
  170.    $SortName{"MO"}=$FieldName[10];
  171. }
  172. if ((defined $opt{B}) || (defined $opt{a}))
  173. {
  174.    $InterfaceOID{$FieldName[11]}=$InterfaceOID_orgin{$FieldName[11]};
  175.    $InterfaceOID{$FieldName[12]}=$InterfaceOID_orgin{$FieldName[12]};
  176.    $SortName{"BI"}=$FieldName[11];
  177.    $SortName{"BO"}=$FieldName[12];
  178. }
  179. if ((defined $opt{b}) || (defined $opt{a}))
  180. {
  181.    $InterfaceOID{$FieldName[13]}=$InterfaceOID_orgin{$FieldName[13]};
  182.    $InterfaceOID{$FieldName[14]}=$InterfaceOID_orgin{$FieldName[14]};
  183.    $SortName{"bI"}=$FieldName[13];
  184.    $SortName{"bO"}=$FieldName[14];
  185. }
  186. if ((defined $opt{e}) || (defined $opt{a}))
  187. {
  188.    $InterfaceOID{$FieldName[15]}=$InterfaceOID_orgin{$FieldName[15]};
  189.    $InterfaceOID{$FieldName[16]}=$InterfaceOID_orgin{$FieldName[16]};
  190.    $SortName{"eI"}=$FieldName[15];
  191.    $SortName{"eO"}=$FieldName[16];
  192. }
  193. if ((defined $opt{C}) || (defined $opt{a}))
  194. {
  195.    $InterfaceOID{$FieldName[17]}=$InterfaceOID_orgin{$FieldName[17]};
  196.    $SortName{"C"}=$FieldName[17];
  197. }
  198. if ((defined $opt{t}) || (defined $opt{a}))
  199. {
  200.    $InterfaceOID{$FieldName[18]}=$InterfaceOID_orgin{$FieldName[18]};
  201.    $SortName{"t"}=$FieldName[18];
  202. }
  203. if ((defined $opt{D}) || (defined $opt{a}))
  204. {
  205.    $InterfaceOID{$FieldName[19]}=$InterfaceOID_orgin{$FieldName[19]};
  206.    $SortName{"D"}=$FieldName[19];
  207. }
  208. if ((defined $opt{o}) || (defined $opt{a}))
  209. {
  210.    $InterfaceOID{$FieldName[20]}=$InterfaceOID_orgin{$FieldName[20]};
  211.    $SortName{"o"}=$FieldName[20];
  212. }

  213. if (defined $opt{s})
  214. {
  215.    if (exists $SortName{$opt{s}})
  216.    {
  217.       $SortTable = $opt{s};
  218.    }
  219.    else
  220.    {
  221.       print "\n***********************Error*************************************\n\n";
  222.       my @aabb = keys(%SortName);
  223.       print "Options 's' valus is @aabb\n\n";
  224.       usage();
  225.       exit 0;
  226.    }
  227. }
  228. if (defined $opt{S})
  229. {
  230.    
  231.    if (($opt{S} eq "0") || ($opt{S} eq "1") )
  232.    {
  233.       $SortType = $opt{S};
  234.    }
  235.    else
  236.    {
  237.       print "\n***********************Error*************************************\n\n";
  238.       print "Options 'S' value is 0 or 1 only\n";
  239.       usage();
  240.       exit(0);
  241.    }
  242. }

  243. #if (defined $opt{d})
  244. #{
  245. # print this interface list what it'sline proctocol is down
  246. #}

  247. #test code
  248. #method:1
  249. my $index;
  250. my $value;
  251. while (($index,$value) = each(%InterfaceOID))
  252. {
  253.    print "$index : $value\n";
  254. }
  255. #Method 2
  256. #my @test = values(%InterfaceOID);
  257. #print "OID-List:@test\n";
  258. #
  259. #print "Option s value(SortTable):$SortTable\n";
  260. #print "Option S value(SortType):$SortType\n";
  261. #my @aabb = keys(%SortName);
  262. #printf "SortOption: @aabb\n";

  263. #-------------------------End-----------------------------------------

  264. # Create the SNMP session
  265. my ($session, $error) = Net::SNMP->session(
  266.      -community => $community,
  267.      -hostname => $ipaddress,
  268.      -port => $port,
  269.      -version => $version,
  270.      -timeout => $timeout
  271. );
  272. if (!defined $session)
  273. {
  274.     printf "ERROR: %s.\n", $error;
  275.     exit 1;
  276. }

  277. main();

  278. $session->close(); # Close the session
  279. exit 0; # exit this program

  280. ####################################################################
  281. ####################################################################
  282. # sub program #
  283. ####################################################################
  284. ####################################################################
  285. sub main()
  286. {

  287.     #######################################################################################
  288.     #get interface ifIndex
  289.     #######################################################################################

  290.     #step1: get ifIndex of all interface.
  291.     my $OID_ifTable = '1.3.6.1.2.1.2.2.1.1';
  292.     my $result = $session->get_table( -baseoid => $OID_ifTable );
  293.     if (!defined $result) {
  294.        printf "ERROR: %s\n", $session->error();
  295.        $session->close();
  296.        exit 1;
  297.     }
  298.     my @InterFaceIndex;
  299.     foreach ($session->var_bind_names())
  300.     {
  301.        if (oid_base_match($OID_ifTable,$_))
  302.        {
  303.           my $v=$session->var_bind_list()->{$_};
  304.           push @InterFaceIndex ,$v;
  305.        }
  306.     }
  307.     #print "@InterFaceIndex\n";

  308.     #step2:choice interface what this status up and ifType 6/53 and routeVLAN.
  309.     my $IfIndex;
  310.     my $OIDIfName;
  311.     my $OIDIfType;
  312.     my $OIDIfStatus;
  313.     my $reIfName;
  314.     my $reIfType;
  315.     my $reIfStatus;
  316.     my @InterFaceIndexView; #it's crucial option.
  317.     #print "OIDIfName, reIfName, reIfType, reIfStatus\n";
  318.     foreach $IfIndex(@InterFaceIndex)
  319.     {
  320.        $OIDIfName="1.3.6.1.2.1.2.2.1.2.$IfIndex";
  321.        $OIDIfType="1.3.6.1.2.1.2.2.1.3.$IfIndex";
  322.        $OIDIfStatus="1.3.6.1.2.1.2.2.1.8.$IfIndex";
  323.        #print "$IfIndex,$OIDIfName,$OIDIfType,$OIDIfStatus\n";
  324.        my $re = $session->get_request(-varbindlist=>[$OIDIfName,$OIDIfType,$OIDIfStatus]);
  325.        $reIfName = $session->var_bind_list()->{$OIDIfName};
  326.        $reIfType = $session->var_bind_list()->{$OIDIfType};
  327.        $reIfStatus = $session->var_bind_list()->{$OIDIfStatus};
  328.        my $L2VLAN = substr($reIfName,0,8);
  329.        if (defined $opt{d})
  330.        {
  331.           #print this interface list what it'sline proctocol is down
  332.           if ( ($reIfStatus eq "2") && ( ($reIfType eq "6") || ($reIfType eq "53") ) && ($L2VLAN ne "unrouted") )
  333.           {
  334.              #print "$OIDIfName, $reIfName, $reIfType, $reIfStatus\n";
  335.              push @InterFaceIndexView ,$IfIndex;
  336.           }
  337.        }
  338.        else
  339.        {
  340.           #print this interface list what it'sline proctocol is up
  341.           if ( ($reIfStatus eq "1") && ( ($reIfType eq "6") || ($reIfType eq "53") ) && ($L2VLAN ne "unrouted") )
  342.           {
  343.              #print "$OIDIfName, $reIfName, $reIfType, $reIfStatus\n";
  344.              push @InterFaceIndexView ,$IfIndex;
  345.           }
  346.        }
  347.     }
  348.     #foreach (@InterFaceIndexView)
  349.     #{ print "$_\n";}

  350.     #######################################################################################
  351.     #create interface info database.
  352.     #######################################################################################
  353.     my @InterfaceOIDList;
  354.     my $tempIfIndex;
  355.     my %tbNIC; #it's crucial option.
  356.     foreach $tempIfIndex(@InterFaceIndexView)
  357.     {
  358.         @InterfaceOIDList = map($_ . "." . $tempIfIndex,values(%InterfaceOID));
  359.         #print "@InterfaceOIDList\n";
  360.         my $re = $session->get_request(-varbindlist=>[@InterfaceOIDList]);
  361.         my $InterFace; #obj save Interface info.
  362.         my $OIDtp;

  363.         #set obj->{"Name"}
  364.         #$OIDtp = $InterfaceOID{$FieldName[0]}.".".$tempIfIndex;
  365.         #$InterFace -> {$FieldName[0]} = $session->var_bind_list()->{$OIDtp};
  366.         my $keyID;
  367.         foreach $keyID (keys(%InterfaceOID))
  368.         {
  369.             $OIDtp = $InterfaceOID{$keyID}.".".$tempIfIndex;
  370.             $InterFace -> {$keyID} = $session->var_bind_list()->{$OIDtp};
  371.         }

  372.         #save obj to %tbNIC
  373.         $tbNIC{$tempIfIndex} = $InterFace;
  374.     }

  375.     #view : Method 1
  376.     my $index;
  377.     my $obj;
  378.     my $str;
  379.     my @FliedGroup = values(%SortName);
  380.     my $Tile="Index\t" . $FieldName[0] . "\t" ;
  381.     foreach (@FliedGroup)
  382.     {
  383.        $Tile = $Tile . "\t" . $_ ;
  384.     }
  385.     print "$Tile\n";

  386.     if ($SortTable eq "n")
  387.     {
  388.        #sort by "Index"
  389.        my $Key;

  390.        sub asc_sort
  391.        {
  392.            $a <=> $b;
  393.        }

  394.        foreach $Key (sort asc_sort(keys(%tbNIC)))
  395.        {
  396.            $str = "$Key\t" . $tbNIC{$Key}->{$FieldName[0]};
  397.            foreach (@FliedGroup)
  398.            {
  399.                $str = $str . "\t" . $tbNIC{$Key}->{$_};
  400.            }
  401.            print "$str\n";
  402.        }
  403.     }
  404.     else
  405.     {
  406.        #sort
  407.        #step 1 : create sort index table
  408.        #Process
  409.        # 1. find SortObj from $SortTable.
  410.        # 2. find Sort Field from %SortName by $SortTable
  411.        # 3. crate index table from %tbNIC with ifIndex and sortField;
  412.        # 4. sort and print
  413.        my $SortTBName = $SortName{$SortTable};
  414.        #print $SortTBName,"\n";
  415.        my %dbIndex;
  416.        my $Ifindex;
  417.        my $SortFileNameTP;
  418.        while (($Ifindex,$SortFileNameTP) = each(%tbNIC))
  419.        {
  420.           $dbIndex{$Ifindex} = $SortFileNameTP->{$SortTBName};
  421.        }
  422.        
  423.        sub asc_sort_0
  424.        {
  425.            $dbIndex{$a} <=> $dbIndex{$b};
  426.        }

  427.        sub asc_sort_1
  428.        {
  429.            $dbIndex{$b} <=> $dbIndex{$a};
  430.        }

  431.        my $Key;
  432.        my $Value;
  433.        if ($SortType)
  434.        {
  435.            foreach $Key (sort asc_sort_1(keys(%dbIndex)))
  436.            {
  437.                $str = "$Key\t" . $tbNIC{$Key}->{$FieldName[0]};
  438.                foreach (@FliedGroup)
  439.                {
  440.                    $str = $str . "\t" . $tbNIC{$Key}->{$_};
  441.                }
  442.                print "$str\n";
  443.            }
  444.        }
  445.        else
  446.        {
  447.            foreach $Key (sort asc_sort_0(keys(%dbIndex)))
  448.            {
  449.                $str = "$Key\t" . $tbNIC{$Key}->{$FieldName[0]};
  450.                foreach (@FliedGroup)
  451.                {
  452.                    $str = $str . "\t" . $tbNIC{$Key}->{$_};
  453.                }
  454.                print "$str\n";
  455.            }

  456.        }
  457.     }
  458.     #print other info
  459.     print "$Tile\n";
  460.     my $index;
  461.     my $value;
  462.     while (($index,$value) = each(%InterfaceOID))
  463.     {
  464.        print "$index : $value\n";
  465.     }
  466.     #----------------------------------End-------------------------------------
  467. }



  468. sub usage {
  469.     print << "USAGE";

  470. --------------------------------------------------------------------    
  471. $script v$script_version

  472. View Interface status with SNMP Protocol on Cisco Switch and Route devices.

  473. Note: not support serial interface.


  474. Usage: $script Remote-Option [OID-Option] [Sort-Option] [Other-Option]

  475. Remote-Options:
  476.          -H         Hostname or IP address
  477.          -c         Community (default is public)

  478. OID-Option
  479.     -m    $FieldName[1]    $InterfaceOID_orgin{$FieldName[1]}
  480.         $FieldName[2]    $InterfaceOID_orgin{$FieldName[2]}

  481.         -r $FieldName[3]    $InterfaceOID_orgin{$FieldName[3]}
  482.                 $FieldName[4]    $InterfaceOID_orgin{$FieldName[4]}

  483.         -R $FieldName[5]    $InterfaceOID_orgin{$FieldName[5]}
  484.                 $FieldName[6]    $InterfaceOID_orgin{$FieldName[6]}

  485.         -P $FieldName[7]    $InterfaceOID_orgin{$FieldName[7]}
  486.                 $FieldName[8]    $InterfaceOID_orgin{$FieldName[8]}

  487.         -M $FieldName[9]    $InterfaceOID_orgin{$FieldName[9]}
  488.                 $FieldName[10]    $InterfaceOID_orgin{$FieldName[10]}

  489.         -B $FieldName[11]    $InterfaceOID_orgin{$FieldName[11]}
  490.                 $FieldName[12]    $InterfaceOID_orgin{$FieldName[12]}

  491.         -b $FieldName[13]    $InterfaceOID_orgin{$FieldName[13]}
  492.                 $FieldName[14]    $InterfaceOID_orgin{$FieldName[14]}

  493.         -e $FieldName[15]    $InterfaceOID_orgin{$FieldName[15]}
  494.                 $FieldName[16]    $InterfaceOID_orgin{$FieldName[16]}

  495.         -C $FieldName[17]    $InterfaceOID_orgin{$FieldName[17]}

  496.         -t $FieldName[18]    $InterfaceOID_orgin{$FieldName[18]}

  497.         -D $FieldName[19]    $InterfaceOID_orgin{$FieldName[19]}

  498.     -o    $FieldName[20]    $InterfaceOID_orgin{$FieldName[20]}

  499.         -a    All

  500. Sort-Option
  501.          -s         Only OID-Option char,more detail as below.
  502.       
  503.                                 chars Sort-Field
  504.                                 ---- -------------
  505.                                 mI $FieldName[1]
  506.                                 mO $FieldName[2]
  507.                                 rI $FieldName[3]
  508.                                 rO $FieldName[4]
  509.                                 RI $FieldName[5]
  510.                                 RO $FieldName[6]
  511.                                 PI $FieldName[7]
  512.                                 PO $FieldName[8]
  513.                                 MI $FieldName[9]
  514.                                 MO $FieldName[10]
  515.                                 BI $FieldName[11]
  516.                                 BO $FieldName[12]
  517.                                 bI $FieldName[13]
  518.                                 bO $FieldName[14]
  519.                                 eI $FieldName[15]
  520.                                 eO $FieldName[16]
  521.                                 C $FieldName[17]
  522.                                 t $FieldName[18]
  523.                                 D $FieldName[19]
  524.                 o $FieldName[20]

  525.          -S 0 or 1        0 increase,1 decrease

  526.          -d view this interface what it's line proctocol is down.
  527.                                 default view up interface.

  528. Other-Option
  529.      -h or -v         Help script Usage

  530. --------------------------------------------------------------------    
  531.    
  532. This program is free software; you can redistribute it or modify
  533. it under the terms of the GNU General Public License
  534. --------------------------------------------------------------------

  535. USAGE
  536.      exit 1;
  537. }


  538. sub GetScriptName
  539. {
  540.    my @str = @_;
  541.    my $len = rindex($str[0],"/");
  542.    return substr($str[0],$len + 1);
  543. }


安装:

将上面的代码用vi保存为view-nic,并设置可执行权限.

使用帮助:

#view-nic -v

 


 

 


 

阅读(2956) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~