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

Hi Monks,
I need to know if there is some way to read data into buffer and mentioning the bytes to read dynamically ?
I am currently executing something like this

$chan->shell();
$chan->write("egrep -l $searchStr * 2>&1 | tee ~/myfile.txt\n");
the above can have multiple lines as output or jus a single line or nothing at all. After this I have the read call.
$chan->read($buf, 512);
here if the ouput of the egrep does not have 512 bytes, the read call waits until it reads 512.
If I make the channel as non-blocking, then the read call does not complete and I do not get the desired result stored in the file - myfile.txt
please let me know if there is a work-around for this or I am missing something ?
J

Replies are listed 'Best First'.
Re: Read call in Net::SSH2::Channel
by keszler (Priest) on Jan 19, 2010 at 14:01 UTC
    I prefer this:
    use strict; use warnings; use Net::SSH2; my $ssh2 = Net::SSH2->new(); $ssh2->connect('server'); $ssh2->auth_password('username','passwd'); my $shell = $ssh2->channel(); $shell->blocking(0); $shell->shell(); print {$shell} "egrep -l $searchStr * 2>&1 | tee ~/myfile.txt\n"; # <$shell> works, but <$hash{$shell_box1}> does not, so my @results = readline($shell);
    Error-trapping left as an exercise for the reader
Re: Read call in Net::SSH2::Channel
by salva (Canon) on Jan 19, 2010 at 14:25 UTC
    Under Unix/Linux, you can try using Net::OpenSSH that's more perlish:
    use Net::OpenSSH; my $ssh = Net::OpenSSH->new(...); my $out_fh = $ssh->pipe_out("egrep -l $searchStr * 2>&1 | tee ~/myfile +.txt\n");
    Then, the returned $out_fh object will be a real and fully working file handle that can be used, for instance, inside a non-blocking select loop.