in reply to More efficient and flexible

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