mdavis has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a Perl script to collect data from a collection of Cisco switches spread over a corporate LAN. The problem is that I only have Telnet access to the corporate firewall router from outside. What I want to do is telnet into the firewall router, then telnet from there to the switches to do my data collection. I can do it via regular telnet - and I've gotten Net::Telnet::Cisco to take me as far as enabled mode on the router, but I can't figure out how to telnet from the router to a switch. Can anyone give me some pointers as to how I might accomplish this? Thanks much.
  • Comment on Using Net::Telnet::Cisco toTelnet from Host to Host

Replies are listed 'Best First'.
Re: Using Net::Telnet::Cisco toTelnet from Host to Host
by cleen (Pilgrim) on Jun 28, 2001 at 23:00 UTC
    You cant do this with Net::Telnet::Cisco the way it sounds like you are wanting it to act...You may have to go into somthing like this using just Socket...Somthing like this:
    #!/usr/bin/perl use Socket; # no its a quicky, i didnt use strict..its hard to write # code in this text box $HOSTNAME = "hostname.of.router"; $TELNETTO = "where.to.telnet.to.from.hostname"; $USER = "username"; $PASS = "password"; while (1) { socket(SOCK,PF_INET,SOCK_STREAM,getprotobyname('tcp')); connect(SOCK, sockaddr_in(23,inet_aton("$HOSTNAME"))); select(SOCK); $|=1; select('stdout'); print SOCK "$USER\n"; while(<SOCK>) { ($stuff) = split(/\s+$/,$_); if ($stuff =~ /Password:/) { print SOCK "$PASS\n"; } if ($stuff =~ /\>$/) { # we have user access print SOCK "telnet $TELNETTO"; # do some matching on its prompt } } exit; }
    hope this helps a bit...its kinda what I had to do to get extended ping sweeps to work before.
Re: Using Net::Telnet::Cisco toTelnet from Host to Host
by McD (Chaplain) on Jul 01, 2001 at 23:16 UTC
    Piece o' cake. What you want to do is use a local script to telnet to host1, then from that host telnet to host2, and capture command output, right?

    Simply create a telnet object, establishing a connection to host1, print a telnet command to host2 via that object, then tell the object to login again, this time with host2's settings.

    I tested this with Net::Telnet, but I suspect it would work with ::Cisco as well. You might also add code to logout gracefully.

    #!/perl -w use strict; use Net::Telnet; # Cavaets: # # 1. This simple example assumes that the login, password, and command # prompts are all able to be matched by the Net::Telnet defaults - if # not, you'll need to add the appropriate parameters to the login() # command when logging in to host2. # # 2. I'm not sure $! is going to be meaningful in my die() statements, # but what the heck - insert your own debugging if it's not working. # :-) # First host my $host1 = 'hostname1'; my $user1 = 'user1'; my $pass1 = 'password1'; # Second host my $host2 = 'hostname2'; my $user2 = 'user2'; my $pass2 = 'password2'; my $session = new Net::Telnet (host => $host1) or die "Couldn't create: $!"; $session->login($user1, $pass1) or die "Couldn't login to $host1: $!"; $session->print("telnet $host2") or die "Couldn't telnet to $host2: $! +"; $session->login($user2, $pass2) or die "Couldn't login to $host2: $!"; # Obviously, put your own commands in here - $session is now talking # to host2 via host1. my @output = $session->cmd('uptime') or die "Couldn't run command: $!" +; print "@output"; exit;