Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Telnet into Cisco switches

by primus (Scribe)
on Jul 11, 2003 at 14:11 UTC ( [id://273369]=perlquestion: print w/replies, xml ) Need Help??

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

hail monks,

i am going to write a perl script that will telnet into Cisco 2650 Switches to configure some ports, and i was wondering if anyone has had any experience doing something similiar.

i was thinking of using the Net-Telnet-Cisco module on CPAN. has anyone used this module to telnet into a switch? it looks very promising, but i was just wondering if anyone had any experience with this.

thanks monks!

Replies are listed 'Best First'.
Re: Telnet into Cisco switches
by naChoZ (Curate) on Jul 11, 2003 at 15:06 UTC
    I've used it too. This is a snippet from my first perl script (that did anything useful).

    my $cs = new Net::Telnet::Cisco ( Errmode => "return", Timeout => 100, Prompt => '/>/' ); $cs->open("$ubr"); $cs->login( $userid, $password); $cs->cmd( 'terminal length 0'); @lines = $cs->cmd( "show cable modem | inc $mac" ); foreach my $line (@lines) { chomp $line; $line =~ s/ +/ /g; my @values=split(/ /,$line); push(@scmresults,@values); }

    ~~
    naChoZ

      thanks for the replies, but i am running into a login error... i tried to do $session->login(Password => $switch->{PASS1}); but doesnt login complaining about a bad password or something... when i do it by hand it works with that password, also there is no username, it just presents you with a password prompt.

      does it make a difference if i do:
      if ($session->login(Password => $switch->{PASS1})) {}
      of just:
      $session->login(Password => $switch->{PASS1});

        I am being lazy for the moment and not looking at the man page for Net::Telnet or Net::Telnet::Cisco.

        There is a way to get Net::Telnet to write an audit log of the conversation between your script and the device. If you invoke that you might get some idea of what is going wrong.


        Peter L. BergholdBrewer of Belgian Ales
        Peter@Berghold.Netwww.berghold.net
        Unix Professional
        here is my code, in case i am missing something obvious:
        my $session; $session = Net::Telnet::Cisco->new(Host => $switch->{IP}); $session->login(Password => $switch->{PASS1}); $session->close();
        well, i think that the login routine they provide should work, i mean in the documentation they state, "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." so i mean i think it should work just supplying the password.
        okay all, i must appologize... but i hope you find this alittle humerous:

        turns out the system admin mistyped the password my script was using... silly error.

        sorry again
        Are there any oddball chars you may need to escape?

        ~~
        naChoZ

        well i tried even to hardcode the password in, and still no go...
        $session->login(Password => 'password');
        yea... it prints fine to the screen
        well, i suppose i could give it a shot, but the file it is being read from has not spaces, "ipaddress,password"

        and i chomp that when i read it in to get rid of the newline, also, i tried the password manually, $session->login(Password => 'password'); so i dont know how there could be a space...
Re: Telnet into Cisco switches
by blue_cowdawg (Monsignor) on Jul 11, 2003 at 14:58 UTC

    As a matter of fact I do have experience with Net::Telnet::Cisco in particular and telnet'ing into routers and switches.

    A while back I wrote a rather convoluted and re-entrant script to update our MAC address pools in QIP.

    Just as an aside: QIP as an IP address management product sucks. It just sucks less than its competition.

    The way my script worked was when run on a machine it first looked at the routing table on that machine and figured out what the default router was. The default route could actually be a switch.

    Then we dumped the arp cache for that device and stored it and then dumped the devices routing table to find its neighbors. Then for each neighbor we recursed until we ran out of unique neighbors and had the whole list of ARP addresses for the enterprise.

    Doing this not only allowed us to update our global MAC pools, but it help us discover routers and switches that were not supposed to be there. After all this is a school campus and college students will play!

    Net::Telnet::Ciso worked very will for this. What I did discover though was I had to make my login algorithm somewhat intelligent because different devices posed different behavior upon login so my script had to be sensitive to that.


    Peter L. BergholdBrewer of Belgian Ales
    Peter@Berghold.Netwww.berghold.net
    Unix Professional
      i think i am just going to hard code it into the script, see what happens...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://273369]
Approved by Tanalis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-24 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found