Skip to content

Instantly share code, notes, and snippets.

@losinggeneration
Created December 10, 2012 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save losinggeneration/4251944 to your computer and use it in GitHub Desktop.
Save losinggeneration/4251944 to your computer and use it in GitHub Desktop.
libssh with & without goto for error handling
#include <stdio.h>
#include <stdlib.h>
#include <libssh/libssh.h>
int main(int argc, const char *argv[]) {
char *host = "localhost", *username = "username", *password = "password";
int port = 22, timeout = 5;
ssh_init();
atexit((void(*)())ssh_finalize);
ssh_session s = ssh_new();
// Terrible, we're out of memory! All bets are off!
if(s == NULL) {
fprintf(stderr, "Unable to allocate SSH session\n");
exit(1);
}
// Set our host and port options
if((ssh_options_set(s, SSH_OPTIONS_HOST, host) |
ssh_options_set(s, SSH_OPTIONS_PORT, &port) |
ssh_options_set(s, SSH_OPTIONS_TIMEOUT, &timeout)) < 0) {
fprintf(stderr, "Unable to set SSH options for %s:\n\t%s\n", host, ssh_get_error(s));
ssh_free(s);
exit(1);
}
// Try to connect
if(ssh_connect(s) != SSH_OK) {
fprintf(stderr, "Unable to connect to %s:\n\t%s\n", host, ssh_get_error(s));
ssh_free(s);
exit(1);
}
// Authenticate our mikrotik user
if(ssh_userauth_password(s, username, password) != SSH_AUTH_SUCCESS) {
fprintf(stderr, "Unable to authenticate user: %s\n", username);
ssh_disconnect(s);
ssh_free(s);
exit(1);
}
// Then we'd setup a channel to send data to and from
// Clean up
ssh_disconnect(s);
ssh_free(s);
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#include <libssh/libssh.h>
int main(int argc, const char *argv[]) {
char *host = "localhost", *username = "username", *password = "password";
int port = 22, timeout = 5;
ssh_init();
atexit((void(*)())ssh_finalize);
ssh_session s = ssh_new();
// Terrible, we're out of memory! All bets are off!
if(s == NULL) {
fprintf(stderr, "Unable to allocate SSH session\n");
exit(1);
}
// Set our host and port options
if((ssh_options_set(s, SSH_OPTIONS_HOST, host) |
ssh_options_set(s, SSH_OPTIONS_PORT, &port) |
ssh_options_set(s, SSH_OPTIONS_TIMEOUT, &timeout)) < 0) {
fprintf(stderr, "Unable to set SSH options for %s:\n\t%s\n", host, ssh_get_error(s));
goto connect_failed;
}
// Try to connect
if(ssh_connect(s) != SSH_OK) {
fprintf(stderr, "Unable to connect to %s:\n\t%s\n", host, ssh_get_error(s));
goto connect_failed;
}
// Authenticate our mikrotik user
if(ssh_userauth_password(s, username, password) != SSH_AUTH_SUCCESS) {
fprintf(stderr, "Unable to authenticate user: %s\n", username);
goto authenticate_failed;
}
// Then we'd setup a channel to send data to and from
authenticate_failed: // We get here if we have the wrong credentials
ssh_disconnect(s);
connect_failed: // and we get here if we had bad options or unable to create the session
ssh_free(s);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment