In my last article, I talked about using various network tools over the years to automate network configuration management. We had some great comments and feedback to that post, so I thought I'd take a moment to do a deep dive on one of the tools I've used - PERL.   This is a great 'first step' into configuration archival for somebody who may be scripting-inclined, but may not have need to full blown change-comparison capability.  This isn't meant to be a programming tutorial on how to write code, but it's more of a jumping-off point for those of you who aren't using tools today and would like to kick the tires on what's possible.   This is more an exercise in getting you to 'think network management', and we're just going to use a little scripting wizardry to do just that. I'm going to assume that you've got a working installation of perl on your operating system of choice (for windows users, ActivePerl is a great choice), you linux and OSX users should already have perl installed with your operating system


Let me start off by sharing some code. (And feel free to share / modify / rant about this code. It's a compilation of code I've pieced together over the years)

use  Net::Telnet::Cisco::IOS;
use  Net::Telnet::Cisco;

open INFILE,$InFile;
close INFILE;

$username = "ciscovoicedude";               # the username to use when logging in
$password = "n0fax";                           # the matching password
$enablepw = "enablepassword";
@hosts = qw(@CONFIG);
   foreach $host ( @CONFIG )  {             # go through the array one element at a time
       chomp $host;
         my $conn = Net::Telnet::Cisco::IOS->new(HOST => $host);  # connect to the host
         $conn->login(   Name => $username, #  Log into the device
                          Password => $password);
     #   $conn->enable($enablepw);          # You can enable this line if you have to send an enable to the router. I don't.

@output = $conn->getConfig();               # Put the config in an array

        $outfile = ">" . $host . "-config.txt";  # create a filename called ">host-confg"
        open OUTFILE,$outfile;              # open a file for writing
        print "Writing $host to file\n";    # write a status message
        print OUTFILE @output;              # print the config to file
        close OUTFILE;                      # close the file
        $conn->close;                       # close the connection to the device
@output = ""




In this short script, you'll see a basic 'running configuration' fetch from a device via telnet.   I'm using the Net::Telnet::Cisco::IOS module, so use your favorite perl package manager to install this module prior to trying to run the code.  In essence, the script will read an input file (I show it as being a CSV file, but really I'm only accessing a single column of data), from which it will determine the devices to connect to.  See the code below for an example of an input file.


example routers.csv


The script will iterate through each device in the CSV file, and issue a 'show run', and save the output to a text file named  "devicename-config.txt".  You could easily modify the script and input CSV to allow you to pass a different set of login credentials for each device, but I'll leave that as an exercise for the reader.


As you can see, with very basic scripting capability, you can automate configuration collection from a great number of devices. You could even automate running this using a cron job or scheduled task on your operating system.


Taking things a step farther, you could even modify this code to deploy configuration changes to devices, or do more advanced parsing and reporting. Heck, you could even store the configuration archive in a database for all of your devices and that opens up all kinds of opportunities down the road.


So I leave you with this - if you're one of those people who haven't yet dipped their toes into this, go ahead and take the plunge and let us know what you think!  For anybody who has written code to do even cooler things, I invite you to share some links with the rest of us so we can collaborate and learn together as a community.


Next time I'm going to talk about strategies with dealing with all of this data we have gathered, and useful things we can do with it.