Re: NET::SSH2 - No Output

2008-12-04 Thread listmail
SelfSimilar wrote:
> After successfully establishing a SSH connection I use the following code 
> snippet, variations of which have been posted many places.
>
> my $chan = $ssh2->channel();
> $chan->shell();
> $chan->blocking(0);
> my ($len, $buf);
> $chan->write("ls\n");
> select(undef,undef,undef,0.25);
> print $buf while (($len = $chan->read($buf,512)) > 0);
>
> I get an error message "Use of uninitialized value in numeric gt (>) at 
> SFTP_test2.pl line 21" which is the print statement
>
> print $buf while (($len = $chan->read($buf,512)) > 0);
>   
try something like

print $buf while (($len = $chan->read($buf,512))||0) > 0);

> If I turn on debugging ( $ssh->debug(1) ). I get the following output
>
> libssh2_channel_open_ex(ss->session, pv_channel_type, len_channel_type,
> window_size, packet_size, ((void *)0) , 0 ) -> 0x1ca3ecc
> Net::SSH2::Channel::read(size = 512, ext = 0)
> mail
> mbox
> Network Trash Folder
> Private
> user
> vsite
> web
> - read 54 bytes
> - read -37 bytes
> - read 54 total
> Net::SSH2::Channel::read(size = 512, ext = 0)
> - read -37 bytes
> Use of uninitialized value in numeric gt (>) at SFTP_test2.pl line 21.
> Net::SSH2::Channel::DESTROY
> Net::SSH2::DESTROY object 0x19a4c9c
>
> Lines 3-9 is the 'ls' output I am looking for so I know it is in the 
> channel. What is causing the error "Use of uninitialized value in numeric gt 
> (>)"  from the statement
>
> print $buf while (($len = $chan->read($buf,512)) > 0);
>
>
> Thanks
>  
>
> ___
> Perl-Win32-Users mailing list
> Perl-Win32-Users@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>   


___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: NET::SSH2 - No Output

2008-12-04 Thread SelfSimilar
> try something like
> 
> print $buf while (($len = $chan->read($buf,512))||0) > 0);

Thank you for the suggestion. Unfortunately, it get the same error warning

Use of uninitialized value in numeric gt (>) at SFTP_test2.pl line 21
 
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: NET::SSH2 - No Output

2008-12-04 Thread Sisyphus

- Original Message - 
From: "SelfSimilar" <[EMAIL PROTECTED]>
To: 
Sent: Friday, December 05, 2008 6:34 AM
Subject: NET::SSH2 - No Output


> After successfully establishing a SSH connection I use the following code
> snippet, variations of which have been posted many places.
>
>my $chan = $ssh2->channel();
>$chan->shell();
>$chan->blocking(0);
>my ($len, $buf);
>$chan->write("ls\n");
>select(undef,undef,undef,0.25);
>print $buf while (($len = $chan->read($buf,512)) > 0);

As an alternative, that could be replaced with:

my $chan = $ssh2->channel();
my ($len, $buf) = (1000, ' ' x 1000);
$chan->exec("ls\n");
$chan->read($buf, $len);
print $buf, "\n";

One problem with that replacement code is that you need to allocate a 
sufficiently large buffer ($buf). It doesn't matter if $buf is larger than 
necessary, but if it's smaller than needed then you miss out on the "extra" 
data.

>
> I get an error message "Use of uninitialized value in numeric gt (>) at
> SFTP_test2.pl line 21" which is the print statement

It's not an error message - it's a warning which you could eliminate with 
(at the beginning of your script):

no warnings 'uninitialized';

I get the same warning with your code, but I also get the "ls" output 
printed out before the warning appears. Is it the same for you ?

Are you using perl 5.8.x or 5.10.0 ?

Cheers,
Rob


___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: NET::SSH2 - No Output

2008-12-04 Thread Sisyphus

- Original Message - 
From: "SelfSimilar" <[EMAIL PROTECTED]>
.
.
>$chan->write("ls\n");

If the "ls" command throws an error, you won't get to see what the error is. 
(You'll simply get no output.) Better to write:

$chan->write("ls 2>&1\n");

That way, any error message will end up in the buffer, and you'll get to see 
it.
For the same reason, the same change should be made to the alternative code 
that I provided earlier.

Cheers,
Rob


___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: NET::SSH2 - No Output

2008-12-04 Thread listmail
Sisyphus wrote:
> - Original Message - 
> From: "SelfSimilar" <[EMAIL PROTECTED]>
> .
> .
>   
>>$chan->write("ls\n");
>> 
>
> If the "ls" command throws an error, you won't get to see what the error is. 
> (You'll simply get no output.) Better to write:
>
> $chan->write("ls 2>&1\n");
>
> That way, any error message will end up in the buffer, and you'll get to see 
> it.
> For the same reason, the same change should be made to the alternative code 
> that I provided earlier.
>
> Cheers,
> Rob
>
>
> ___
> Perl-Win32-Users mailing list
> Perl-Win32-Users@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>   
I didn't even notice that you are using write and not exec.  Why not use 
exec and ext_data('merge') for one command runs ?  During my testing of 
write I found that a while loop isn't sufficient in the case of running 
any sort of command that takes a bit of time while producing its 
output.  Hence why he added the select call.  You also get os headers 
and other outputs which might not be ideal if you are parsing.  Anyway, 
Just curious.
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: NET::SSH2 - No Output

2008-12-04 Thread Gaurav Vaidya
Hi SelfSimilar,

On Dec 5, 2008, at 3:34 AM, SelfSimilar wrote:
> I get an error message "Use of uninitialized value in numeric gt (>)  
> at
> SFTP_test2.pl line 21" which is the print statement
>
> print $buf while (($len = $chan->read($buf,512)) > 0);
That error (or is it a warning?) means that you tried to compare  
'undef' with a number using the '>' operator. In terms of your code, I  
guess it means that
$chan->read($buf,512)
returned 'undef'.

So what does *that* mean? According to the documentation for  
Net::SSH2::Channel->read:
Attempts to read size bytes into the buffer. Returns number of bytes  
read,
undef on failure. If ext is present and set, reads from the extended  
data
channel (stderr).
(from http://search.cpan.org/~dbrobins/Net-SSH2-0.18/lib/Net/SSH2/Channel.pm)

So maybe there was a failure reading data from the SSH channel, which  
made $chan->read return undef, causing the warning in your code.

You can test this theory by rewriting that line as:
while (1) {
var $len = $chan->read($buf, 512);
if($len == undef) {
print "$chan->read returned undef\n";
my ($error_code, $error_name, $error_string) = 
$ssh2->error;
print "Error reported by Perl: $!\n" if(defined($!));
print "Error reported by SSH: 
$error_code:$error_name:$error_string 
\n" if defined($error_code);
exit 0;
}
last if ($len < 0);
print STDERR "Length is zero" if($len == 0);

print $buf;
}

If your program exits with a "$chan->read returned undef" message,  
you'll know what's going on. With any luck, it'll also give you an  
error message of some sort, which you can look into that to figure out  
why $chan->read is returning 'undef'.

Hope that helps,
Gaurav
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: NET::SSH2 - No Output

2008-12-05 Thread Chris Wagner
At 02:04 PM 12/4/2008 -0600, SelfSimilar wrote:
>> try something like
>> 
>> print $buf while (($len = $chan->read($buf,512))||0) > 0);
>
>Thank you for the suggestion. Unfortunately, it get the same error warning
>
>Use of uninitialized value in numeric gt (>) at SFTP_test2.pl line 21

I have to chime in.  That kind of construct is just plain wrong. :)  U need
an if{} block to test for every possible return type of $chan->read().  i.e.
blank, undef, valid number, invalid number, and other junk.  U've fallen
into the one liner trap.  Laziness *is* the cardinal virtue of the
programmer, but u can still be burned by it.




--
REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=--
"...ne cede malis"

0100

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: NET::SSH2 - No Output

2008-12-05 Thread listmail
Chris Wagner wrote:
> At 02:04 PM 12/4/2008 -0600, SelfSimilar wrote:
>   
>>> try something like
>>>
>>> print $buf while (($len = $chan->read($buf,512))||0) > 0);
>>>   
>> Thank you for the suggestion. Unfortunately, it get the same error warning
>>
>> Use of uninitialized value in numeric gt (>) at SFTP_test2.pl line 21
>> 
>
> I have to chime in.  That kind of construct is just plain wrong. :)  U need
> an if{} block to test for every possible return type of $chan->read().  i.e.
> blank, undef, valid number, invalid number, and other junk.  U've fallen
> into the one liner trap.  Laziness *is* the cardinal virtue of the
> programmer, but u can still be burned by it.
>
>
>
>
> --
> REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=--
> "...ne cede malis"
>
> 0100
>
>   
Uh, I think you are just complaining about one of your pet peeves :) -- 
His example is simplified, if not *lazy*  as you put it, in its entirety 
much like the suggestion that I provided to him.  It also didn't work 
for him.  The docs or Channel.pm code do not seem to indicate the return 
types you have mention here beyond -- bytes read or undef -- so I'm not 
sure how you would code against all of those conditions unless you are 
suggesting one should code based on speculation alone.  I believe I 
would continue on the "lazy" route if that is the case. 

If he's really concerned about ensuring that he gets all of the output 
-- he needs to consider using $channel->exec otherwise even with adding 
delays (ie. using select calls) *inside* the while loop it still may not 
be long enough of a wait for read to be able to pull in all the output 
as it will probably return undef several times.  Unless, that is, he's 
ok with adding in several seconds of delays as needed to cover all types 
of commands or scripts he'd run.



___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: NET::SSH2 - No Output

2008-12-05 Thread Chris Wagner
At 04:10 PM 12/5/2008 -0500, listmail wrote:
>> I have to chime in.  That kind of construct is just plain wrong. :)  U need
>> an if{} block to test for every possible return type of $chan->read().  i.e.
>> blank, undef, valid number, invalid number, and other junk.  U've fallen
>Uh, I think you are just complaining about one of your pet peeves :) -- 

Yep. :)


>for him.  The docs or Channel.pm code do not seem to indicate the return 
>types you have mention here beyond -- bytes read or undef -- so I'm not 
>sure how you would code against all of those conditions unless you are 

Ur right it does depend on what the docs say, I was just trying to be
generic in describing a good practice.  If the module itself restricts what
the call can return then u don't have to check all that, just leaving an
else{} block is sufficient.  If undef or a number is the only legal return
value then the if{} block only needs to check for those specifically.


>If he's really concerned about ensuring that he gets all of the output 
>-- he needs to consider using $channel->exec otherwise even with adding 
>delays (ie. using select calls) *inside* the while loop it still may not 
>be long enough of a wait for read to be able to pull in all the output 
>as it will probably return undef several times.  Unless, that is, he's 
>ok with adding in several seconds of delays as needed to cover all types 
>of commands or scripts he'd run.

Ur right.  The problem is no terminator to the read.  *No* amount of loop
delay will guarantee that all the data has been read.  I ran into that
myself with some IPC I was working on.  I ended up creating a mini protocol
to solve the problem since I had control of both processes.






--
REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=--
"...ne cede malis"

0100

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs