1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-07-28 01:41:48 +03:00

Added an example for exec.

This commit is contained in:
Andreas Schneider
2009-12-22 18:33:16 +01:00
parent a6383cec51
commit d0647afae5
2 changed files with 69 additions and 0 deletions

67
examples/exec.c Normal file
View File

@ -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;
}