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

Simplification of the "exec" sample

This commit is contained in:
Aris Adamantiadis
2009-12-24 10:46:46 +01:00
parent fcda8beb1c
commit bfdc48e320

View File

@ -7,7 +7,7 @@
int main(void) { int main(void) {
ssh_session session; ssh_session session;
ssh_channel channel; ssh_channel channel;
ssh_buffer buf; char buffer[256];
int rc; int rc;
session = connect_ssh("localhost", NULL, 0); session = connect_ssh("localhost", NULL, 0);
@ -18,7 +18,6 @@ int main(void) {
channel = channel_new(session);; channel = channel_new(session);;
if (channel == NULL) { if (channel == NULL) {
ssh_disconnect(session); ssh_disconnect(session);
ssh_finalize();
return 1; return 1;
} }
@ -26,7 +25,6 @@ int main(void) {
if (rc < 0) { if (rc < 0) {
channel_close(channel); channel_close(channel);
ssh_disconnect(session); ssh_disconnect(session);
ssh_finalize();
return 1; return 1;
} }
@ -34,34 +32,24 @@ int main(void) {
if (rc < 0) { if (rc < 0) {
channel_close(channel); channel_close(channel);
ssh_disconnect(session); ssh_disconnect(session);
ssh_finalize();
return 1; return 1;
} }
if (channel_is_open(channel)) { while ((rc = channel_read(channel, buffer, sizeof(buffer), 0)) >= 0) {
while (channel_poll(channel, 0) >= 0) { fwrite(buffer, 1, rc, stdout);
buf = buffer_new(); }
rc = channel_read_buffer(channel, buf, 0, 0);
if (rc < 0) { if (rc < 0) {
buffer_free(buf);
channel_close(channel); channel_close(channel);
ssh_disconnect(session); ssh_disconnect(session);
ssh_finalize();
return 1; return 1;
} }
printf("%s\n", (char *) buffer_get(buf));
buffer_free(buf);
}
}
channel_send_eof(channel); channel_send_eof(channel);
channel_close(channel); channel_close(channel);
ssh_disconnect(session); ssh_disconnect(session);
ssh_finalize();
return 0; return 0;
} }