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

=head2 show_run(); $output = $host->show_run(); $host->show_run($output_ref); sub show_run { my($self,$output_ref) = @_; my $data = $self->cmd('show run', undef, $output_ref, qr/^(?![:!])/) +; return $data; }

Above code takes the output from command 'show run', strips the lines starting with special chars and puts it in $data.

Is there a way to run another command and append that output to $data or show_run in $output also? Thanks

Replies are listed 'Best First'.
Re: Get outputs from 2 commands and add to var
by trippledubs (Deacon) on Nov 16, 2019 at 19:53 UTC

    The binary operator dot takes two arguments and concatenates the data from the right operand to the left operand. Combine with an equal sign to concatenate and assign with one operator.

    my $data = $self->cmd('show run', undef, $output_ref, qr/^(?![:!])/); $data .= 'appended data'; # Equivalent to $data = $data . 'appended data';

    Additive Operators

    my $data = $self->cmd('show run', undef, $output_ref, qr/^(?![:!])/); $data .= $self->cmd('some other command', undef, $output_ref, qr/^(?![ +:!])/);

    You also have to think about context. You probably don't want to append a list to a scalar. If $self->cmd returns a list, that's what you would be doing.

    use strict; use warnings; sub something { return (3,4,5) }; my $x = 'some data'; #scalar $x .= something; #list print $x; # some data5 print "\n"; print something; # 345
Re: Get outputs from 2 commands and add to var
by AnomalousMonk (Archbishop) on Nov 16, 2019 at 20:12 UTC

    ... and if you do want to append a list to a scalar, join may be useful:

    c:\@Work\Perl\monks>perl -wMstrict -le "sub something { return (3,4,5) }; ;; my $x = 'some data'; ;; $x .= join '--', something; print qq{'$x'}; " 'some data3--4--5'


    Give a man a fish:  <%-{-{-{-<

Re: Get outputs from 2 commands and add to var
by Cisco_Dave (Acolyte) on Nov 17, 2019 at 14:56 UTC

    Thanks for the responses guys. Yes just adding this did the trick I can see the output appended to the bottom..

    $data .= $self->cmd('some other command', undef, $output_ref);

    Having one problem though. If the command is not in format 'show this' I get an error. Thought it was the pipes at first but same issue when creating an alias on the remote device to run the command for me.

    Essentially the command is run, grepping string and then displays the last 3 lines of the output.

    $data .= $self->cmd('command | grep string | last 3', undef, $output_r +ef);

    Here it is again after setting alias name of 'runthis' against 'command | grep string | last 3' on the remote device (works ok locally)

    $data .= $self->cmd('runthis', undef, $output_ref);

    Error is Use of uninitialized value in concatenation (.) or string... Insecure dependency in sprintf while running with -T switch at ....

    Presumably the sub(s) above this command needs amending...

    sub smart_cmd { my($self,$cmd,@args) = @_; return $self->show_mem if ($cmd =~ /^\s*show mem/); return $self->show_cfg if ($cmd =~ /^\s*show (conf|run)/); return $self->show_start if ($cmd =~ /^\s*show start/); return $self->set_pass($1,$2) if ($cmd =~ /^\s*set pass\S*\s+(\S+)\s ++(\S+)/); return $self->set_enablepass($1,$2) if ($cmd =~ /^\s*set enablepass\ +S*\s+(\S+)\s+(\S+)/); my($data,$match,$remainder) = $self->cmd($cmd,@args); return (wantarray)?($data,$match,$remainder):$data; } =head2 cmd(); $data = $host->cmd($cmd); ($data,$matched_prompt) = $host->cmd($cmd,$prompt);

    Thanks again :)

Re: Get outputs from 2 commands and add to var
by Anonymous Monk on Nov 16, 2019 at 22:28 UTC

    Hi, thats pod not code ;)