Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
PerlMonks (whoopdee doo) ;P~

by lindex (Friar)
on Jul 19, 2000 at 13:27 UTC ( #23152=CUFP: print w/replies, xml ) Need Help??

This is a lil perl module I wrote to talk to cisco's.
Some of you guys out there could prolly write better (or just tell me how horrible my code is)
But Iam bored and felt like posting my code.

the pm ...
package cisco; use Net::Telnet; use Carp; sub new { my($class) = shift; my($self) = {}; my(%arg) = @_; my($ok); $self->{Timeout} = $arg{Timeout} || 10; $self->{Prompt} = $arg{Prompt} || undef(); $self->{Host} = $arg{Host} || undef(); $self->{Username} = $arg{Username} || undef(); $self->{Password} = $arg{Password} || undef(); if (!$arg{Prompt} && !$arg{Host}) { $self->{Error} = "Bad args (you did read the documentation didnt y +ou?)"; return(0); } bless($self,$class); return($self); } sub connect { my($self) = shift;; my($net,$ok) = undef(); my($prompt) = "/$self->{Prompt}/"; $net = new Net::Telnet(Timeout=>$self->{Timeout}, Prompt=>'/$self->{Prompt}/'); $ok = $net->open($self->{Host}); if (!$ok) { $self->{Error} = "Problem with opening a network connection !"; return(0); } else { $self->{Net} = $net; } if ($self->{Username}) { $net->login(Name=>$self->{Username}, Password=>$self->{Password}, Prompt=>$prompt); } return($net); } sub send { my($self) = shift; my($command) = shift; my(@data); my($prompt) = $self->{Prompt}; my($net) = $self->{Net}; $net->print($command); while(<$net>) { if ($_ =~ /$prompt/) { last; } push(@data,$_); $net->print(" "); } return(@data); } sub errorstr { my($self) = shift; return($self->{Error}); } 1;
and the example script ...
#!/usr/bin/perl use cisco; $cis = new cisco(Host=>"", # router hostname Username=>"bob", # username (bob is good ) Password=>"bleh", # hehe Prompt=>"router>", # this is a common prompt Timeout=>10); # prolly wont need to touch this $r = $cis->connect; if (!$r) { print $cis->errorstr(),"\n"; exit(-1); } foreach($cis->send("show bridge")) { print $_; }

Replies are listed 'Best First'.
RE: ( at CPAN also)
by ybiC (Prior) on Jul 19, 2000 at 16:51 UTC
    Stuff like this always interests me. I'm not terribly grokful of OOP, so can't comment on how horrible (or good) your code is.

    This just appeared at CPAN last week. So you may have been re-inventing the wheel. An interesting exercise, nonetheless.

      I'm the author of Net::Telnet::Cisco. You can always find the most recent copy at: At the time of this writing, Aug 1, Net::Telnet::Cisco is up to v1.03. Here are some of the nice things about it: 1. The default prompt works for a variety of hosts. I spent the time figuring it out so you don't have to. 2. The error-handling that Net::Telnet provides allows you to call a sub when an error occurs. Net::Telnet::Cisco handles all router-generated errors that match /^%/, which is almost all of them. Still curious? Take a look at the Net::Telnet::Cisco README in to get an idea of what you can accomplish with the module. Future plans: I'd like to include an EXAMPLES section which discusses common tasks (e.g. backing up the running-confg aka performing a write net aka performing a copy) Yours, Joshua
        What needs to be changed in you code to replace telnet with regular console or modem connection? Marek Ptak
        I'm still having problems with Net::Telnet::Cisco when I try to enable. Seems our routers use an extended login for enable which this module doesn't account for. The router doesn't just simply ask for the password but expects the username first. Has anyone gotten around this? Kevin
        Aright, I guess its time I do like Net::Telnet::LIVPM3 for livington portmaster 3's :)

        /****************************/, /*****************************/
      Ah poopy, Thats been sitting in my scripts dir for 3 months now
      oh well :)
      /****************************/, /*****************************/
        Any Network Programming Monks have a few spare cycles to compare & contrast the two modules?

        It would be very illuminating for me, and lindex kinda asked for feedback on his code.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://23152]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2020-10-23 06:46 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (235 votes). Check out past polls.