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!

Net::Telnet script question

Status
Not open for further replies.

ezeke1

Programmer
Mar 20, 2003
41
US
Hi All, I wrote a simple script that telnets into a list hosts and issues a command. Everything works fine, but the script fails whenever there is a failed login attempt. I'd like to create a if condition inside my subroutine and when there is a failed authentication, the program simply skips to the next host in the list. Does anyone know what cndition I should use for the if statement? Below is my code, thanks all.

#!/usr/bin/perl -w
#
#program to clear counters on all backhaul circuits
#
#

use Net::Telnet();

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


print "Enter your username: ";
$username = <STDIN>;
chomp($username);

# read in password from file
open (PASS, &quot;passwd.txt&quot;) or die(&quot;Error, file cannot be opened&quot;);
$pass = <PASS>;
close (PASS);

# read in routers from shell script 'drhost'
open (HOST, &quot;/home/jschmoe/scripts/drhost |&quot;) or die(&quot;Error, unable to open file&quot;);

# loop through each host while read in from file
while (defined($router = <HOST>) )
{

chomp ($router);
subtelnet ($router); # call subroutine to log into router

}

print &quot;Done with script&quot;;

sub subtelnet
{
$telnet-> open (&quot;$router&quot;);
$telnet-> login ($username, $pass);
$telnet-> cmd (&quot;clear counter&quot;);
$telnet-> cmd (&quot;yes&quot;);
$telnet-> cmd (&quot;exit&quot;);
}
 
You need to change the error mode I think. The default is 'die' and you have to change it if you want an alternative response to a failed login. Try:

$mode=$telnet->errmode;
$mymode='return';
$prev=$telnet->errmode($mymode);
$newmode=$telnet->errmode;

You will obviously need to remove the 'or die...' in your script also.
 
Thanks for the response.....I'll include that in the code and see if it fixes the error :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top