Generating a Server Host Table File

Problem

You want to build a detailed host file containing the IP addresses and interface names of all of your routers.

Solution

The Perl script in Example 2-4, host.pl, builds a detailed host table that includes all of the IP addresses on each router in a list of devices. The script is written in Perl and requires NET-SNMP to extract data from the router list. No arguments are expected or required.

Example 2-4. host.pl

#!/usr/local/bin/perl # # host.pl -- a script to build a detailed host file from # information gathered from a router list. # #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 (RESULT, ">$workingdir/RESULT") || die "Can't open RESULT file"; while (<RTR>) { chomp($rtr="$_"); @ifIndex=\Q$snmpwalk $rtr ipAdEntIfIndex\Q; @ipAddress=\Q$snmpwalk $rtr ipAdEntAddr\Q; $rtr1=\Q$snmpget $rtr .1.3.6.1.4.1.9.2.1.3.0\Q; chomp(($foo, $RTR) = split (/"/, $rtr1)); $arraynum=0; for $ifnumber (@ifIndex) { chomp(($foo, $ifnum) = split(/= /, $ifnumber)); $ifDescription=\Q$snmpget $rtr ifName.$ifnum\Q; chomp(($foo, $ipaddr) = split(/: /, $ipAddress[$arraynum])); chomp(($foo, $ifdes) = split(/= /, $ifDescription)); $name="$RTR-$ifdes"; #$name=~s/\//-/; if ( $ifdes eq "Lo0" ) { $name=$RTR }; print RESULT "$ipaddr\t\t$name\n"; $arraynum++; } } close(RTR); ...

Get Cisco IOS Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.