Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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 }

In reply to Re^4: Telnet File Handle isn't open on login by jbev2328
in thread Telnet File Handle isn't open on login by jbev2328

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (2)
    As of 2017-12-16 10:14 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      What programming language do you hate the most?




















      Results (449 votes). Check out past polls.

      Notices?