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

Net::Telnet::Cisco Alternate Credentials

Status
Not open for further replies.

pam196

Technical User
Jul 1, 2002
25
0
0
GB
Hi,

I'm in the process of writing a perl script to telnet to cisco 857/870 series routers. The script itself works fine if the initial username and password are correct. Unfortunately, the routers have 2 possible sets of credentials and I would like to provide an alternate set to my script. I'm not particulalry good with perl, but thought there would be a way of trapping the script error output and provide a new username and password.

The current error returned from a failed login is:
login failed: access denied or bad username at ./telnet_cisco_arp.pl line 47

Does anyone have any suggestions on how I might achieve trapping the error and replacing the username/password; or continuing with an alternate username/password? Script is below:

Code:
#!/usr/bin/perl -w
use warnings;
use Net::Telnet::Cisco;

## BB and related test constants
#############################################################################

use constant GREEN => 'green';
use constant YELLOW => 'yellow';
use constant RED => 'red';

## BB Global variables
#############################################################################

my $prev_run_log = "$ENV{BBTMP}/$ENV{MACHINE}.ciscoarp.data";
my $bbtest = 'arp';
my $color = GREEN;
my $hostname;
my $username = "myusername1";
my $passwd = "mypassword1";
my $status = $bbtest . " OK";
my ($bbhost,$slaves);
my (@ips,@hosts);


## Main Program
#############################################################################


## TEST STUFF HERE

open (BBHOSTGREP, $ENV{BBHOME} . "/bin/bbhostgrep " . "snmparp" . " |") or die;
               while (<BBHOSTGREP>) {
                       my ($ip,$host);
                       ($ip,$host) = split ' ';
                       print "DEBUG Adding $host $ip to server list\n";
                       push @ips, $ip;
                       push @hosts, $host;
               }
               close BBHOSTGREP;

foreach $ip (@ips) {
        $bbhost = $ip;
        $hostname = $host;
        print "DEBUG Connecting to $bbhost\n";
        my $session = Net::Telnet::Cisco->new(Host => $bbhost,
                        Errmode => sub { warn $_[0] });
        $session->login($username, $passwd);
        @lines = $session->cmd("show arp");
        print @lines;
        my $DATA = "@lines";
        ## Send to Hobbit
        ######################################################################
        my $report_date = `/bin/date`;
        chomp($report_date);
        $host=shift(@hosts);
        print "DEBUG Sending $host DATA to Xymon\n";
        system("$ENV{BB} $ENV{BBDISP} 'status $host.$bbtest $color $report_date
- $status\n\n$DATA'\n");

}

Thanks for your time,
Phil
 
I believe you can just do

Code:
if (! $session->login($username, $passwd)) {
  $session->login($username2, $passwd2);
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Thanks for the tip, it quickly pointed me in the right direction. I ended up modifying the suggestion with the following:
Code:
if (! $session->login($username, $passwd)) {
                print "DEBUG Trying alternative login...\n";
                $session = ();
                print "DEBUG Connecting to $bbhost\n";
                $session = Net::Telnet::Cisco->new(Host => $bbhost,
                        Errmode => "return");
                $session->login($username2, $passwd2);
        }

Cheers,
Phil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top