raveguy2k has asked for the wisdom of the Perl Monks concerning the following question:
My goal for this particular script is to go into a switch and find ports that are connected and then find ip's based on mac addresses and do an nslookup to find out what servers the ports are connected to. However my ultimate goal is to create a script that will allow one to go into a switch or a router and pull whatever information they want easily and quickly. This is where you helpful folks come in. How can I reduce the repitition in my code to make it more efficient by removing some of the for loops by taking advantage of some of perls idioms and are there any other ways of making the script more flexible so that whatever information I want from a router or script can easily be obtained? Here is some sample code:
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'); my @connected; my @port; my @macs; # get only connected ports foreach (@output1) { # should be splitting on those that are connected my @tmp = split(/ /, $_); # just get a stream of interfaces? push @port, $tmp[0]; # create new command my $string; $string = "sh mac-address-table int $tmp[0]" if defined $tmp[0]; # show what we get push @macs, $session->cmd($string); } # go through sh mac-address-table for connected interfaces # get just mac address my $cmd; my @ip; foreach (@macs) { if(m/(\.\w{4}\.\w{4})/) { $cmd = "sh ip arp | include $1"; push @ip, $session->cmd($cmd); } } #extract ip and perform nslookup foreach (@ip) { if(m/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/) { print `nslookup $1`; } } $session->close;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: More efficient and flexible
by davido (Cardinal) on Dec 14, 2005 at 20:24 UTC |