This is an untested snippet (I don't have a Cisco switch to telnet into), though it does compile. I've eliminated two of the three foreach loops by processing each port all the way through to an nslookup on each iteration of the single foreach loop. This eliminates the need for several of your single-use arrays.

use warnings; use strict; use Net::Telnet::Cisco; my $session = Net::Telnet::Cisco->new(Host => 'xxx.xx.xx.xx'); $session->login('','password'); # no paging - disrupts output $session->cmd('terminal length 0'); my @output1 = $session->cmd('show int status | include connected'); foreach my $out (@output1) { my $port = ( split / /, $out )[0]; next unless defined $port; my $mac = $session->cmd( "sh mac-address-table int $port" ); if ( $mac =~ m/(\.\w{4}\.\w{4})/ ) { my $ip = $session->cmd( "sh ip arp | include $1" ); if( $ip =~ m/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ ) { print `nslookup $1`; } } } $session->close;

Any more terse than this would make it difficult to read and maintain, IMHO. I'd like to note that your original script was not really any less efficient except that it required storage of mac addresses, ports, and IP's. But that's minor. Doing things in stages as you originally coded isn't inherently slower. The three foreach loops perform the same tasks as my single foreach loop, but my single loop takes just as long. It just rearranges the order in a way that makes the script itself shorter.

Updated to clean up the code further.

One more thing (update): I did notice that when I compiled this with 'perl -c mytest.pl', I got the following warning: Use of /g modifier is meaningless in split at C:/Perl/site/lib/Net/Telnet/Cisco.pm line 756., which indicates an issue buried somewhere within Net::Telnet::Cisco. Probably insignificant, but one always wonders when a module spits out a warning.


Dave


In reply to Re: More efficient and flexible by davido
in thread More efficient and flexible by raveguy2k

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.