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) {
      chomp($router);
      my $session = Net::Telnet::Cisco->new(Host => $router);
      $session->login('','TELNET_PASSWORD');
      $session->enable("ENABLE_PASSWORD");
      @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!

2 Comments:

Blogger nzeFrank said...

I found that quite interesting becuse it is close to something that's been bothering me for a while.

Do you know how i can use a batch or script, telnet into a Linux box and shut it down?

So the batch/script would:
- telnet 1.2.3.4
- login
- password
- su
- password
- /sbin/shutdown -h now

Much obliged.

9:32 AM  
Anonymous Anonymous said...

Even easier would be to use ssh.

ssh 1.2.3.4 /sbin/shutdown -h now

If you have the ssh keys set properly it will all Just Work.

2:41 PM  

Post a Comment

<< Home