http://www.perlmonks.org?node_id=23152

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=>"somerouter.domain.com", # 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: cisco.pm (cisco.pm 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 http://search.cpan.org/doc/JOSHUA/Net-Telnet-Cisco-1.01/Cisco.pm just appeared at CPAN last week. So you may have been re-inventing the wheel. An interesting exercise, nonetheless.
        cheers,
        ybiC

      I'm the author of Net::Telnet::Cisco. You can always find the most recent copy at: ftp://ftp.cpan.org/CPAN/authors/id/J/JO/JOSHUA/ 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 http://search.cpan.org/ 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 mptak@cme.com
        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 :)



        lindex
        /****************************/ jason@gost.net, wh@ckz.org http://jason.gost.net /*****************************/
      Ah poopy, Thats been sitting in my scripts dir for 3 months now
      oh well :)
      /****************************/ jason@gost.net, wh@ckz.org http://jason.gost.net /*****************************/
        Any Network Programming Monks have a few spare cycles to compare & contrast the two cisco.pm modules?

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