Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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 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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2020-10-21 16:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (222 votes). Check out past polls.

    Notices?