Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PERL and TELNET 3

Status
Not open for further replies.
Jul 31, 2001
37
US
I need to write a script to telnet into a router and issue a few commands any ideas. I know there is a telnet module but i'm not to familiar with it's workings? I'm new to perl so i thank you in advance for your help
 
The module you want is Net-Telnet. I have used it and it works fine on Windows and Unix systems. The module comes with a few examples that can get you started. I've included the first program that I wrote using Net-Telnet.

use Net::Telnet ();

$host = 'xxxx';
$username = "yyyy";
$password = "zzzz";
$t = new Net::Telnet;
$t->open($host);
$t->waitfor('/login:/');
$t->print($username);
$t->waitfor('/Password:/');
$t->print($password);
$t->waitfor('/]#/');
$t->print('uptime');
$t->getline;
($up) = $t->getline;
print "uptime for $host is $up\n";
$t->waitfor('/]#/');
close($t);
 
Once again the experts have not let me down. Thanks for the help.
 
Ok folks now is when I become the bug. Is there a way for me to see the script as it happenes or send it to a logfile. So I can see the responses from the hosts the script telnets to? This would allow me to troubleshoot errors I'm getting with timeouts against prompt matches. Thanks
 
I've never used Net::Telnet before, but here's a snippet from "perldoc Net::Telnet" having to do with options to new:

$obj = Net::Telnet->new([Binmode => $mode,]
[Cmd_remove_mode => $mode,]
[Dump_Log => $filename,]
[Errmode => $errmode,]
[Fhopen => $filehandle,]
[Host => $host,]
[Input_log => $file,]
[Input_record_separator => $char,]
[Option_log => $file,]
[Output_log => $file,]
[Output_record_separator => $char,]
[Port => $port,]
[Prompt => $matchop,]
[Telnetmode => $mode,]
[Timeout => $secs,]);

Looks like you can set argument "DumpLog" to capture output from the remote host.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
hmerrill and raider2001,
Again Thanks for the time you both have provided excellent information you'll be happy to know I have completed the script and all is working well. Couldn't have done it without you. This forum rocks!!
 
Sorry! I spoke to soon. My log files don't seem to be capturing any I/O. Any suggestions? The files get created but are always empty regardless of whether or not the script runs clean. Raider2001 I used the $t->getline; statement so the script kicks back the status of my transfer but unless i'm infront of the screen I wouldn't see that. My hopes are that I can be lazy and cron the script to run overnight and check the logfile in the morning, LOL maybe even have it e-mailed to me LOL.
 
I tried it your way and I get the same result blank files the only difference is your way they are .log files. Can I view those through a text editor or do I need something special. Also the file sizes are 0kb so even if there is info I cant view in notepad the file size should still be bigger than 0kb correct.

Befor your last post I was invoking like this on merrill's suggestion. the script is creating the file it's just not filling it. i tried different variations on the filename extensions.

$filename = "dump";
$file = "outlog";
$obj = Net::Telnet->new (Dump_Log => $filename,);
$obj = Net::Telnet->new (Output_log => $file);
 
I tried the following script running on a Windows and a linux system. On both systems, it created the 3 log files. All 3 files are text files. Try running this script (change the host, username, password, and prompt).

Code:
use Net::Telnet;
my $host = "XXX";
my $user = "YYY";
my $password = "ZZZ";
my $prompt = "]#";
my (@data, $line);
my $t = new Net::Telnet(
                        Timeout     => '5',
                        Prompt      => "/$prompt/",
                        Dump_log    => 'dump.log',
                        Input_log   => 'input.log',
                        Output_log  => 'output.log',
                        );
$t->Net::Telnet::open($host);
$t->login(
          Name     => $user,
          Password => $password,
         );
@data = $t->cmd(String => 'uptime');
foreach $line (@data) {
     print "$line\n";
}
$t->close();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top