|
Packit Service |
31306d |
/* simple exec example */
|
|
Packit Service |
31306d |
#include <stdio.h>
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
#include <libssh/libssh.h>
|
|
Packit Service |
31306d |
#include "examples_common.h"
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
int main(void) {
|
|
Packit Service |
31306d |
ssh_session session;
|
|
Packit Service |
31306d |
ssh_channel channel;
|
|
Packit Service |
31306d |
char buffer[256];
|
|
Packit Service |
31306d |
int nbytes;
|
|
Packit Service |
31306d |
int rc;
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
session = connect_ssh("localhost", NULL, 0);
|
|
Packit Service |
31306d |
if (session == NULL) {
|
|
Packit Service |
31306d |
ssh_finalize();
|
|
Packit Service |
31306d |
return 1;
|
|
Packit Service |
31306d |
}
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
channel = ssh_channel_new(session);;
|
|
Packit Service |
31306d |
if (channel == NULL) {
|
|
Packit Service |
31306d |
ssh_disconnect(session);
|
|
Packit Service |
31306d |
ssh_free(session);
|
|
Packit Service |
31306d |
ssh_finalize();
|
|
Packit Service |
31306d |
return 1;
|
|
Packit Service |
31306d |
}
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
rc = ssh_channel_open_session(channel);
|
|
Packit Service |
31306d |
if (rc < 0) {
|
|
Packit Service |
31306d |
goto failed;
|
|
Packit Service |
31306d |
}
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
rc = ssh_channel_request_exec(channel, "lsof");
|
|
Packit Service |
31306d |
if (rc < 0) {
|
|
Packit Service |
31306d |
goto failed;
|
|
Packit Service |
31306d |
}
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
|
|
Packit Service |
31306d |
while (nbytes > 0) {
|
|
Packit Service |
31306d |
if (fwrite(buffer, 1, nbytes, stdout) != (unsigned int) nbytes) {
|
|
Packit Service |
31306d |
goto failed;
|
|
Packit Service |
31306d |
}
|
|
Packit Service |
31306d |
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
|
|
Packit Service |
31306d |
}
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
if (nbytes < 0) {
|
|
Packit Service |
31306d |
goto failed;
|
|
Packit Service |
31306d |
}
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
ssh_channel_send_eof(channel);
|
|
Packit Service |
31306d |
ssh_channel_close(channel);
|
|
Packit Service |
31306d |
ssh_channel_free(channel);
|
|
Packit Service |
31306d |
ssh_disconnect(session);
|
|
Packit Service |
31306d |
ssh_free(session);
|
|
Packit Service |
31306d |
ssh_finalize();
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
return 0;
|
|
Packit Service |
31306d |
failed:
|
|
Packit Service |
31306d |
ssh_channel_close(channel);
|
|
Packit Service |
31306d |
ssh_channel_free(channel);
|
|
Packit Service |
31306d |
ssh_disconnect(session);
|
|
Packit Service |
31306d |
ssh_free(session);
|
|
Packit Service |
31306d |
ssh_finalize();
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
return 1;
|
|
Packit Service |
31306d |
}
|