There are many tutorials on the net on how to use the Net::Telnet module to write a script that will automate a telnet session. Perhaps the best place is the CPAN site itself. Though all i needed was a simple script that did the job, i had to scour through many other sites for reference. Finally i’m done with the script that will telnet to the server collect the logs and then mail it to the id that i specified.
I wrote the working version as two perl scripts. I still dont know why i did that, may be for the simplicity sake.
- log.pl => does the telnet to retrieve the logs and mail it to me
- run.pl => which runs the log.pl every hour
So here is the code -
log.pl
#!/usr/bin/perl
use Telnet;
# ---------------------------
$date = `date`;
chop $date;
$mailId = "jerry\@jerrymannel.com";
$cmd = "cat in.log | mail ".$mailId." -s \"Logs - ".$date."\" ";
# ---------------------------
$host = "192.168.1.100";
$port = "2300";
$uid = "jerry";
$pwd = "password";
open $inputLog, ">in.log";
$box = new Net::Telnet();
$box->open( Host => $host,
Port => $port,
);
$iLog = $box->input_log($inputLog);
$flag = $box->login( Name => $uid,
Password => $pwd,
);
$box->print("co");
$box->waitfor('/# $/i');
$box->print("show load;show cpu");
$box->waitfor('/# $/i');
$box->print("show log");
$box->waitfor('/# $/i');
$box->close;
# MAIL section
system $cmd;
exit
run.pl
#!/usr/bin/perl
while (1)
{
$date = `date +%M%S`;
chop $date;
#print $date." - ";
if ( $date eq "0000" )
{
print "Sending the mail @ - ".(`date`);
system("./logs.pl");
}
sleep(1);
}
The script was run on a Linux machine. So i neednt worry about the ‘date’ command and getting the date printed out in a fashion that i needed.
Drop in a line of you have more queries on the script. Always happy to help.

course about my friend 