Search code examples
bashperlperl-module

Net::OpenSSH command remote with multi pipeline


i have a problem when attempt run multi command in remote linux, with Perl and Module Net::OpenSSH. .

use Net::OpenSSH;
my $new_connect = Net::OpenSSH->new($D_CONNECT{'HOST'}, %OPT); 
my $file = "file.log.gz" 
my ($output2, $error2) = $new_connect->capture({ timeout => 10 }, "gunzip -c /path/to/file/$file | tail -n1 | awk '/successfully completed/ {print \$NF}'");

the output that i get is:

bash: -c: line 1: syntax error near unexpected token |' bash: -c: line 1: |tail -n1 |awk '/successfully completed/ {print $NF}'' ;;;

any idea or suggestion, thanks.

Fcs


Solution

  • Probably a quoting error. Just let Net::OpenSSH take care of the quoting for you:

    my ($output2, $error2) = $new_connect->capture({ timeout => 10 },
                                                   'gunzip', '-c', "/path/to/file/$file", \\'|',
                                                   'tail', '-n1', \\'|',
                                                   'awk', '/successfully completed/ {print $NF}');
    

    Note how the pipes (|) are passed as a double reference so that they are passed unquoted to the remote shell. The module documentation has a section on quoting.