Created
December 10, 2012 17:20
-
-
Save losinggeneration/4251944 to your computer and use it in GitHub Desktop.
libssh with & without goto for error handling
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 <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; | |
} |
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 <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