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

collect, set, and analyze command outputs

by dpasch (Initiate)
on Oct 04, 2019 at 16:06 UTC ( [id://11107054]=perlquestion: print w/replies, xml ) Need Help??

dpasch has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I am new, and do not have a coding/scripting background.
I am looking to turn some commands I run at the command line of a device into a PERL script, and am seeking advice.

The ask:
1. Run a command to collect data.
EXAMPLE:
"list cm device all-properties | grep configsync-ip"

2. Collect ONLY the IPs from the output of the above command, and set them variables.
EXAMPLE OUTPUT:
    configsync-ip 1.1.1.1
    configsync-ip 1.1.1.2

$csip1 = 1.1.1.1
$csip2 = 1.1.1.2

3. Run another command(s) using the above variables.
EXAMPLE:
tmsh list net self | grep ($csip1) | -A 1
tmsh list net self | grep ($csip2) | -A 1

4. Based on the text containing "all" in the output, form a response to print.
EXAMPLE OUTPUT:
address 1.1.1.1/24
    allow-service all <-- This woud trigger a print, with multiple variables collected outside of the ask.

I do not have much code to offer as my starting point. Just the template that the tool I am working in provides.

Thanks in advance for all responses!

#============= your logic goes here ================== # execute commands # collect cs_vlan ip info my $response = $connect->cmd('tmsh list cm device all-properties | gre +p configsync-ip',{-prompt => '#'}); # set ips from output to variables # use variables in additional commands # analyze the output and create resposes for print my $target; if ($response =~ /(\.....d+\.\d+\.\d+\.\d+….)/) { $target = $1; } #================================================= my $2ndresponse =….. …. $connect->cmd('exit'); $connect->disconnect; # PRINT WHATEVER YOU WANT TO SHOW TO POLICIES print "\n$FINALresponse\n";

Replies are listed 'Best First'.
Re: collect, set, and analyze command outputs
by rjt (Curate) on Oct 04, 2019 at 19:17 UTC

    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.
Re: collect, set, and analyze command outputs
by jcb (Parson) on Oct 04, 2019 at 22:32 UTC

    Your problem (interacting with some kind of remote shell prompt) seems to be almost exactly what Expect was designed to solve, and there is the Expect module on CPAN that provides that API in Perl.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11107054]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2024-04-23 14:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found