Last active
March 4, 2016 21:36
-
-
Save leite/0d2cb77bd7a84954eebc to your computer and use it in GitHub Desktop.
test libssh connectivity and iteraction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <libssh/libssh.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <errno.h> | |
#include <string.h> | |
int verify_knownhost (ssh_session session) { | |
int state, hlen; | |
unsigned char *hash = NULL; | |
char *hexa; | |
char buf[10]; | |
state = ssh_is_server_known(session); | |
hlen = ssh_get_pubkey_hash(session, &hash); | |
if (hlen < 0) | |
return -1; | |
switch (state) { | |
case SSH_SERVER_KNOWN_OK: | |
break; // ok | |
case SSH_SERVER_KNOWN_CHANGED: | |
fprintf(stderr, "Host key for server changed: it is now:\n"); | |
ssh_print_hexa("Public key hash", hash, hlen); | |
fprintf(stderr, "For security reasons, connection will be stopped\n"); | |
free(hash); | |
return -1; | |
case SSH_SERVER_FOUND_OTHER: | |
fprintf(stderr, "The host key for this server was not found but an other" | |
"type of key exists.\n"); | |
fprintf(stderr, "An attacker might change the default server key to" | |
"confuse your client into thinking the key does not exist\n"); | |
free(hash); | |
return -1; | |
case SSH_SERVER_FILE_NOT_FOUND: | |
fprintf(stderr, "Could not find known host file.\n"); | |
fprintf(stderr, "If you accept the host key here, the file will be" | |
"automatically created.\n"); | |
// fallback to SSH_SERVER_NOT_KNOWN behavior | |
case SSH_SERVER_NOT_KNOWN: | |
hexa = ssh_get_hexa(hash, hlen); | |
fprintf(stderr,"The server is unknown. Do you trust the host key?\n"); | |
fprintf(stderr, "Public key hash: %s\n", hexa); | |
free(hexa); | |
if (fgets(buf, sizeof(buf), stdin) == NULL) { | |
free(hash); | |
return -1; | |
} | |
if (strncasecmp(buf, "yes", 3) != 0) { | |
free(hash); | |
return -1; | |
} | |
if (ssh_write_knownhost(session) < 0) { | |
fprintf(stderr, "Error %s\n", strerror(errno)); | |
free(hash); | |
return -1; | |
} | |
break; | |
case SSH_SERVER_ERROR: | |
fprintf(stderr, "Error %s", ssh_get_error(session)); | |
free(hash); | |
return -1; | |
} | |
free(hash); | |
return 0; | |
} | |
int show_remote_processes (ssh_session session) { | |
ssh_channel channel; | |
int rc; | |
char 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) { | |
ssh_channel_free(channel); | |
return rc; | |
} | |
rc = ssh_channel_request_exec(channel, "ps aux"); | |
if (rc != SSH_OK) { | |
ssh_channel_close(channel); | |
ssh_channel_free(channel); | |
return rc; | |
} | |
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); | |
while (nbytes > 0) { | |
if (write(1, buffer, nbytes) != (unsigned int) nbytes) { | |
ssh_channel_close(channel); | |
ssh_channel_free(channel); | |
return SSH_ERROR; | |
} | |
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); | |
} | |
if (nbytes < 0) { | |
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 show_remote_files (ssh_session session) { | |
ssh_channel channel; | |
int rc; | |
printf("\n getting channel \n"); | |
channel = ssh_channel_new(session); | |
printf("\n got channel ? \n"); | |
if (channel == NULL) return SSH_ERROR; | |
rc = ssh_channel_open_session(channel); | |
if (rc != SSH_OK) { | |
ssh_channel_free(channel); | |
return rc; | |
} | |
//Once a session is open, you can start the remote command with ssh_channel_request_exec(): | |
rc = ssh_channel_request_exec(channel, "ls -l"); | |
if (rc != SSH_OK) { | |
ssh_channel_close(channel); | |
ssh_channel_free(channel); | |
return rc; | |
} | |
//If the remote command displays data, you get them with ssh_channel_read(). This function returns the number of bytes read. If there is no more data to read on the channel, this function returns 0, and you can go to next step. If an error has been encountered, it returns a negative value: | |
char buffer[256]; | |
int nbytes; | |
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); | |
while (nbytes > 0) { | |
if (fwrite(buffer, 1, nbytes, stdout) != nbytes) { | |
ssh_channel_close(channel); | |
ssh_channel_free(channel); | |
return SSH_ERROR; | |
} | |
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); | |
} | |
if (nbytes < 0) { | |
ssh_channel_close(channel); | |
ssh_channel_free(channel); | |
return SSH_ERROR; | |
} | |
//Once you read the result of the remote command, you send an end-of-file to the channel, close it, and free the memory that it used: | |
ssh_channel_send_eof(channel); | |
ssh_channel_close(channel); | |
ssh_channel_free(channel); | |
return SSH_OK; | |
} | |
int main () { | |
ssh_session session; | |
int rc; | |
int verbosity = SSH_LOG_PROTOCOL; | |
int port = 22; | |
long timeout = 15; | |
int blocking = 1; | |
char *banner; | |
session = ssh_new(); | |
if (session == NULL) exit(-1); | |
printf("\n start \n"); | |
//ssh_set_blocking(session, 1); | |
ssh_options_set(session, SSH_OPTIONS_HOST, "grex.org"); | |
ssh_options_set(session, SSH_OPTIONS_USER, "xico"); | |
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); | |
ssh_options_set(session, SSH_OPTIONS_PORT, &port); | |
ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &timeout); | |
printf("\n getting session \n"); | |
rc = ssh_connect(session); | |
if (rc != SSH_OK) { | |
fprintf(stderr, "SSH_CONNECT: %s\n", ssh_get_error(session)); | |
exit(-1); | |
} | |
rc = ssh_userauth_password(session, NULL, "29novell"); | |
if (rc == SSH_AUTH_ERROR) { | |
fprintf(stderr, "Authentication failed: %s\n", ssh_get_error(session)); | |
return SSH_AUTH_ERROR; | |
} | |
rc = ssh_userauth_none(session, NULL); | |
if (rc == SSH_AUTH_ERROR) | |
return rc; | |
printf("\n getting banner \n"); | |
banner = ssh_get_issue_banner(session); | |
printf("\n got banner ? %s \n", banner); | |
if (banner) { | |
printf("%s\n", banner); | |
free(banner); | |
} | |
if (verify_knownhost(session) < 0) { | |
ssh_disconnect(session); | |
ssh_free(session); | |
exit(-1); | |
} | |
rc = show_remote_processes(session); | |
if (rc != SSH_OK) { | |
fprintf(stderr, "Error: %s\n", ssh_get_error(session)); | |
exit(-1); | |
} | |
printf("\n show remote files \n"); | |
rc = show_remote_files(session); | |
if (rc != SSH_OK) { | |
//ssh_channel_close(session); | |
//ssh_channel_free(session); | |
fprintf(stderr, "Error: %s\n", ssh_get_error(session)); | |
return rc; | |
} | |
ssh_disconnect(session); | |
ssh_free(session); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to compile
output