Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

NET:SSH2 exec syntax usage

by ZigZaggin (Initiate)
on Apr 15, 2011 at 22:07 UTC ( #899692=perlquestion: print w/replies, xml ) Need Help??
ZigZaggin has asked for the wisdom of the Perl Monks concerning the following question:

First off - I would say I am intermediate PERL programmer. I can follow instruction, examples, etc. I wrote a program to ssh2 into a server and return some data. I've posted code that works vs code that doesn't work. Here's the problem... If i put something like 'ls -la /directory/cirectory2' within the parens on the exec line it works ok. However my zcat command with the grep statement doesn't work :/ The $ibmslapd_fn variable is poulated and is a string format. I've also tried it with & without the dbl quotes and would really like to know what I'm doing wrong so I can get the zcat cmd to work too! Thanks in advance for your time and knowledge.

my $chan = $ssh2->channel(); $chan->blocking(0); $chan->exec('ls -la /db2home/idsldap/idsslapd-idsldap/logs/'); while (<$chan>){ print FILE } print FILE "\n\n\n\n"; $ssh2->disconnect(); ### DISCONNECT ###
my $chan = $ssh2->channel(); $chan->blocking(0); $chan->exec('zcat /db2home/idsldap/idsslapd-idsldap/logs/ibmslapd.log. +$ibmslapd_fn-2350.gz | grep -i "entries have been successfully" '); while (<$chan>){ print FILE } print FILE "\n\n\n\n"; $ssh2->disconnect(); ### DISCONNECT ###

Replies are listed 'Best First'.
Re: NET:SSH2 exec syntax usage
by Illuminatus (Curate) on Apr 15, 2011 at 22:20 UTC
    1. Your zcat command is encased in single-quotes, which means that variable substitution will not occur.
    2. It is not totally clear from the documentation, but I am pretty sure that setting blocking to 0 means non-blocking. Since the ls probably returns right away, this makes no difference in the first case. zcat, however, can take a while on files of non-trivial size. Since you are not checking for EAGAIN, this could give you a problem


      Illuminatus is correct. If you want to use single quotes, you will have to concatenate your variables. Otherwise you can switch them to double quotes and backslash your literal double-quotes.

      ex. $chan->exec('zcat /db2home/idsldap/idsslapd-idsldap/logs/ibmslapd.log.' . $ibmslapd_fn . '-2350.gz | grep -i "entries have been successfully" ');


      ex. $chan->exec("zcat /db2home/idsldap/idsslapd-idsldap/logs/ibmslapd.log.$ibmslapd_fn-2350.gz | grep -i \"entries have been successfully\" ");

        Thank You SitrucHtims.

        This Worked
        ex. $chan->exec('zcat /db2home/idsldap/idsslapd-idsldap/logs/ibmslapd.log.' . $ibmslapd_fn . '-2350.gz | grep -i "entries have been successfully" ');

        This Didn't
        escaping the double quotes didn't work...
        $chan->exec("zcat /db2home/idsldap/idsslapd-idsldap/logs/ibmslapd.log.$ibmslapd_fn-2350.gz | grep -i \"entries have been successfully\" ");

        Thanks again for your assistance. My Issue is solved.

        The $chan->blocking(0); statement didn't seem to matter.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://899692]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2017-12-12 06:56 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (326 votes). Check out past polls.