Tuesday, June 14, 2005

Gathering Cisco Router Configurations with Perl

Have a number of routers from which to gather configurations? The following bit of Perl makes this extremely quick and easy, if they all have matching telnet and enable passwords. Must install the CPAN module Net::Telnet::Cisco, and enter the IP addresses of the routers in a file called routerlist.txt, one IP per line. To use this, you just need to replace "TELNET_PASSWORD" and "ENABLE_PASSWORD" below with the appropriate real passwords from the router.

It'll output files in the directory you run it from called IP.add.re.ss.conf, one file per IP.

#!/usr/bin/perl -w

use Net::Telnet::Cisco;
@routers = `cat routerlist.txt`;

foreach my $router (@routers) {
      my $session = Net::Telnet::Cisco->new(Host => $router);
      @conf = $session->cmd("wr t");
      open LOG, "> $router.conf";
      select LOG;
      print @conf;
      close LOG;

Disclaimer: Are there better ways to write this? I'm sure there are. I don't proclaim to be a programmer, but the way I do things *works*, whether or not it's the "best" way. There's more than one way to do it!