Generating a Server Host Table File


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


The Perl script in Example 2-4,, 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.

# -- a script to build a detailed host file from
# information gathered from a router list.
#Set behavour
$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 () {
 @ifIndex=Q$snmpwalk $rtr ipAdEntIfIndexQ;
 @ipAddress=Q$snmpwalk $rtr ipAdEntAddrQ;
 $rtr1=Q$snmpget $rtr .;
 chomp(($foo, $RTR) = split (/"/, $rtr1));
 for $ifnumber (@ifIndex) {
 chomp(($foo, $ifnum) = split(/= /, $ifnumber));
 $ifDescription=Q$snmpget $rtr ifName.$ifnumQ;
 chomp(($foo, $ipaddr) = split(/: /, $ipAddress[$arraynum]));
 chomp(($foo, $ifdes) = split(/= /, $ifDescription));
 if ( $ifdes eq "Lo0" ) { $name=$RTR };
 print RESULT "$ipaddr		$name


Most organizations manually build a host table for their management server(s) with a single IP entry per router, usually the loopback IP address. This script automatically builds a host file that contains all known IP addresses for each router.

Here is an example of the output from the script: miami-Se0/0 miami miami-Se0/2 miami-Fa1/0 miami-Et0/0 toronto-Se0/1 toronto-Se0/0.2 toronto-Fa0/1 toronto-Fa0/0.1 toronto-Se0/0.1 toronto toronto-Lo1 detroit-BR0 detroit-Et0 detroit boston-Se0.1 boston-Et0 boston

This output is in the format required for a Unix /etc/hosts file. The script extracts the IP address information via SNMP, and then associates each address with the related router name and interface. The script also automatically creates a primary host entry for each router using the address of the loopback0 interface, but with no interface information in the hostname. So, in this example, you can reach the router located in Boston with hostname boston rather than the less intuitive boston-Lo0.

Having a detailed host file is useful for many reasons. Sometimes the router will send a message to your server, either using SNMP or Syslog, and use the IP address of the interface instead of a main loopback interface. Also, having a detailed host file makes the output of a traceroute much easier to understand:

Freebsd% traceroute miami
traceroute to miami (, 64 hops max, 52 byte packets
 1 detroit-Et0 ( 2.263 ms 2.210 ms 2.178 ms
 2 toronto-Fa0/0.1 ( 3.042 ms 3.060 ms 3.846 ms
 3 boston-Se0.1 ( 8.234 ms 8.245 ms 8.145 ms
 4 miami-Se0/2 ( 9.893 ms 9.893 ms 9.432 ms 

This makes it much easier to decipher the path between the management station and the Miami router. Not only can we tell which routers lie along the path, but we can also clearly see which interfaces a packet sent along this path will use.

The script does not update the /etc/hosts file directly. You may need to manually import the script's output file into your system's /etc/hosts file. However, you can make this task easier by building a master file containing your normal host information and concatenating this master file together with the script output. This way you could even use the cron utility to automatically run this script and create a new up-to-date host file on a nightly basis.

Before the script will work, you must modify two variables. The $workingdir variable must be set to the directory that you will launch the script from. The $snmpro variable must be set to your SNMP read-only community string. The script assumes that you use the same read-only community string on all of your routers.

The script reads through a router list, and queries each device in sequence. It expects to find this list in a file called RTR_LIST in the working directory. The list can contain router names or IP addresses, with one entry per router, and one router per line. The script will extract the hostname directly from the router, so you should also ensure that all of your routers are configured with unique hostnames. The results of the script are stored in a file called RESULT, contained in the working directory.

As a final note, we should mention that this script can generate hostnames that do not confirm to RFC 952, "DOD INTERNET HOST TABLE SPECIFICATION," which defines the official rules for hostnames. This is because the script can create hostnames with a "/" character in them, such as miami-Se0/2. This may cause problems for some applications, particularly if they use URL format addressing. For example, a query to http://miami-Se0/2 will clearly cause problems because the last character, 2, will be interpreted as a filename. However, most common applications, such as ping and Telnet, will accept this hostname without any complaints.

If you are a purist, or if you have applications that complain about these hostnames, we've included a line in the script that you can use to convert all of the slashes, "/", to dashes, "-". This line is currently commented out, but you can invoke it by simply removing the comment character, "#", to change this line:


to this:



See Also

RFC 952

Router Configuration and File Management

Router Management

User Access and Privilege Levels


IP Routing





Frame Relay

Handling Queuing and Congestion

Tunnels and VPNs

Dial Backup

NTP and Time


Router Interfaces and Media

Simple Network Management Protocol





First Hop Redundancy Protocols

IP Multicast

IP Mobility




Appendix 1. External Software Packages

Appendix 2. IP Precedence, TOS, and DSCP Classifications


show all menu

Cisco IOS Cookbook
Cisco IOS Cookbook (Cookbooks (OReilly))
ISBN: 0596527225
EAN: 2147483647
Year: 2004
Pages: 505
Similar book on Amazon © 2008-2017.
If you may any questions please contact us: