net-telnet-cisco-1.10.orig/0040710000175000017500000000000007537631544016616 5ustar jfernandezjfernandeznet-telnet-cisco-1.10.orig/README0100600000175000017500000000333507452645154017476 0ustar jfernandezjfernandezNet::Telnet::Cisco ------------------ DESCRIPTION Net::Telnet::Cisco adds additional functionality to Net::Telnet that helps you automate Cisco router management and statistic gathering. DOCUMENTATION POD style documentation is included with each module. This is normally converted to a manual page and installed as part of the process. You should also be able to use the 'perldoc' utility to extract and read documentation from the module file directly. See Changes for recent changes. SUPPORT Main webpage http://NetTelnetCisco.sourceforge.net/ Mailing lists There are mailing lists for Net::Telnet::Cisco. To subscribe or view past messages, please visit the following URL: http://sourceforge.net/mail/?group_id=48856 Help/discussion forums View and partage in USENET/BBS style discussions at: http://sourceforge.net/forum/?group_id=48856 AVAILABILITY The library is available from CPAN: http://search.cpan.org/search?dist=Net-Telnet-Cisco http://www.cpan.org/authors/id/J/JO/JOSHUA The latest version is also available at: http://sourceforge.net/project/showfiles.php?group_id=48856 AUTHOR Joshua_Keroes@eli.net It would greatly amuse the author if you would send email to him and tell him how you are using Net::Telnet::Cisco. Thanks to all who have. (And no, you won't get any spam from me directly or indirectly, ever) COPYRIGHT AND LICENSE Copyright (c) 2000-2002 Joshua Keroes, Electric Lightwave Inc. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.net-telnet-cisco-1.10.orig/Cisco.pm0100711000175000017500000010712607503665417020223 0ustar jfernandezjfernandezpackage Net::Telnet::Cisco; #----------------------------------------------------------------- # Net::Telnet::Cisco - interact with a Cisco router # # $Id: Cisco.pm,v 1.52 2002/06/18 17:17:03 jkeroes Exp $ # # Todo: Add error and access logging. # # POD documentation at end of file. # #----------------------------------------------------------------- require 5.005; use strict; use Net::Telnet 3.02; use AutoLoader; use Carp; use vars qw($AUTOLOAD @ISA $VERSION $DEBUG); @ISA = qw(Net::Telnet); $VERSION = '1.10'; $^W = 1; $DEBUG = 0; $|++; #------------------------------ # Public Methods #------------------------------ sub new { my $class = shift; my ($self, $host, %args); # Add default prompt to args if none present. push @_, (-Prompt => '/(?m:^[\w.-]+\s?(?:\(config[^\)]*\))?\s?[\$#>]\s?(?:\(enable\))?\s*$)/') unless grep /^-?prompt$/i, @_; # There's a new cmd_prompt in town. $self = $class->SUPER::new(@_) or return; *$self->{net_telnet_cisco} = { last_prompt => '', last_cmd => '', always_waitfor_prompt => 1, waitfor_pause => 0.1, autopage => 1, more_prompt => '/(?m:^\s*--More--)/', normalize_cmd => 1, send_wakeup => 0, ignore_warnings => 0, warnings => '/(?mx:^% Unknown VPN |^%IP routing table VRF.* does not exist. Create first$ |^%No CEF interface information |^%No matching route to delete$ |^%Not all config may be removed and may reappear after reactivating/ )/', }; ## Parse the args. if (@_ == 2) { # one positional arg given $host = $_[1]; } elsif (@_ > 2) { # named args ## Get the named args. %args = @_; ## Parse the errmode named arg first. foreach (keys %args) { $self->errmode($args{$_}) if /^-?errmode$/i; } ## Parse all other named args. foreach (keys %args) { if (/^-?always_waitfor_prompt$/i) { $self->always_waitfor_prompt($args{$_}); } elsif (/^-?waitfor_pause$/i) { $self->waitfor_pause($args{$_}); } elsif (/^-?more_prompt$/i) { $self->more_prompt($args{$_}); } elsif (/^-?autopage$/i) { $self->autopage($args{$_}); } elsif (/^-?normalize_cmd$/i) { $self->normalize_cmd($args{$_}); } elsif (/^-?send_wakeup$/i) { $self->send_wakeup($args{$_}); } } } $self; } # end sub new # The new prompt() stores the last matched prompt for later # fun 'n amusement. You can access this string via $self->last_prompt. # # It also parses out any router errors and stores them in the # correct place, where they can be acccessed/handled by the # Net::Telnet error methods. # # No POD docs for prompt(); these changes should be transparent to # the end-user. sub prompt { my( $self, $prompt ) = @_; my( $prev, $stream ); $stream = $ {*$self}{net_telnet_cisco}; $prev = $self->SUPER::prompt; ## Parse args. if ( @_ == 2 ) { defined $prompt or $prompt = ''; $self->_match_check($prompt); $self->SUPER::prompt($prompt); } elsif (@_ > 2) { return $self->error('usage: $obj->prompt($match_op)'); } return $prev; } # end sub prompt # cmd() now parses errors and sticks 'em where they belong. # # This is a routerish error: # routereast#show asdf # ^ # % Invalid input detected at '^' marker. # # "show async" is valid, so the "d" of "asdf" raised an error. # # If an error message is found, the following error message # is sent to Net::Telnet's error()-handler: # # Last command and router error: # # sub cmd { my $self = shift; my $ok = 1; my $normalize = $self->normalize_cmd; # Parse args if (@_ == 1) { $ {*$self}{net_telnet_cisco}{last_cmd} = $_[0]; } elsif ( @_ >= 2 ) { my @args = @_; while (my ($k, $v) = splice @args, 0, 2) { $ {*$self}{net_telnet_cisco}{last_cmd} = $v if $k =~ /^-?[Ss]tring$/; $normalize = $v if $k =~ /^-?[Nn]ormalize_cmd$/; } } my $cmd = $ {*$self}{net_telnet_cisco}{last_cmd}; my $old_ors = $self->output_record_separator; my $need_more = 0; my @out; while(1) { # Send a space (with no newline) whenever we see a "More" prompt. if ($need_more) { $self->output_record_separator(''); # We saw a more prompt, so put it in the command output. my @tmp = $self->last_prompt; # Send the , taking care not to # discard the top line. push @tmp, $self->SUPER::cmd(String => " ", Cmd_remove_mode => 0); if ($self->normalize_cmd) { push @out, _normalize(@tmp); } else { push @out, @tmp; } } else { $self->output_record_separator($old_ors); push @out, $self->SUPER::cmd(@_); } # Look for errors in output for ( my ($i, $lastline) = (0, ''); $i <= $#out; $lastline = $out[$i++] ) { # This may have to be a pattern match instead. if ( ( substr $out[$i], 0, 1 ) eq '%' ) { if ( $out[$i] =~ /'\^' marker/ ) { # Typo & bad arg errors chomp $lastline; $self->error( join "\n", "Last command and router error: ", ( $self->last_prompt . $cmd ), $lastline, $out[$i], ); splice @out, $i - 1, 3; } else { # All other errors. chomp $out[$i]; $self->error( join "\n", "Last command and router error: ", ( $self->last_prompt . $cmd ), $out[$i], ); splice @out, $i, 2; } $ok = 0; last; } } # Restore old settings $self->output_record_separator($old_ors); # redo the while loop if we saw a More prompt. my $more_re = $self->re_sans_delims($self->more_prompt); if ($self->autopage && $self->last_prompt =~ /$more_re/) { $need_more = 1; } else { last; } } return wantarray ? @out : $ok; } # waitfor now stores prompts to $obj->last_prompt() sub waitfor { my $self = shift; return unless @_; # $all_prompts will be built into a regex that matches all currently # valid prompts. # # -Match args will be added to this regex. The current prompt will # be appended when all -Matches have been exhausted. my $all_prompts = ''; # Literal string matches, passed in with -String. my @literals = (); # Parse the -Match => '/prompt \$' type options # waitfor can accept more than one -Match argument, so we can't just # hashify the args. if (@_ >= 2) { my @args = @_; while ( my ($k, $v) = splice @args, 0, 2 ) { if ($k =~ /^-?[Ss]tring$/) { push @literals, $v; } elsif ($k =~ /^-?[Mm]atch$/) { $all_prompts = $self->prompt_append($all_prompts, $v); } } } elsif (@_ == 1) { # A single argument is always a -match. $all_prompts = $self->prompt_append($all_prompts, $_[0]); } my $all_re = $self->re_sans_delims($all_prompts); my $prompt_re = $self->re_sans_delims($self->prompt); my $more_re = $self->re_sans_delims($self->more_prompt); # Add the current prompt if it's not already there. You can turn this behavior # off by setting always_waitfor_prompt to a false value. if ($self->always_waitfor_prompt && index($all_re, $prompt_re) == -1) { unshift @_, "-Match" if @_ == 1; push @_, (-Match => $self->prompt); $all_prompts = $self->prompt_append($all_prompts, $self->prompt); $all_re = $self->re_sans_delims($all_prompts); } # Add the more prompt if it's not present. See the autopage() docs # to turn this behaviour off. if ($self->autopage && index($all_re, $more_re) == -1) { unshift @_, "-Match" if @_ == 1; push @_, (-Match => $self->more_prompt); $all_prompts = $self->prompt_append($all_prompts, $self->more_prompt); $all_re = $self->re_sans_delims($all_prompts); } return $self->error("Godot ain't home - waitfor() isn't waiting for anything.") unless $all_prompts || @literals; # There's a timing issue that I can't quite figure out. # Adding a small pause here seems to make it go away. select undef, undef, undef, $self->waitfor_pause; my ($prematch, $match) = $self->SUPER::waitfor(@_); # If waitfor saw a prompt then store it. if ($match) { for (@literals) { if (index $match, $_) { return wantarray ? ($prematch, $match) : 1; } } if ($match =~ /($all_re)/m ) { $ {*$self}{net_telnet_cisco}{last_prompt} = $1; return wantarray ? ($prematch, $match) : 1; } } return wantarray ? ( $prematch, $match ) : 1; } sub login { my($self) = @_; my( $cmd_prompt, $endtime, $error, $lastline, $match, $orig_errmode, $orig_timeout, $prematch, $reset, $timeout, $usage, $sent_wakeup, ); my ($username, $password, $tacpass, $passcode ) = ('','','',''); my (%args, %seen); local $_; ## Init vars. $timeout = $self->timeout; $self->timed_out(''); return if $self->eof; $cmd_prompt = $self->prompt; $sent_wakeup = 0; print "login:\t[orig: $cmd_prompt]\n" if $DEBUG; $usage = 'usage: $obj->login([Name => $name,] [Password => $password,] ' . '[Passcode => $passcode,] [Prompt => $matchop,] [Timeout => $secs,])'; if (@_ == 3) { # just username and passwd given ($username, $password) = (@_[1,2]); } else { # named args given ## Get the named args. (undef, %args) = @_; ## Parse the named args. foreach (keys %args) { if (/^-?name$/i) { $username = $args{$_}; } elsif (/^-?passw/i) { $password = $args{$_}; } elsif (/^-?passcode/i) { $passcode = $args{$_}; } elsif (/^-?prompt$/i) { # login() always looks for a cmd_prompt. This is not # controllable via always_waitfor_prompt(). $cmd_prompt = $self->prompt_append($cmd_prompt, $args{$_}); } elsif (/^-?timeout$/i) { $timeout = _parse_timeout($args{$_}); } else { return $self->error($usage); } } } print "login:\t[after args: $cmd_prompt]\n" if $DEBUG; ## Override these user set-able values. $endtime = _endtime($timeout); $orig_timeout = $self->timeout($endtime); $orig_errmode = $self->errmode; ## Create a subroutine to reset to original values. $reset = sub { $self->errmode($orig_errmode); $self->timeout($orig_timeout); 1; }; ## Create a subroutine to generate an error for user. $error = sub { my($errmsg) = @_; &$reset; if ($self->timed_out) { return $self->error($errmsg); } elsif ($self->eof) { ($lastline = $self->lastline) =~ s/\n+//; return $self->error($errmsg, ": ", $lastline); } else { return $self->error($self->errmsg); } }; # Send a newline as the wakeup-call if ($self->send_wakeup eq 'connect') { $sent_wakeup = 1; my $old_sep = $self->output_record_separator; $self->output_record_separator("\n"); $self->print(''); $self->output_record_separator($old_sep); } while (1) { (undef, $_) = $self->waitfor( -match => '/(?:[Ll]ogin|[Uu]sername|[Pp]assw(?:or)?d)[:\s]*$/', -match => '/(?i:Passcode)[:\s]*$/', -match => $cmd_prompt, ); unless ($_) { return &$error("read eof waiting for login or password prompt") if $self->eof; # We timed-out. Send a newline as the wakeup-call. if ($sent_wakeup == 0 && $self->send_wakeup eq 'timeout') { $sent_wakeup = 1; my $old_sep = $self->output_record_separator; $self->output_record_separator("\n"); $self->print(''); $self->output_record_separator($old_sep); next; } return &$error("timed-out during login process"); } my $cmd_prompt_re = $self->re_sans_delims($cmd_prompt); if (not defined) { return $self->error("login failed: access denied or bad name, passwd, etc"); } elsif (/sername|ogin/) { $self->print($username) or return &$error("login disconnected"); $seen{login}++ && $self->error("login failed: access denied or bad username"); } elsif (/[Pp]assw/) { $self->print($password) or return &$error("login disconnected"); $seen{passwd}++ && $self->error("login failed: access denied or bad password"); } elsif (/(?i:Passcode)/) { $self->print($passcode) or return &$error("login disconnected"); $seen{passcode}++ && $self->error("login failed: access denied or bad passcode"); } elsif (/($cmd_prompt_re)/) { &$reset; # Success. Reset obj to default vals before continuing. last; } else { $self->error("login received unexpected prompt. Aborting."); } } 1; } # end sub login # Overridden to support ignore_warnings() sub error { my $self = shift; # Ignore warnings if ($self->ignore_warnings) { my $errmsg = join '', @_; my $warnings_re = $self->re_sans_delims($self->warnings); return if $errmsg =~ /$warnings_re/; } return $self->SUPER::error(@_); } # Tries to enter enabled mode with the password arg. sub enable { my $self = shift; my $usage = 'usage: $obj->enable([Name => $name,] [Password => $password,] ' . '[Passcode => $passcode,] [Level => $level] )'; my ($en_username, $en_password, $en_passcode, $en_level) = ('','','',''); my ($error, $lastline, $orig_errmode, $reset, %args, %seen); if (@_ == 1) { # just passwd given ($en_password) = shift; } else { # named args given %args = @_; foreach (keys %args) { if (/^-?name$|^-?login$|^-?user/i) { $en_username = $args{$_}; } elsif (/^-?passw/i) { $en_password = $args{$_}; } elsif (/^-?passc/i) { $en_passcode = $args{$_}; } elsif (/^-?level$/i) { $en_level = $args{$_}; } else { return $self->error($usage); } } } ## Create a subroutine to generate an error for user. $error = sub { my($errmsg) = @_; if ($self->timed_out) { return $self->error($errmsg); } elsif ($self->eof) { ($lastline = $self->lastline) =~ s/\n+//; return $self->error($errmsg, ": ", $lastline); } else { return $self->error($errmsg); } }; # Store the old prompt without the //s around it. my ($old_prompt) = $self->re_sans_delims($self->prompt); # We need to expect either a Password prompt or a # typical prompt. If the user doesn't have enough # access to run the 'enable' command, the device # won't even query for a password, it will just # ignore the command and display another [boring] prompt. $self->print("enable $en_level"); { my ($prematch, $match) = $self->waitfor( -match => '/[Ll]ogin[:\s]*$/', -match => '/[Uu]sername[:\s]*$/', -match => '/[Pp]assw(?:or)?d[:\s]*$/', -match => '/(?i:Passcode)[:\s]*$/', -match => "/$old_prompt/", ) or do { return &$error("read eof waiting for enable login or password prompt") if $self->eof; return &$error("timed-out waiting for enable login or password prompt"); }; if (not defined $match) { return &$error("enable failed: access denied or bad name, passwd, etc"); } elsif ($match =~ /sername|ogin/) { $self->print($en_username) or return &$error("enable failed"); $seen{login}++ && return &$error("enable failed: access denied or bad username"); redo; } elsif ($match =~ /[Pp]assw/ ) { $self->print($en_password) or return &$error("enable failed"); $seen{passwd}++ && return &$error("enable failed: access denied or bad password"); redo; } elsif ($match =~ /(?i:Passcode)/ ) { $self->print($en_passcode) or return &$error("enable failed"); $seen{passcode}++ && return &$error("enable failed: access denied or bad passcode"); redo; } elsif ($match =~ /$old_prompt/) { ## Success! Exit the block. last; } else { return &$error("enable received unexpected prompt. Aborting."); } } if (not defined $en_level or $en_level =~ /^[1-9]/) { # Prompts and levels over 1 give a #/(enable) prompt. return $self->is_enabled ? 1 : &$error('Failed to enter enable mode'); } else { # Assume success return 1; } } # Leave enabled mode. sub disable { my $self = shift; $self->cmd('disable'); return $self->is_enabled ? $self->error('Failed to exit enabled mode') : 1; } # Send control-^ (without newline) sub ios_break { my $self = shift; my $old_ors = $self->output_record_separator; $self->output_record_separator(''); my $ret = $self->print("\c^"); $self->output_record_separator($old_ors); return $ret; } # Displays the last prompt. sub last_prompt { my $self = shift; my $stream = $ {*$self}{net_telnet_cisco}; exists $stream->{last_prompt} ? $stream->{last_prompt} : undef; } # Displays the last command. sub last_cmd { my $self = shift; my $stream = $ {*$self}{net_telnet_cisco}; exists $stream->{last_cmd} ? $stream->{last_cmd} : undef; } # Examines the last prompt to determine the current mode. # Some prompts may be hard set to #, so this won't always return a valid answer. # Call 'show priv' instead. # 1 => enabled. # undef => not enabled. sub is_enabled { $_[0]->last_prompt =~ /\#|enable|config/ ? 1 : undef } # Typical get/set method. sub always_waitfor_prompt { my ($self, $arg) = @_; my $stream = $ {*$self}{net_telnet_cisco}; $stream->{always_waitfor_prompt} = $arg if defined $arg; return $stream->{always_waitfor_prompt}; } # Typical get/set method. sub waitfor_pause { my ($self, $arg) = @_; my $stream = $ {*$self}{net_telnet_cisco}; $stream->{waitfor_pause} = $arg if defined $arg; return $stream->{waitfor_pause}; } # Typical get/set method. sub autopage { my ($self, $arg) = @_; my $stream = $ {*$self}{net_telnet_cisco}; $stream->{autopage} = $arg if defined $arg; return $stream->{autopage}; } # Typical get/set method. sub normalize_cmd { my ($self, $arg) = @_; my $stream = $ {*$self}{net_telnet_cisco}; $stream->{normalize_cmd} = $arg if defined $arg; return $stream->{normalize_cmd}; } # Typical get/set method. sub send_wakeup { my ($self, $arg) = @_; my $stream = $ {*$self}{net_telnet_cisco}; $stream->{send_wakeup} = $arg if defined $arg; return $stream->{send_wakeup}; } # Typical get/set method. sub ignore_warnings { my ($self, $arg) = @_; my $stream = $ {*$self}{net_telnet_cisco}; $stream->{ignore_warnings} = $arg if defined $arg; return $stream->{ignore_warnings}; } # Get/set the More prompt sub more_prompt { my ($self, $arg) = @_; my $stream = $ {*$self}{net_telnet_cisco}; if (defined $arg) { $self->_match_check($arg); $stream->{more_prompt} = $arg; } return $stream->{more_prompt}; } # Join two or more regexen into one on "|". sub prompt_append { my $self = shift; my $orig = shift || ''; return $self->error("usage: \$obj->prompt_append(orig, new, [new...])") unless @_; print "prompt_append:\t[original: $orig]\n" if $DEBUG; if ($orig) { if ($self->_match_check($orig)) { $orig = $self->re_sans_delims($orig); return $self->error("Can't parse prompt: '$orig'") unless $orig; } } for (@_) { print "prompt_append:\t[append: $_]\n" if $DEBUG; if ($self->_match_check($_)) { my $re = $self->re_sans_delims($_); unless ($re) { $self->error("Can't parse prompt: '$_'"); next; } $orig .= $orig ? "|$re" : $re; } } print "prompt_append:\t[return: /$orig/]\n\n" if $DEBUG; return "/$orig/"; } # Return a Net::Telnet regular expression without the delimiters. sub re_sans_delims { my ($self, $str) = @_; return $self->error("usage: \$obj->re_sans_delims(\$matchop)") unless $str; $self->_match_check($str); my ($delim, $re) = $str =~ /^\s*m?\s*(\W)(.*)\1\s*$/; return $re; } #------------------------------ # Private methods #------------------------------ # strip backspaces, deletes, kills, and the character they # pertain to, from an array. sub _normalize { $_ = join "", @_; 1 while s/[^\cH\c?][\cH\c?]//mg; # ^H ^? s/^.*\cU//mg; # ^U return wantarray ? split /$/mg, $_ : $_; # ORS instead? } # Lifted from Net::Telnet en toto sub _match_check { my ($self, $code) = @_; return unless $code; my $error; my @warns = (); print "_match_check:\t[Checking: $code]\n" if $DEBUG; ## Use eval to check for syntax errors or warnings. { local $SIG{'__DIE__'} = 'DEFAULT'; local $SIG{'__WARN__'} = sub { push @warns, @_ }; local $^W = 1; local $_ = ''; eval "\$_ =~ $code;"; } if ($@) { ## Remove useless lines numbers from message. ($error = $@) =~ s/ at \(eval \d+\) line \d+.?//; chomp $error; return $self->error("bad match operator: $error"); } elsif (@warns) { ## Remove useless lines numbers from message. ($error = shift @warns) =~ s/ at \(eval \d+\) line \d+.?//; $error =~ s/ while "strict subs" in use//; chomp $error; return $self->error("bad match operator: $error"); } 1; } # end sub _match_check #------------------------------ # Class methods #------------------------------ # Look for subroutines in Net::Telnet if we can't find them here. sub AUTOLOAD { my ($self) = @_; croak "$self is an [unexpected] object, aborting" if ref $self; $AUTOLOAD =~ s/.*::/Net::Telnet::/; goto &$AUTOLOAD; } 1; __END__ #------------------------------------------------------------ # Docs #------------------------------------------------------------ =head1 NAME Net::Telnet::Cisco - interact with a Cisco router =head1 SYNOPSIS use Net::Telnet::Cisco; my $session = Net::Telnet::Cisco->new(Host => '123.123.123.123'); $session->login('login', 'password'); # Execute a command my @output = $session->cmd('show version'); print @output; # Enable mode if ($session->enable("enable_password") ) { @output = $session->cmd('show privilege'); print "My privileges: @output\n"; } else { warn "Can't enable: " . $session->errmsg; } $session->close; =head1 DESCRIPTION Net::Telnet::Cisco provides additional functionality to Net::Telnet for dealing with Cisco routers. cmd() parses router-generated error messages - the kind that begin with a '%' - and stows them in $obj-Eerrmsg(), so that errmode can be used to perform automatic error-handling actions. =head1 CAVEATS Before you use Net::Telnet::Cisco, you should have a good understanding of Net::Telnet, so read it's documentation first, and then come back here to see the improvements. Some things are easier to accomplish with UCD's C-based SNMP module, or the all-perl Net::SNMP. SNMP has three advantages: it's faster, handles errors better, and doesn't use any VTYs on the router. SNMP does have some limitations, so for anything you can't accomplish with SNMP, there's Net::Telnet::Cisco. =head1 METHODS =over 4 =item B - create new Net::Telnet::Cisco object $session = Net::Telnet::Cisco->new( [Autopage => $boolean,] # 1 [More_prompt => $matchop,] # '/(?m:^\s*--More--)/', [Always_waitfor_prompt => $boolean,] # 1 [Waitfor_pause => $milliseconds,] # 0.1 [Normalize_cmd => $boolean,] # 1 [Send_wakeup => $when,] # 0 [Ignore_warnings => $boolean,] # 0 [Warnings => $matchop,] # see docs # Net::Telnet arguments [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,] # see docs [Telnetmode => $mode,] [Timeout => $secs,] ); Creates a new object. Read `perldoc perlboot` if you don't understand that. =item B - login to a router $ok = $obj->login($username, $password); $ok = $obj->login([Name => $username,] [Password => $password,] [Passcode => $passcode,] # for Secur-ID/XTACACS [Prompt => $match,] [Timeout => $secs,]); All arguments are optional as of v1.05. Some routers don't ask for a username, they start the login conversation with a password request. =item B - send a command $ok = $obj->cmd($string); $ok = $obj->cmd(String => $string, [Output => $ref,] [Prompt => $match,] [Timeout => $secs,] [Cmd_remove_mode => $mode,]); @output = $obj->cmd($string); @output = $obj->cmd(String => $string, [Output => $ref,] [Prompt => $match,] [Timeout => $secs,] [Cmd_remove_mode => $mode,] [Normalize_cmd => $boolean,]); Normalize_cmd has been added to the default Net::Telnet args. It lets you temporarily change whether backspace, delete, and kill characters are parsed in the command output. (This is performed by default) =item B - return control to the program whenever this string occurs in router output $matchop = $obj->prompt; $prev = $obj->prompt($matchop); The default cmd_prompt changed in v1.05. It's suitable for matching prompts like C, C, C (enable) >, and C Let's take a closer look, shall we? (?m: # Net::Telnet doesn't accept quoted regexen (i.e. qr//) # so we need to use an embedded pattern-match modifier # to treat the input as a multiline buffer. ^ # beginning of line [\w.-]+ # router hostname \s? # optional space (?: # Strings like "(config)" and "(config-if)", "(config-line)", # and "(config-router)" indicate that we're in privileged \(config[^\)]*\) # EXEC mode (i.e. we're enabled). )? # The middle backslash is only there to appear my syntax # highlighter. \s? # more optional space [\$#>] # Prompts typically end with "$", "#", or ">". Backslash # for syntax-highlighter. \s? # more space padding (?: # Catalyst switches print "(enable)" when in privileged \(enable\) # EXEC mode. )? \s* # spaces before the end-of-line aren't important to us. $ # end of line ) # end of (?m: The default prompt published in 1.03 was C]\s?(?:\(enable\))?\s*$/>. As you can see, the prompt was drastically overhauled in 1.05. If your code suddenly starts timing out after upgrading Net::Telnet::Cisco, this is the first thing to investigate. =item B - enter enabled mode $ok = $obj->enable; $ok = $obj->enable($password); $ok = $obj->enable([Name => $name,] [Password => $password,] [Passcode => $passcode,] [Level => $level,]); This method changes privilege level to enabled mode, (i.e. root) If a single argument is provided by the caller, it will be used as a password. For more control, including the ability to set the privilege-level, you must use the named-argument scheme. enable() returns 1 on success and undef on failure. =item B - Am I root? $bool = $obj->is_enabled; A trivial check to see whether we have a root-style prompt, with either the word "(enable)" in it, or a trailing "#". B: this method will return false positives if your prompt has "#"s in it. You may be better off calling C<$obj-Ecmd("show privilege")> instead. =item B - leave enabled mode $ok = $obj->disable; This method exits the router's privileged mode. =item B - send a break (control-^) $ok = $obj->ios_break; You may have to use errmode(), fork, or threads to break at the an appropriate time. =item B - displays the last prompt matched by prompt() $match = $obj->last_prompt; last_prompt() will return '' if the program has not yet matched a prompt. =item B - waitfor and cmd prompt behaviour $boolean = $obj->always_waitfor_prompt; $boolean = $obj->always_waitfor_prompt($boolean); Default value: 1 If you pass a Prompt argument to cmd() or waitfor() a String or Match, they will return control on a successful match of your argument(s) or the default prompt. Set always_waitfor_prompt to 0 to return control only for your arguments. This method has no effect on login(). login() will always wait for a prompt. =item B - insert a small delay before waitfor() $boolean = $obj->waitfor_pause; $boolean = $obj->waitfor_pause($milliseconds); Default value: 0.1 In rare circumstances, the last_prompt is set incorrectly. By adding a very small delay before calling the parent class's waitfor(), this bug is eliminated. If you ever find reason to modify this from it's default setting, please let me know. =item B - Turn autopaging on and off $boolean = $obj->autopage; $boolean = $obj->autopage($boolean); Default value: 1 IOS pages output by default. It expects human eyes to be reading the output, not programs. Humans hit the spacebar to scroll page by page so autopage() mimicks that behaviour. This is the slow way to handle paging. See the Paging EXAMPLE for a faster way. =item B - Turn normalization on and off $boolean = $obj->normalize_cmd; $boolean = $obj->normalize_cmd($boolean); Default value: 1 IOS clears '--More--' prompts with backspaces (e.g. ^H). If you're excited by the thought of having raw control characters like ^H (backspace), ^? (delete), and ^U (kill) in your command output, turn this feature off. Logging is unaffected by this setting. =item B - Matchop used by autopage() $matchop = $obj->prompt; $prev = $obj->prompt($matchop); Default value: '/(?m:\s*--More--)/'. Please email me if you find others. =item B - send a newline to the router at login time $when = $obj->send_wakeup; $when = $obj->send_wakeup( 'connect' ); $when = $obj->send_wakeup( 'timeout' ); $when = $obj->send_wakeup( 0 ); Default value: 0 Some routers quietly allow you to connect but don't display the expected login prompts. Sends a newline in the hopes that this spurs the routers to print something. 'connect' sends a newline immediately upon connection. 'timeout' sends a newline if the connection timeouts. 0 turns this feature off. I understand this works with Livingston Portmasters. =item B - Don't call error() for warnings $boolean = $obj->ignore_warnings; $boolean = $obj->ignore_warnings($boolean); Default value: 0 Not all strings that begin with a '%' are really errors. Some are just warnings. By setting this, you are ignoring them. This will show up in the logs, but that's it. =item B - Matchop used by ignore_warnings(). $boolean = $obj->warnings; $boolean = $obj->warnings($matchop); Default value: /(?mx:^% Unknown VPN |^%IP routing table VRF.* does not exist. Create first$ |^%No CEF interface information |^%No matching route to delete$ |^%Not all config may be removed and may reappear after reactivating )/ Not all strings that begin with a '%' are really errors. Some are just warnings. Cisco calls these the CIPMIOSWarningExpressions. =back =head1 EXAMPLES =head2 Paging v1.08 added internal autopaging support to cmd(). Whenever a '--Page--' prompt appears on the screen, we send a space right back. It works, but it's slow. You'd be better off sending one of the following commands just after login(): # To a router $session->cmd('terminal length 0'); # To a switch $session->cmd('set length 0'); =head2 Logging Want to see the session transcript? Just call input_log(). e.g. my $session = Net::Telnet::Cisco->new(Host => $router, Input_log => "input.log", ); See input_log() in L for info. Input logs are easy-to-read translated transcripts with all of the control characters and telnet escapes cleaned up. If you want to view the raw session, see dump_log() in L. If you're getting tricky and using print() in addition to cmd(), you may also want to use output_log(). =head2 Big output Trying to dump the entire BGP table? (e.g. "show ip bgp") The default buffer size is 1MB, so you'll have to increase it. my $MB = 1024 * 1024; $session->max_buffer_length(5 * $MB); =head2 Sending multiple lines at once Some commands like "extended ping" and "copy" prompt for several lines of data. It's not necessary to change the prompt for each line. Instead, send everything at once, separated by newlines. For: router# ping Protocol [ip]: Target IP address: 10.0.0.1 Repeat count [5]: 10 Datagram size [100]: 1500 Timeout in seconds [2]: Extended commands [n]: Sweep range of sizes [n]: Try this: my $protocol = ''; # default value my $ip = '10.0.0.1'; my $repeat = 10; my $datagram = 1500; my $timeout = ''; # default value my $extended = ''; # default value my $sweep = ''; # default value $session->cmd( "ping $protocol $ip $repeat $datagram $timeout $extended $sweep "); If you prefer, you can put the cmd on a single line and replace every static newline with the "\n" character. e.g. $session->cmd("ping\n$protocol\n$ip\n$repeat\n$datagram\n" . "$timeout\n$extended\n$sweep\n"); =head2 Backup via TFTP Backs up the running-confg to a TFTP server. Backup file is in the form "router-confg". Make sure that file exists on the TFTP server or the transfer will fail! my $backup_host = "tftpserver.somewhere.net"; my $device = "cisco.somewhere.net"; my $type = "router"; # or "switch"; my $ios_version = 12; my @out; if ($type eq "router") { if ($ios_version >= 12) { @out = $session->cmd("copy system:/running-config " . "tftp://$backup_host/$device-confg\n\n\n"); } elsif ($ios_version >= 11) { @out = $session->cmd("copy running-config tftp\n$backup_host\n" . "$device-confg\n"); } elsif ($ios_version >= 10) { @out = $session->cmd("write net\n$backup_host\n$device-confg\n\n"); } } elsif ($type eq "switch") { @out = $session->cmd("copy system:/running-config " . "tftp://$backup_host/$device-confg\n\n\n"); } =head1 SUPPORT http://NetTelnetCisco.sourceforge.net/ =head2 Mailing lists I is for important security bulletins and upgrades. Very low traffic, no spam, B http://lists.sourceforge.net/lists/listinfo/nettelnetcisco-announce I is for usage discussion, help, tips, tricks, etc. http://lists.sourceforge.net/lists/listinfo/nettelnetcisco-users I is for uber-hackers; you know who you are. http://lists.sourceforge.net/lists/listinfo/nettelnetcisco-devel =head2 Help/discussion forums http://sourceforge.net/forum/?group_id=48856 =head2 Bug tracker http://sourceforge.net/tracker/?group_id=48856 =head1 SEE ALSO L L UCD NetSNMP - http://www.netsnmp.org/ RAT/NCAT - http://ncat.sourceforge.net/ =head1 AUTHOR Joshua_Keroes@eli.net $Date: 2002/06/18 17:17:03 $ It would greatly amuse the author if you would send email to him and tell him how you are using Net::Telnet::Cisco. As of Mar 2002, 170 people have emailed me. N::T::C is used to help manage over 14,000 machines! Keep the email rolling in! =head1 THANKS The following people understand what Open Source Software is all about. Thanks Brian Landers, Aaron Racine, Niels van Dijke, Tony Mueller, Frank Eickholt, Al Sorrell, Jebi Punnoose, Christian Alfsen, Niels van Dijke, Kevin der Kinderen, Ian Batterbee, Leonardo Cont, Steve Meier, and Andre Bonhote. Institutions: infobot.org #perl, perlmonks.org, sourceforge.net, the geeks at geekhouse.org, and eli.net. Send in a patch and we can make the world a better place. =head1 COPYRIGHT AND LICENSE Copyright (c) 2000-2002 Joshua Keroes, Electric Lightwave Inc. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. net-telnet-cisco-1.10.orig/.cvsignore0100600000175000017500000000003407503665356020612 0ustar jfernandezjfernandezblib Makefile pm_to_blib ot net-telnet-cisco-1.10.orig/MANIFEST0100600000175000017500000000013307503666013017732 0ustar jfernandezjfernandez.cvsignore Changes Cisco.pm INSTALL MANIFEST MANIFEST.SKIP Makefile.PL README TODO test.pl net-telnet-cisco-1.10.orig/test.pl0100711000175000017500000001521607452377622020140 0ustar jfernandezjfernandez# $Id: test.pl,v 1.19 2002/04/02 18:59:30 jkeroes Exp $ # # Before `make install' is performed this script should be runnable with # `make test'. After `make install' it should work as `perl test.pl' use Test::More tests => 32; #use Test::More qw/no_plan/; use ExtUtils::MakeMaker qw/prompt/; use Term::ReadKey; use Carp; use Cwd; use vars qw/$ROUTER $PASSWD $LOGIN $S $EN_PASS $PASSCODE/; my $input_log = "input.log"; my $dump_log = "dump.log"; #------------------------------------------------------------ # tests #------------------------------------------------------------ get_login(); BEGIN { use_ok("Net::Telnet::Cisco") } ok($Net::Telnet::Cisco::VERSION, "\$VERSION set"); SKIP: { skip("Won't login to router without a login and password.", 27) unless $LOGIN && $PASSWD; ok( $S = Net::Telnet::Cisco->new( Errmode => \&fail, Host => $ROUTER, Input_log => $input_log, Dump_log => $dump_log, ), "new() object" ); $S->errmode(sub {&confess}); # So we pass an even number of args to login() $LOGIN ||= ''; $PASSWD ||= ''; $PASSCODE ||= ''; ok( $S->login(-Name => $LOGIN, -Password => $PASSWD, -Passcode => $PASSCODE), "login()" ); # Autopaging tests ok( $S->autopage, "autopage() on" ); my @out = $S->cmd('show ver'); ok( $out[-1] !~ /--More--/, "autopage() last line" ); ok( $S->last_prompt !~ /--More--/, "autopage() last prompt" ); open LOG, "< $input_log" or die "Can't open log: $!"; my $log = join "", ; close LOG; # Remove last prompt, which isn't present in @out $log =~ s/\cJ\cJ.*\Z//m; # get rid of "show ver" line shift @out; # Strip ^Hs from log $log = Net::Telnet::Cisco::_normalize($log); my $out = join "", @out; $out =~ s/\cJ\cJ.*\Z//m; my $i = index $log, $out; ok( $i + length $out == length $log, "autopage() 1.09 bugfix" ); # Turn off autopaging. We should timeout with a More prompt # on the last line. ok( $S->autopage(0) == 0, "autopage() off" ); $S->errmode('return'); # Turn off error handling. $S->errmsg(''); # We *want* this to timeout. $S->cmd(-String => 'show run', -Timeout => 5); ok( $S->errmsg =~ /timed-out/, "autopage() not called" ); $S->errmode(\&fail); # Restore error handling. $S->cmd("\cZ"); # Cancel out of the "show run" # Print variants ok( $S->print('terminal length 0'), "print() (unset paging)"); ok( $S->waitfor($S->prompt), "waitfor() prompt" ); ok( $S->cmd('show clock'), "cmd() short" ); ok( $S->cmd('show ver'), "cmd() medium" ); ok( @confg = $S->cmd('show run'), "cmd() long" ); # breaks $old_timeout = $S->timeout; $S->timeout(1); $S->errmode(sub { $S->ios_break }); @break_confg = $S->cmd('show run'); $S->timeout($old_timeout); ok( @break_confg < @confg, "ios_break()" ); # Error handling my $seen; ok( $S->errmode(sub {$seen++}), "set errmode(CODEREF)" ); $S->cmd( "Small_Change_got_rained_on_with_his_own_thirty_eight" . "_And_nobody_flinched_down_by_the_arcade"); # $seen should be incrememnted to 1. ok( $seen, "error() called" ); # $seen should not be incremented (it should remain 1) ok( $S->errmode('return'), "no errmode()" ); $S->cmd( "Brother_my_cup_is_empty_" . "And_I_havent_got_a_penny_" . "For_to_buy_no_more_whiskey_" . "I_have_to_go_home"); ok( $seen == 1, "don't call error()" ); ok( $S->always_waitfor_prompt(1), "always_waitfor_prompt()" ); ok( $S->print("show clock") && $S->waitfor("/not_a_real_prompt/"), "waitfor() autochecks for prompt()" ); ok( $S->always_waitfor_prompt(0) == 0, "don't always_waitfor_prompt()" ); ok( $S->timeout(5), "set timeout to 5 seconds" ); ok( $S->print("show clock") && $S->waitfor("/not_a_real_prompt/") && $S->timed_out, "waitfor() timeout" ); # restore errmode to test default. $S->errmode(sub {&fail}); ok( $S->cmd("show clock"), "cmd() after waitfor()" ); # log checks ok( -e $input_log, "input_log() created" ); ok( -e $dump_log, "dump_log() created" ); $S = Net::Telnet::Cisco->new( Prompt => "/broken_pre1.08/" ); ok( $S->prompt eq "/broken_pre1.08/", "new(args) 1.08 bugfix" ); } SKIP: { skip("Won't enter enabled mode without an enable password", 3) unless $LOGIN && $PASSWD && $EN_PASS; ok( $S->disable, "disable()" ); ok( $S->enable($EN_PASS), "enable()" ); ok( $S->is_enabled, "is_enabled()" ); } END { cleanup() }; #------------------------------------------------------------ # subs #------------------------------------------------------------ sub cleanup { return unless -f "input.log" || -f "dump.log"; print <); } } else { print "$def\n"; } return ($ans ne '') ? $ans : $def; } net-telnet-cisco-1.10.orig/MANIFEST.SKIP0100600000175000017500000000014507461341747020511 0ustar jfernandezjfernandezCVS/.* \.bak$ \.tar$ \.tgz$ \.tar\.gz$ ^tmp/ ^blib/ ^Makefile$ ^Makefile\.[a-z]+$ ^pm_to_blib$ ~$ ^# net-telnet-cisco-1.10.orig/Changes0100711000175000017500000000510107503666653020111 0ustar jfernandezjfernandez$Id: Changes,v 1.19 2002/06/18 16:37:02 jkeroes Exp $ Revision history for Perl extension Net::Telnet::Cisco. 1.0 Fri Jul 14 11:11:42 PDT 2000 - Initial release 1.01 Fri Jul 14 15:07:00 PDT 2000 - Fixed CPAN installation issues 1.02 Mon Jul 24 16:22:11 PDT 2000 - enable() enables. - Simplified disable(). - prompt() and is_enabled() handle prompts with '(enable)' in them. - Added Windows installation tips to INSTALL. - test.pl keeps a log (test.log) of the session if there were errors. 1.03 Sun Jul 30 14:58:44 PDT 2000 - Found and fixed bug in enable(), hopefully forever. - cmd() and waitfor() properly handle multiple args. - waitfor() handles -Match args with m... notation. - Fixed occasional bug where last_prompt would return a regex matching a prompt and not the prompt itself. - Added enable() block to POD's Synopsis. - Added "new" to the constructor in POD's Synopsis. - Improved default prompt: Old: /[\w\s().-]*[\$#>]\s?(?:\(enable\))?\s*$/' New: /[\w().-]*[\$#>]\s?(?:\(enable\))?\s*$/ 1.04 Thu Jan 25 15:49:57 PST 2001 - Aaron Racine submitted a patch for a prompt bug in enable() - Private release 1.05 Wed Aug 8 17:57:56 PDT 2001 - enable() accepts -Name, -Password, -Passcode, and -Level args - All args to login() are optional, including -Name - Reworked internals of login() and enable() - New EXAMPLES docs - New PIX firewall "PIX Passwd: " prompt support - New XTACACS/SecurID "PASSCODE: " prompt support - Default cmd_prompt now anchored to beginning of line with (?m). - New cmd_prompt, see docs. 1.06 Mon Jan 14 09:42:20 PST 2002 - Turned on warnings, minor related changes - Correctly return an error in waitfor() 1.07 Tue Jan 15 12:41:36 PST 200 - Bugfix from Leonardo Cont - used wrong errmsg in enable() 1.08 Wed Jan 30 15:49:26 PST 2002 - New feature: autopage() - Fixed argument handling in new() - Better internal prompt handling - Better error reporting from invalid prompts - Testing: Errmode set to \&Test::More::fail 1.09 Wed Mar 13 12:45:33 PST 2002 - Autopage bugfix - 1st line of every page after the 1st was missing - New method: normalize_cmd() - strips ^H, ^?, and ^U - Defaults now listed in docs - Project has a home: NetTelnetCisco.sourceforge.net - New method: ios_break() - sends control-^ - Using ExtUtils::MakeMaker::prompt in test.pl for noninteractive installs 1.10 Tue Jun 18 10:28:05 PDT 2002 - send_wakeup() written to help use module with Livingston Portmasters. - warnings() and ignore_warnings() allow some error-strings to be ignored. - fixed warning in prompt_append() net-telnet-cisco-1.10.orig/INSTALL0100600000175000017500000000453207503664333017644 0ustar jfernandezjfernandezINSTALL 1.1 Prerequisites Net::Telnet::Cisco relies on and extends Net::Telnet. CPAN and PPM will both automatically detect and install this module for you. + Net::Telnet 3.02 ------------------------------------------------------------ 1.2 Make and install 1.2.1 UNIX 1.2.1.1 CPAN Install 1. Run the CPAN shell from your command-line: $ perl -MCPAN -e shell 2. If this is your first time running CPAN, it will ask you a number of setup questions. 3. Install the module: cpan> install Net::Telnet::Cisco 1.2.1.2 Manual Install Failing those, download the module and install by hand: $ perl Makefile.PL [--prefix=...] $ make $ make test $ make install 1.2.2 Windows There are two ways to install Net::Telnet::Cisco on a Windows machine: 1.2.2.1 Install with PPM3, the ActiveState Perl Package Manager 1. run PPM3 2. type "rep add NTC http://prdownloads.sourceforge.net/nettelnetcisco/" (without the quotes) 3. type "install Net::Telnet::Cisco". For more information, read the PPM FAQ: http://aspn.activestate.com/ASPN/Reference/Products/ActivePerl/faq/ActivePerl-faq2.html 1.2.2.2 Install with PPM, v2, the older version 1. run PPM. 2. type "set repository NTC http://prdownloads.sourceforge.net/nettelnetcisco/" (without the quotes) 3. type "install Net::Telnet::Cisco". 1.2.2.2 Install with CPAN Windows needs an additional program called "nmake" to install modules with CPAN. 1. Get and install nmake from ftp://ftp.microsoft.com/Softlib/MSLFILES/nmake15.exe 2. Run CPAN from a DOS prompt: C:\WINDOWS\DESKTOP> perl -MCPAN -e shell 3. If this is your first time running CPAN, it will ask you a number of setup questions. When it asks you for the location of 'make', give it the path to 'nmake'. 4. install Net::Telnet::Cisco from CPAN: cpan> install Net::Telnet::Cisco 1.2.3 MacOS 1. If you have MacOS X, you can run the CPAN program from a Terminal: perl -MCPAN -e shell 2. If this is your first time running CPAN, it will ask you a number of setup questions. 3. Install the module: cpan> install Net::Telnet::Cisco 1.3 Troubleshooting Discuss on the mailing lists of forums at http://NetTelnetCisco.SourceForge.net/ Please include a script(1) typescript of the installation problems. $Id: INSTALL,v 1.2 2002/06/18 17:07:39 jkeroes Exp $ __END__ net-telnet-cisco-1.10.orig/Makefile.PL0100711000175000017500000000103407461317746020570 0ustar jfernandezjfernandezuse ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. WriteMakefile( 'NAME' => 'Net::Telnet::Cisco', 'VERSION_FROM' => 'Cisco.pm', # finds $VERSION 'PREREQ_PM' => { Net::Telnet => 3.02, Term::ReadKey => 2, Test::More => undef, Cwd => undef, }, # e.g., Module::Name => 1.1 ($] ge '5.005') ? ( 'AUTHOR' => 'Joshua Keroes (joshua@cpan.org)', 'ABSTRACT' => 'automate Cisco management', ) : (), ); net-telnet-cisco-1.10.orig/TODO0100600000175000017500000000031007503665756017303 0ustar jfernandezjfernandez- More TACACS support. - Start a telnet session to a router from within an existing telnet session. - Add scripts for common tasks. Got any? Please send them to the website, I'd like to catalog them.