netstat.pl 2.04 KB
Newer Older
O'Reilly Media, Inc.'s avatar
O'Reilly Media, Inc. committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
netstat.pl
#!/usr/local/bin/perl 
#
#      netstat.pl -- a script to build a detailed IP interface  
#                    listing directly from a list of routers. 
#
#Set behavour
$workingdir="/home/cisco/net";
$snmpro="ORARO";      
#
$rtrlist="$workingdir/RTR_LIST";
$snmpwalk="/usr/local/bin/snmpwalk -v 1 -c $snmpro";
$snmpget="/usr/local/bin/snmpget -v 1 -c $snmpro";
open (RTR, "$rtrlist") || die "Can't open $rtrlist file";
open (CSV, ">$workingdir/RESULT.csv") || die "Can't open RESULT.csv file";
while (<RTR>) {
   chomp($rtr="$_");
   @ifIndex=`$snmpwalk $rtr .1.3.6.1.2.1.4.20.1.2`;
   @ipAddress=`$snmpwalk $rtr .1.3.6.1.2.1.4.20.1.1`;
   @ipMask=`$snmpwalk $rtr .1.3.6.1.2.1.4.20.1.3`;
   $arraynum=0;
   print CSV "\n$rtr\n";
   print CSV "Interface, IP-Address, Mask, MTU, Speed, Admin, Operational\n";
   for $ifnumber (@ifIndex) {
      chomp(($foo, $ifnum) = split(/= /, $ifnumber));
      $ifDescription=`$snmpget $rtr ifDescr.$ifnum`;
      $ifMTU=`$snmpget $rtr ifMtu.$ifnum`;
      $ifSpeed=`$snmpget $rtr ifSpeed.$ifnum`;
      $ifAdminstatus=`$snmpget $rtr ifAdminStatus.$ifnum`;
      $ifOperstatus=`$snmpget $rtr ifOperStatus.$ifnum`;
      chomp(($foo, $ipaddr) = split(/: /, $ipAddress[$arraynum]));
      chomp(($foo, $mask) = split(/: /, $ipMask[$arraynum]));
      chomp(($foo, $ifdes, $foo) = split(/"/, $ifDescription));
      chomp(($foo, $mtu) = split (/= /, $ifMTU));
      chomp(($foo, $speed) = split (/: /, $ifSpeed));
      chomp(($foo, $admin) = split (/= /, $ifAdminstatus));
      chomp(($foo, $oper) = split (/= /, $ifOperstatus));
      if ( $speed > 3194967295 ) { $speed = 0 };
      $admin =~ s/\(.*\)//;
      $oper  =~ s/\(.*\)//;
      if ( $oper eq "dormant" ) { $oper = "up(spoofing)"};
      $speed = $speed/1000;
      if ( $speed > 1000) {
         $speed = $speed/1000;
         $speed =~ s/$/ Mb\/s/;
      }
      else {
      $speed =~ s/$/ Kb\/s/;
      }
      print CSV "$ifdes,$ipaddr,$mask,$mtu,$speed,$admin,$oper\n";
      $arraynum++;
   }
} 
close(RTR);
close(CSV);