Script to run on a router

I want to make a Perl script to telnet to a router and apply commands, and get the output of the commands in a file.

Can any one help me in that?

Can't any one help me, I find the script to login to router but the issue is to redirect the output of commands to a file.

#!/usr/local/bin/perl
#

use Net::Telnet ();

$machine = '1.1.1.1';
$logfile = "cisco.log";

$t = new Net::Telnet (Timeout => 10);
$t->input_log($logfile);
$t->open($machine);
print "Connected to $machine\n";
$t->waitfor('/Username: $/');
$t->print("cisco");
$t->waitfor('/Password: $/');
$t->print("cisco");
$t->prompt('/LTD-INC>/');
$t->cmd("enable");
$t->waitfor('/Password: $/');
$t->print("cisco");
$t->prompt('/LTD-INC#/');

$t->cmd("show run");

????????????????????? This is what I don't know ?????????????????

print "Logged in successfully.\n";
print "Collecting informations";
print "done.\n";
print "Most heavy connection/s :\n" ;

I have the script now running on cisco routers, but on Juniper routers, it time out and not continue.

Can Any one help me important , PLZ ???