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

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.

THIS WORKS FINE
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 ###
THIS DOESN'T WORK
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 ###


Comment on NET:SSH2 exec syntax usage
Select or Download Code
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

    fnord

      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" ');

      or

      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-11-27 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (178 votes), past polls