Greetings Monks,

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;

In reply to 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.