Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Telnet File Handle isn't open on login

by VinsWorldcom (Priest)
on Sep 24, 2013 at 21:41 UTC ( #1055547=note: print w/ replies, xml ) Need Help??


in reply to Telnet File Handle isn't open on login

Any code would be most appreciated. Thank you


Comment on Re: Telnet File Handle isn't open on login
Re^2: Telnet File Handle isn't open on login
by jbev2328 (Initiate) on Sep 24, 2013 at 22:29 UTC
    Right. Of course.
    sub GetConfig { my $telnet=new Net::Telnet(); $telnet->output_log; $telnet->timeout($login_timeout); $telnet->errmode('return'); $telnet->prompt('/[\$#>]/'); $telnet->open( $deviceIP ); if( $telnet->errmsg ) { $msg = "Error connecting to device: ".$telnet->errmsg; $telnet->close; return( 252, $msg ); } #first try login without username and just password $telnet->waitfor( Match => '/Password:/'); if( $telnet->errmsg ) { $telnet->login($user, $pass); if( $telnet->errmsg ) { $msg = $telnet->errmsg; $telnet->close; return( 251, $msg ); } } else { $telnet->cmd( $pass ); if( $telnet->errmsg ) { # can't use errmsg as it will give command timed-out # and we should really indicate bad password $msg = "login failed: bad password"; $telnet->close; return( 251, $msg ); } }
    I'm getting a Return Code 251 every time with the message "Login Failed. File Handle isn't open on Login"

      I'm sure you're seeing some problem but I'm pretty sure it isn't what you're telling us.

      First off, the code you posted won't work - it's not complete. See How do I post a question effectively?.

      I've grep'd Net::Telnet for the error message "File Handle isn't open on Login" you're presumably getting from you first "return( 251, $msg );" line as there really is no other place in the code I see that could generate that message. I further grep'd my entire perl/lib, perl/site/lib and perl/vendor/lib directories for that message in some module and I don't find it.

      Please provide (brief) working code that demonstrates the problem.

      In the meantime, show us a screen cut/paste of a console Telnet to the device in question so we can rule out connecting to the device as an issue and prove it's the Perl code.

        here is the script I'm trying to get to work. The main operational part is the subroutine called 'GetConfig'

        #!/opt/SPECTRUM/bin/perl -w # This script will capture the startup configuration of a # Cisco BG OS device and print it to STDOUT. # # Error Codes: # 0 = Success # 255 = Usage error # 254 = Invalid timeout value # 252 = Connection error # 251 = Login error # 249 = Enable error # 244 = Error retrieving configuration # 253 = Unexpected output # use strict; use warnings; use Net::Telnet; ### Main ### if( $#ARGV != 4 && $#ARGV != 5 ) { print "Usage: capture_startup.pl <device IP> <user> <pass> <enable +_pass> <login_timeout_in_seconds> <capture_timeout_in_seconds>\n"; print STDERR "Usage: capture_startup.pl <deviceIP> <user> <pass> +<enable_pass> <login_timeout_in_seconds> <capture_timeout_in_seconds> +\n"; exit 255; } elsif( $ARGV[4] < 1 || $ARGV[4] > 600 ) { print "$ARGV[4] is the login timeout and must be an int between 1 +and 600 seconds\n"; print STDERR "$ARGV[4] is the login timeout and must be an int bet +ween 1 and 600 seconds\n"; exit 254; } elsif( $#ARGV == 5 && ( $ARGV[5] < 1 || $ARGV[5] > 600 ) ) { print "$ARGV[5] is the capture timeout and must be an int between +1 and 600 seconds\n"; print STDERR "$ARGV[5] is the capture timeout and must be an int b +etween 1 and 600 seconds\n"; exit 254; } else { my $capture_timeout = $ARGV[4]; if( $ARGV[5] ) { $capture_timeout = $ARGV[5]; } my $errorCode = 1; my @data; my $errorString = "\nHost $ARGV[0]: \n"; ($errorCode,@data) = GetConfig( $ARGV[0], $ARGV[1], $ARGV[2], $ARG +V[3], $ARGV[4], $capture_timeout ); if( $errorCode == 0 ) { # Success. The startup configuration # content is in the data variable for( @data ) { print }; # print the configuration to STDOUT exit 0; } else { print STDERR $errorString; if( $errorCode == 253 ) { print STDERR join " ", @data, "\nEnable password may be in +valid\n"; } else { print STDERR join " ", @data, "\n"; } exit $errorCode; } } exit 0; sub GetConfig { my $deviceIP=shift; my $user=shift; my $pass=shift; my $epass=shift; my $login_timeout=shift; my $capture_timeout=shift; my @config; my $msg; my $telnet=new Net::Telnet(); # $telnet->output_log; $telnet->timeout($login_timeout); $telnet->errmode('return'); $telnet->prompt('/[\$#>]/'); $telnet->open( $deviceIP ); if( $telnet->errmsg ) { $msg = "Error connecting to device: ".$telnet->errmsg; $telnet->close; return( 252, $msg ); } #first try login without username and just password $telnet->waitfor( Match => '/Password:/'); if( $telnet->errmsg ) { $telnet->login($user, $pass); if( $telnet->errmsg ) { $msg = $telnet->errmsg; $telnet->close; return( 251, $msg ); } } else { $telnet->cmd( $pass ); if( $telnet->errmsg ) { # can't use errmsg as it will give command timed-out # and we should really indicate bad password $msg = "login failed: bad password"; $telnet->close; return( 251, $msg ); } } $telnet->timeout( $capture_timeout); $telnet->print( "enable" ); # check for auto enable after logging in $telnet->waitfor( Match => '/#/i' ); if( $telnet->errmsg ) { $telnet->waitfor( Match => '/Password: $/i'); if( $telnet->errmsg ) { # can't use errmsg as it will be pattern match timed-out $msg = "No password prompt after 'enable' command"; $telnet->close( ); return( 249, $msg ); } $telnet->cmd( $epass ); if( $telnet->errmsg ) { # can't use errmsg as it will give command timed-out # and we should really indicate bad password $msg = "Enable failed: bad password"; $telnet->close( ); return( 249, $msg ); } } # disabe paging # different commands for different devices, if they don't # work then we will get messages about problems later # specifically the "No prompt after 'sh start'" error # errmsg doesn't get set when these error and if we use print # and getlines to read for errors it causes problems with print "s +h start" # later. #$telnet->cmd("term pager 0"); $telnet->cmd("term length 0"); $telnet->print( "sh start" ); if( $telnet->errmsg ) { $msg = "Device did not accept 'sh start' command: ".$telnet-> +errmsg; $telnet->close( ); return( 244, $msg ); } while( my $line = $telnet->getline() ) { # get configuration content if( $line !~ /sh start|Building configuration|Current configuration|^\s +*$/ ) { push @config, $line; } } $telnet->waitfor(Match => '/[\$#>]/'); if( $telnet->errmsg ) { $msg = "No prompt after 'sh start': ".$telnet->errmsg; $telnet->close( ); return( 244, $msg ); } if( @config <= 0 ) { $msg = "No data retrieved, the capture timeout may be too low. +"; $telnet->close(); return( 244, $msg ); } if( scalar grep {$_ =~ /^%/} @_ ) { # Ensure show start actually returned the config and not an er +ror message containing '%' return( 253, @config); } return( 0, @config); # everything was okay, return the captured da +ta }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055547]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2014-09-03 04:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (35 votes), past polls