diff options
author | Andreas Schneider <mail@cynapses.org> | 2009-12-22 18:33:16 +0100 |
---|---|---|
committer | Andreas Schneider <mail@cynapses.org> | 2009-12-22 18:34:23 +0100 |
commit | d0647afae50294ec09b3ce7c968c7c6c42f4190d (patch) | |
tree | 43c036d9d31c081e4ca1e079a0e8740e5050b54f /examples/exec.c | |
parent | a6383cec51e06aac55c80c5e5111856ca5b279a3 (diff) | |
download | libssh-d0647afae50294ec09b3ce7c968c7c6c42f4190d.tar.gz libssh-d0647afae50294ec09b3ce7c968c7c6c42f4190d.tar.xz libssh-d0647afae50294ec09b3ce7c968c7c6c42f4190d.zip |
Added an example for exec.
Diffstat (limited to 'examples/exec.c')
-rw-r--r-- | examples/exec.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/examples/exec.c b/examples/exec.c new file mode 100644 index 0000000..077b94f --- /dev/null +++ b/examples/exec.c @@ -0,0 +1,67 @@ +/* simple exec example */ +#include <stdio.h> + +#include <libssh/libssh.h> +#include "examples_common.h" + +int main(void) { + ssh_session session; + ssh_channel channel; + ssh_buffer buf; + int rc; + + session = connect_ssh("localhost", NULL, 0); + if (session == NULL) { + return 1; + } + + channel = channel_new(session);; + if (channel == NULL) { + ssh_disconnect(session); + ssh_finalize(); + return 1; + } + + rc = channel_open_session(channel); + if (rc < 0) { + channel_close(channel); + ssh_disconnect(session); + ssh_finalize(); + return 1; + } + + rc = channel_request_exec(channel, "ps aux"); + if (rc < 0) { + channel_close(channel); + ssh_disconnect(session); + ssh_finalize(); + return 1; + } + + + if (channel_is_open(channel)) { + while (channel_poll(channel, 0) >= 0) { + buf = buffer_new(); + rc = channel_read_buffer(channel, buf, 0, 0); + if (rc < 0) { + buffer_free(buf); + channel_close(channel); + ssh_disconnect(session); + ssh_finalize(); + return 1; + } + + printf("%s\n", (char *) buffer_get(buf)); + + buffer_free(buf); + } + } + + channel_send_eof(channel); + channel_close(channel); + + ssh_disconnect(session); + ssh_finalize(); + + return 0; +} |