Alexander Avery
3 years ago
2 changed files with 126 additions and 0 deletions
@ -0,0 +1 @@ |
|||||
|
build/ |
@ -0,0 +1,125 @@ |
|||||
|
#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); |
||||
|
} |
Loading…
Reference in new issue