Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://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 1.1.1.1/24 # $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{'1.1.1.1'} is true if and only if 1.1.1.1 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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-16 11:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found