www.libssh2.org | Daily snapshots | Mailing list archive | Docs | Examples

Archive Index This month's Index

Subject: not getting all the data with libssh2_channel_exec and libssh2_channel_read

not getting all the data with libssh2_channel_exec and libssh2_channel_read

From: Seth Grover <sethdgrover_at_gmail.com>
Date: Wed, 6 Jan 2010 11:11:12 -0700

Greetings,

First, let me say that I'm new to this list (and libssh2 in general),
though I have spent the last several days researching, experimenting,
and reading mailing list archives. I hope that from my reading of past
mailing list submissions and the googling I've done that I'm not
coming to you with an oft-asked question. If so, please excuse me.

As for background on my system, I'm running my example on Ubuntu 9.10
x86 with libssh2-1 version 1.1.1 package (in libssh2.h I have #define
LIBSSH2_TIMESTAMP "Thu Apr 2 08:49:40 UTC 2009" and libssh2_version
returns "1.1").

I have slightly modified ssh2_exec.c for the purpose of this example.
I have provided the code I'm working with on pastebin:

http://pastebin.com/f2d9765eb

The problem I'm encountering is this:

When I run my program like so:

./ssh2_exec 1.2.3.4 user password 'ls -l /usr/lib'

I sometimes don't get all of the data before the loop breaks. Note
that the command 'ls -l /usr/lib' in my case is returning about 38000
bytes. Sometimes I get all of the data, but sometimes I'll get output
like this:

-------------------------------------------
1.1
Authentication by password succeeded
libssh2_channel_read returned -37
libssh2_channel_read returned -37
libssh2_channel_read returned -37
libssh2_channel_read returned -37
libssh2_channel_read returned -37
libssh2_channel_read returned 0

EXIT: 0 bytecount: 0
all done
-------------------------------------------

Note that in that case i didn't get ANY data, just
LIBSSH2_ERROR_EAGAIN several times and then libssh2_channel_read
returns 0 bytes, so the loop is exited. Other times I might read some
of the data, but libssh2_channel_read returns 0 before I got all of
it.

I experimented with libssh2_channel_eof but it didn't seem to help me.
What I did was modify the code right before the end of the while loop
to read thus:

-------------------------------------------
    if (rc == LIBSSH2_ERROR_EAGAIN) {
      fprintf(stderr, "told to wait...\n");
      waitsocket (sock, session);
    } else if (libssh2_channel_eof(channel)) {
      fprintf(stderr, "got libssh2_channel_eof, breaking\n");
      break;
    } else {
      fprintf(stderr, "got 0 bytes but no eof yet, sleeping\n");
      sleep(3);
    }
-------------------------------------------

When it works, the end of the output looks like this:

-------------------------------------------
libssh2_channel_read returned 0
got libssh2_channel_eof, breaking

EXIT: 0 bytecount: 38252
all done
-------------------------------------------

or this

-------------------------------------------
libssh2_channel_read returned -37
told to wait...
libssh2_channel_read returned 0
got libssh2_channel_eof, breaking

EXIT: 0 bytecount: 38252
all done
-------------------------------------------

but somtimes it looks like this:
-------------------------------------------
libssh2_channel_read returned -37
told to wait...
libssh2_channel_read returned 0
got 0 bytes but no eof yet, sleeping
libssh2_channel_read returned 0
got 0 bytes but no eof yet, sleeping
-------------------------------------------

and just loops forever, never receiving the EOF.

I have similar issues when i use blocking mode as well (but I really
need to use nonblocking mode).

I'm basically just trying to find the "correct" way to consistently
get ALL of the output from an executed command. Can anyone offer
suggestions?

Thanks,

-SG

--
This email is fiction. Any resemblance to actual events
or persons living or dead is purely coincidental.
Seth Grover
sethdgrover[at]gmail[dot]com
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2010-01-06

the libssh2 team