Cisco Poller Perl Script

Wrote this a while ago and it comes in handy all the time. It’s specific to Cisco devices (although it probably works on a lot of imitators). Basically you add your host, username, passwords and the command you’d like executed on the Cisco device to the config file (poller.hosts). Run the script and it saves the output for you. You can add multiple host entries and then add an entry to your cron file to periodically run cisco_poller.pl

You need one input file which defaults to poller.hosts. As you can see the fields are seperated with a colon. Very handy detail, you can actually pipe to include which makes the output even more grep friendly. Below are a few examples to get you started with your poller.hosts file.

poller.hosts:

192.168.0.1:CiscoUser:CiscoPass:EnablePass:show ver
192.168.0.2:SwitchUser:SwitchPass:EnablePass:show ver
192.168.0.3:User:Pass:pass:show inter | i error
192.168.0.4:MyUser:MyPass:MyPass:sh ip eigrp neighbor

cisco_poller.pl:

#!/usr/bin/perl
use Net::Ping;
use Net::Telnet::Cisco;

$configDir = "/scripts/";
$hostConfig = "/scripts/poller.hosts";
$logFile = "/scripts/poller.log";
$user = "twrightson";

open(INFO, $hostConfig);
@config = ;
close (INFO);
$curdate = `date`;
open( LOG, ">>$logFile");

print LOG "[+]------------ Starting $curdate";

$size = @config;
for($i=0; $inew(Host => "$routerAdd" );
if( $router->login(Name => $user,Password => $password)) {
print "[+] Logged into router: $routerAdd successfully\n";
}

$router->enable($enablePass);
@shRun = $router->cmd($command);

$configFile = $configDir . $routerAdd . "_" . $command;
open ( outFile, '>>' . $configFile );
print outFile @shRun;
close outFile;

print LOG "[+] Saved Output to $configFile";

}
$curdate = `date`;
print LOG "[+]------------ Program Done $curdate";
#EOF

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s