Perl Telnet Script

I wanted to find a way to backup cisco device configurations and retrieve other bits of info in an automated fashion via telnet.  Unfortunately there is no good way with windows batch files without using a 3rd party utility.  If I was going to go 3rd party, I was going to install perl so I can write a script that could be used in windows and/or linux.

I installed the ActiveState Perl for Windows package and used the following script.  You will have to adjust the strings in the 'waitfor' statements.  Also note the line that turns of pagination in the output off. 

This script was for a Cisco firewall, but with a few tweaks it will work for switches and routers and almost any telnet accessible device.

WARNING:  You are storing your username and password in this file...

use warnings;
use Net::Telnet;
use Time::Format;

$myIPaddress = "";
$myDefaultPassword = 'password';
$myUsername = "user";
$myHostname = "hostname";

$date = $time{'yyyymmdd'};
$OutputFile = "$myHostname" . "_" . "$date" . ".txt";

open (OUTPUT, ">>$OutputFile");

$telnet = new Net::Telnet ( Timeout=>10,Errmode=>'die');
$telnet->waitfor('/Username: $/i');
$telnet->waitfor('/Password: $/i');
$telnet->waitfor("/$myHostname> \$/i");
$telnet->waitfor('/Password: $/i');
$telnet->waitfor("/$myHostname# \$/i");
$telnet->print("terminal pager 0");
$telnet->waitfor("/$myHostname# \$/i");
$telnet->print("show ver");
$telnet->waitfor("/$myHostname# \$/i");
$telnet->print("show ver");
@version = $telnet->waitfor("/$myHostname# \$/i");
$version = "@version";
$telnet->print("show run");
@running = $telnet->waitfor("/$myHostname# \$/i");
$running = "@running";

print OUTPUT $version;
print OUTPUT $running;

close (OUTPUT);