Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Welcome to Perl programming, and coding in general. Hoooo boy, you're in for a -1*$heaven of a ride.

I don't want to take all the fun out of it for you, so I'll give you a few hints, snippets, and documentation pages to check out.

Running commands and getting output: see Quote Like Operators, in particular, qx/$cmd/ or `$cmd`

Also note you can use Perl to replace the grep part of your command:

my @lines = `list cm device all-properties`; my @matches = grep { /configsync-ip/ } @lines; chomp @matches; # Remove trailing newlines

Which can be written more concisely:

my @matches = grep { /configsync-ip/ } `list cm device all-properties` +; chomp @matches;

Now you'll want to use split to split the lines on space characters to get your IP addresses.

for (@matches) { my (undef, $ip) = split /\s+/; ... # Your code to deal with the IP here. }

But this is Perl. A common idiom is to chain together all of these things, with some help from map:

my @ips = map { (split /\s+/)[1] } grep { /configsync-ip/ } `list cm device all-properties`;

Note chomp wasn't necessary here, because newlines are also matched by \s, so they will effectively be removed.

That takes care of #1 and #2. For #3, assuming `tmsh list net self` outputs the same thing every time, you can avoid having to run it for every IP by just running it once, and checking each IP against your list. This will be significantly faster.

In this case, you can still use `tmsh list net self` to get the lines of the command, but since you need to get two lines at a time, it is a little bit trickier. There are many ways to do it, but I'll take this opportunity to introduce you to 3rd party modules: CPAN! And in particular, List::MoreUtils and its natatime() sub:

use List::MoreUtils qw/:all/; my $it = natatime 2, `tmsh list net self`; while (my ($addr, $allow_service) = $it->()) { # $addr is the first line, e.g.: address # $allow_service is the 2nd line: allow-service all }

Now you can use the tools you've learned above to split $addr, and strip off the CIDR netmask, and determine if the $allow_service line meets your criteria. How are you going to figure out if $addr is in your list of @ips though? Two ways:

The inefficient and boring way is to loop through @ips and set a flag if you get a match. The way cooler and faster way is to use hashes. First, put all of your IPs into a hash:

my %ips = map { $_ => 1 } @ips;

Now $ips{''} is true if and only if is an IP you saw in step 2. I'm sure you can figure out how to take advantage of that. :-)

There's a fair bit of information here. Hopefully enough to get you off to a good start.

use strict; use warnings; omitted for brevity.

In reply to Re: collect, set, and analyze command outputs by rjt
in thread collect, set, and analyze command outputs by dpasch

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (4)
    As of 2020-06-05 00:35 GMT
    Find Nodes?
      Voting Booth?
      Do you really want to know if there is extraterrestrial life?

      Results (35 votes). Check out past polls.