Ok, so this maybe someone else would be able to help me out here.
been working with getting information off of Cisco routers, and for this, I've been using Net::Telnet and Net::Telnet::Cisco. They were working great until yesterday when I was asked to interface with route servers that do not prompt for login. These servers don't fall under my companies control, and thus I cannot push for a standard, so I'm forced to deal with what I have.
The end result, will be a web page, with some checkboxes for each route server, and a text box to enter commands you want to run on the router comma delimited, you hit submit, it returns a listing or routers with each commands output listed below. I have hte web interface done, now I'm just stuck on the most important part, getting the correct data, here's what's happening.. well.. using Net::Telnet and Net::Telnet::Cisco I've found that the out generated by simply creating the object and issuing a command is N/A, that I have to run a command twice to "open the conenction" for instance:
my $router = new Net::Telnet::Cisco(Host => $ip); my @output = $router->cmd("sh users"); print @output;
yields nothing to the screen, I figured out why though.. the terminal length setting was 25, the cisco default so I:
$router->cmd("terminal length 0");
still nothing.. the script timed out waiting for the prompt to return. so I just out on a limb:
$router->cmd("terminal length 0"); $router->cmd("terminal length 0"); my @output = $router->cmd("sh users"); print @output;
now I have something, so I thought, "Great, lets put this into production". so I did.. the results, are incredibly inconsistant, obviously because this was a bad work around. so I think "hey, I've worked with sockets before, I'll just write my own interface to this tricky situation!" so I did.. but I've been toying with the issuing multiple commands to routers using send() and recv() and autoflushing the socket, no luck and I've tried it many different ways, one of which I though should work:
foreach my $server (@SERVERS) { my ($sname,$sdom); if($server =~ /^([^.]+)(.+)/) { $sname = $1; $sdom = $2; $sdom =~ s/\./\\\./g; } print "\n\n---> Trying $server .... "; socket(ROUTER,AF_INET,SOCK_STREAM,getprotobyname('tcp')); my $sin = sockaddr_in(23,inet_aton($server)); connect(ROUTER,$sin); # or die "$server needs a kick!\n"; print "Connected.\n"; bind(ROUTER,$sin); # below insight provided by Net::Telnet.pm select((select(ROUTER), $|=1)[$[]); #don't buffer writes # end insight. send(ROUTER, "\n", ''); #doing to get a prompt +, establish connection my $task= scalar @CMDS; my $line=""; my $get_output=0; my $i=0; my %OUT=(); while(recv(ROUTER,$line,$LENGTH,'')) { local $_ = lc($line); if(/^$sname($sdom)?\>/) { #we have a prompt if(!$task) { #we're done! send(ROUTER, $close, ''); last; } else { send(ROUTER, $CMDS[$i], ''); $i++; $task--; $get_output=1; next; } } elsif($get_output) { #we're between two pro +mpts and interested in the output push @{ $OUT{$CMDS[$i-1]} }, $line; print $line; } } close ROUTER; }
what happens here is even though the prompt matched previously, it isn't matching again, and is getting printed at the "elsif($get_output)", thus never issuing the next command and only getting to the first server cuz it halts the recv() ..... I can't see how this could happen, but perhaps others could slap me in the right direction.

Any help would be greatly appreciated,

-brad..

In reply to Interesting Dilemma by reyjrar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.