Generate hostnames from router configurations

From CT3

Jump to: navigation, search

This short PERL script parses router configurations specified as the command line arguments (wildcards can be used) and generates an ip host command for every interface IP address encountered in the configurations. When the list of ip host commands is configured on a router, the traceroute utility displays router- and interface names instead of IP addresses, significantly simplifying lab configuration and troubleshooting efforts.

Usage guidelines

  • Store the source code in rtrhost.pl.
  • Start with rtrhost.pl router-configuration-files, for example rtrhost.pl config/*.txt
  • The script generates a list of ip host commands on standard output. Redirect the output into a file and copy the ip host commands into router configuration.
If you'll extend the script functionality with the ability to generate an output file specified in the command line, please post the modified code in this post

After the ip host commands have been entered into the router configuration, the traceroute command displays interface- and router names (including the domain if you've specified the default domain in the router configurations). The traceroute command executed in the sample MPLS Traffic Engineering lab has produced the following results:

PE-A#traceroute PE-C

Type escape sequence to abort.
Tracing the route to PE-C (10.0.1.5)

  1 Serial1-0.C1 (10.0.7.6) [MPLS: Label 1017 Exp 0] 56 msec 8 msec 12 msec
  2 Serial1-2.C3 (10.0.7.26) [MPLS: Label 3018 Exp 0] 12 msec 24 msec 20 msec
  3 Serial1-1.C4 (10.0.7.30) [MPLS: Label 4018 Exp 0] 48 msec 12 msec 16 msec
  4 Serial1-2.C2 (10.0.7.33) [MPLS: Label 2017 Exp 0] 52 msec 12 msec 8 msec
  5 Serial1-0.PE-C (10.0.7.17) 16 msec *  64 msec

Author

Ivan Pepelnjak, © 2008 NIL Data Communications

Source code

#!/usr/bin/perl

use strict;
use Getopt::Long;

our ($host,$domain,$FQDN,$loop,$ifname,$inIfMode);
our ($opt_dbg);

GetOptions(
	"debug"    => \$opt_dbg);	# help

while (<>) {
  if (/^hostname\s+(.*)/) { 
    $host = $1; 
    print STDERR "processing router $host\n" if $opt_dbg; 
    $FQDN = $host; $loop = 0; 
  }
  if (/^ip\s+domain-name\s+(.*)/) { 
    $domain = $1; 
    print STDERR "router $host in domain $domain" if $opt_dbg; 
    $FQDN = "$host.$domain"; }
  if (/^interface\s+(.*)/) { 
    $ifname = $1; $ifname =~ s/[:\/.]/-/gi; $ifname = "$ifname.";
    print "interface $ifname\n" if $opt_dbg; 
    if ($ifname =~ /loopback/i) { $ifname = "" unless $loop++; }
    $inIfMode = 1 ;
  } elsif (/^[a-z]/i) { $inIfMode = 0; }
    elsif (/^\s+ip address ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/i && $inIfMode) { 
      print "ip host $ifname$FQDN $1\n"; 
  }
}
Personal tools

CT3

Main menu