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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Net::Telnet script please

Status
Not open for further replies.

ezeke1

Programmer
Mar 20, 2003
41
US
I'm trying to write a simple telnet script to log into a router and the problem I'm encountering is that the script times out at "$telnet->login($username, $password);". Is it because I am expecting the wrong prompt?

#!/usr/bin/perl -w

#program to telnet to remote host

use Net::Telnet();


$telnet = Net::Telnet->new
(
Timeout => 10,
Prompt => '/Username: /'
);

$username = "joesmoe";
$password = "xxxxx";


$telnet->open ("x.x.x.x");

$telnet->login($username, $password);

$telnet->cmd ("show version");

@output= $telnet->cmd("show version");

$telnet->cmd("exit");
print $output;
 
In all probability it will be the wrong prompt. Try changing the prompt to '/ /' (i.e. a space) and see if it then goes through without timing out. If it does, you will then need a wait for command prompt line such as

$telnet->waitfor('/:/');

immediately after the login line. I find that if the full prompt does not work (e.g. Username:) I reduce it down until it does (just : might work).

I strongly recommend using the logging facility to help see what is going wrong. Change

$telnet = Net::Telnet->new
(
Timeout => 10,
Prompt => '/Username: /'
);

to this:

$telnet = Net::Telnet->new
(
Timeout => 10,
Prompt => '/ /',
Dump_log => 'dump.log',
Input_log => 'input.log',
Output_log => 'output.log',
);

Then read the logs after each failed run.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top