Skip to content

Instantly share code, notes, and snippets.

@timmartin
Created October 7, 2010 11:10
Show Gist options
  • Save timmartin/614962 to your computer and use it in GitHub Desktop.
Save timmartin/614962 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <libssh2.h>
int main (int argc, char ** argv)
{
if (argc < 2) {
printf("usage: test <ip_address>\n");
return -1;
}
LIBSSH2_SESSION * session = NULL;
session = libssh2_session_init();
if (!session) {
printf("Failed to initialise session\n");
return -1;
}
int sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(22);
sin.sin_addr.s_addr = inet_addr(argv[1]);
if (connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) {
printf("Failed to connect\n");
return -1;
}
if (libssh2_session_startup(session, sock) < 0) {
printf("Failed to start session\n");
char * error_message = NULL;
int error_length = 0;
libssh2_session_last_error(session, &error_message, &error_length, 0);
printf("%s\n", error_message);
return -1;
}
printf("Successfully connected\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment