Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: collect, set, and analyze command outputs

by rjt (Curate)
on Oct 04, 2019 at 19:17 UTC ( #11107065=note: print w/replies, xml ) Need Help??

in reply to collect, set, and analyze command outputs

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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11107065]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2023-03-20 18:35 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (59 votes). Check out past polls.