In this video I show some of the basics for creating a simple perl script to collect output from a cisco switch and write it to a log
here's an example of using the script
perl switch_log.pl 10.44.10.1 password
the perl script is below. simply copy and past into a text editor and save with a .pl extension like switch_log.pl
use Net::Telnet::Cisco; use Time::localtime;
$host = $ARGV[0]; #this is the first value entered after switch_log.pl $password = $ARGV[1]; #this is the second value entered after switch_log.pl
print "Switch_log.pl script running .... \n";
$session = Net::Telnet::Cisco->new(Host => $host);#open Telnet session $session->login('login', $password); # 'login' is requierd since I do not have a username configured print ".Logged in\n";
$session->cmd('term len 0'); #term len 0 gets rid of the more prompt my @sh_log = $session->cmd('show log'); # Execute the show log command $session->close; print "..Got Log\n";
$date_time = sprintf("%02d-%02d-%02d_%02d_%02d_%02d", localtime->mon()+1, localtime->mday(), (localtime->year() + 1900),localtime->hour() , localtime->min() , localtime->sec() ) ; $file= ">>".$date_time . ".log"; #create filename for log file
open LOG, $file || die "cannot open output file"; print LOG "Log Created $file\n"; print LOG "@sh_log"; close (LOG); print ".File $file created.\n";