Tuesday, January 29, 2013

IOS hardware inventory script

Once i was asked to create an excel file of the hardware inventory, one way was to telnet each router execute few commands and manually copy paste the lines to excel file but this was a clerical job so i made a script that collects all this information from the IOS devices.

Prerequests:
  • Linux machine with Perl installed
  • apt-get install net-snmp (debian)
  • yum install net-snmp (redhat based)
  • input.txt (a simple IP address list of the devices)
  • set the correct snmp  to variable $snmpro="your_comm";
  • result.csv will generate the output file with all the required serial numbers and card details.
  • Put all the files in same directory
Troubleshooting:

perl -v

check if perl installed, if perl is install it will prompt the version of perl install otherwise perl is not installed.


snmpget -v2c -c public IP_Address 1.3.6.1.4.1.9.2.1.3.0

provide the correct community and IP address of snmp agent, it should return the host-name of the agent. otherwise you can paste your problem on this blog and ill try my level best to reply ASAP.




vi inventory.pl
open your fav editor and paste the code in the code section.

Execution:
perl scriptname.pl # perl inventory.pl

Some important and Usefull OIDs:

iso.3.6.1.2.1.47.1.1.1.1.11.1 motherboard oid
iso.3.6.1.2.1.47.1.1.1.1.11.21 device
iso.3.6.1.2.1.47.1.1.1.1.13.1 device name
1.3.6.1.2.1.47.1.1.1.1.11 serial numbers
1.3.6.1.2.1.47.1.1.1.1.13 names
1.3.6.1.4.1.9.2.1.3.0 hostname



#!/usr/bin/perl
$snmpro="public";
$rtrlist="input.txt";


open (RTR, "input.txt") || die "Can't open $rtrlist file";
open (LOG, ">result.csv") || die "Can't open $workingdir/RESULT file";
open (ERR1, ">err.log") || die "Can't open $workingdir/RESULT file";

while (<RTR>) {
  #print $_;
  chomp($rtr="$_");
  $snmpget=`snmpget -v2c -c $snmpro $rtr 1.3.6.1.4.1.9.2.1.3.0`;
  
  if ($snmpget =~ /Timeout/) { print ERR1 "cant open $rtr\n"; print "can't open $rtr.\n"; } else
  {
  @str = split (/\=/,$snmpget);
  @hostname = split (/ /,$str[1]);
  print $hostname[2];
  chomp($hostname[2]);
  
 }


  @snmpname = `snmpwalk -v2c -c $snmpro $rtr 1.3.6.1.2.1.47.1.1.1.1.13`;
  @snmpserial = `snmpwalk -v2c -c $snmpro $rtr 1.3.6.1.2.1.47.1.1.1.1.11`;
  $line_num=0;
  foreach (@snmpname) {
        chomp ($_);
        @name = split (/\=/,$_);
        @name1= split (/ /,$name[1]);
        if($name1[2]) {
        chomp($snmpserial[$line_num]);
        @serial1= split (/\=/,$snmpserial[$line_num]);
        @serial = split (/ /,$serial1[1]);
        print $name1[2]."=".$serial[2]."\n";
        print LOG $rtr.",".$hostname[2].",".$name1[2],$serial[2]."\n";
        }
       
   
    $line_num++;
  }
  #print @snmpname;
  #print @snmpserial;
  #printf LOG ("%-12.12s; %-30.30s; %-25.25s; %-12.12s\n", $RTR, $LOC, $CON, $SIN);

  }
close (RTR);
close (LOG);
close (ERR1);


1 comment: