You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
2.8 KiB
126 lines
2.8 KiB
2 years ago
|
#include <libssh/libssh.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
int verify_host(ssh_session session) {
|
||
|
enum ssh_known_hosts_e state;
|
||
|
|
||
|
state = ssh_session_is_known_server(session);
|
||
|
|
||
|
// only handling known hosts for now
|
||
|
// will implement other responses later.
|
||
|
switch (state) {
|
||
|
case SSH_KNOWN_HOSTS_OK:
|
||
|
break;
|
||
|
default:
|
||
|
return -1;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int get_movies(ssh_session session) {
|
||
|
ssh_channel channel;
|
||
|
int rc;
|
||
|
char read_buffer[256];
|
||
|
int nbytes;
|
||
|
|
||
|
channel = ssh_channel_new(session);
|
||
|
if (channel == NULL)
|
||
|
return SSH_ERROR;
|
||
|
|
||
|
rc = ssh_channel_open_session(channel);
|
||
|
if (rc != SSH_OK) {
|
||
|
fprintf(stderr, "Failed to open channel");
|
||
|
ssh_channel_free(channel);
|
||
|
return rc;
|
||
|
}
|
||
|
|
||
|
rc = ssh_channel_request_exec(channel, "ls -l /");
|
||
|
if (rc != SSH_OK) {
|
||
|
fprintf(stderr, "Failed to request exec");
|
||
|
ssh_channel_close(channel);
|
||
|
ssh_channel_free(channel);
|
||
|
return rc;
|
||
|
}
|
||
|
|
||
|
nbytes = ssh_channel_read(channel, read_buffer, sizeof(read_buffer), 0);
|
||
|
while (nbytes > 0) {
|
||
|
if (fwrite(read_buffer, 1, nbytes, stdout) != nbytes) {
|
||
|
fprintf(stderr, "Failed to write to stdout");
|
||
|
ssh_channel_close(channel);
|
||
|
ssh_channel_free(channel);
|
||
|
return SSH_ERROR;
|
||
|
}
|
||
|
|
||
|
nbytes = ssh_channel_read(channel, read_buffer, sizeof(read_buffer), 0);
|
||
|
|
||
|
if (nbytes < 0) {
|
||
|
fprintf(stderr, "Failed to read bytes from channel");
|
||
|
ssh_channel_close(channel);
|
||
|
ssh_channel_free(channel);
|
||
|
return SSH_ERROR;
|
||
|
}
|
||
|
}
|
||
|
ssh_channel_send_eof(channel);
|
||
|
ssh_channel_close(channel);
|
||
|
ssh_channel_free(channel);
|
||
|
return SSH_OK;
|
||
|
}
|
||
|
|
||
|
int main(int argc, char *argv[]) {
|
||
|
char *host = "localhost";
|
||
|
char *user = getlogin();
|
||
|
int opt;
|
||
|
int rc;
|
||
|
|
||
|
while ((opt = getopt(argc, argv, "h:u:")) != -1)
|
||
|
switch (opt) {
|
||
|
case 'h':
|
||
|
host = optarg;
|
||
|
break;
|
||
|
case 'u':
|
||
|
user = optarg;
|
||
|
break;
|
||
|
case '?':
|
||
|
printf("Unknown option\n");
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
ssh_session session = ssh_new();
|
||
|
if (session == NULL) {
|
||
|
fprintf(stderr, "Failed to get new ssh_session");
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
ssh_options_set(session, SSH_OPTIONS_HOST, host);
|
||
|
ssh_options_set(session, SSH_OPTIONS_USER, user);
|
||
|
|
||
|
if (ssh_connect(session) != SSH_OK) {
|
||
|
fprintf(stderr, "Unable to connect to host: %s\n", ssh_get_error(session));
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
if (verify_host(session) != 0) {
|
||
|
fprintf(stderr, "Attempted to connect to unknown host\n");
|
||
|
ssh_disconnect(session);
|
||
|
ssh_free(session);
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
rc = ssh_userauth_publickey_auto(session, NULL, NULL);
|
||
|
if (rc == SSH_AUTH_ERROR) {
|
||
|
fprintf(stderr, "Authentication failed: %s\n", ssh_get_error(session));
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
if (get_movies(session) != SSH_OK) {
|
||
|
fprintf(stderr, "Unable to list movies: %s\n", ssh_get_error(session));
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
|
||
|
ssh_disconnect(session);
|
||
|
ssh_free(session);
|
||
|
}
|