13
#include "libssh2_setup.h"
15
#include <libssh2_sftp.h>
17
#ifdef HAVE_SYS_SOCKET_H
18
#include <sys/socket.h>
23
#ifdef HAVE_NETINET_IN_H
24
#include <netinet/in.h>
26
#ifdef HAVE_ARPA_INET_H
32
static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
33
static const char *privkey = "/home/username/.ssh/id_rsa";
34
static const char *username = "username";
35
static const char *password = "password";
36
static const char *sftppath = "/tmp/sftp_mkdir";
38
int main(int argc, char *argv[])
41
libssh2_socket_t sock;
43
struct sockaddr_in sin;
44
const char *fingerprint;
46
LIBSSH2_SESSION *session = NULL;
47
LIBSSH2_SFTP *sftp_session;
52
rc = WSAStartup(MAKEWORD(2, 0), &wsadata);
54
fprintf(stderr, "WSAStartup failed with error: %d\n", rc);
60
hostaddr = inet_addr(argv[1]);
63
hostaddr = htonl(0x7F000001);
77
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
85
sock = socket(AF_INET, SOCK_STREAM, 0);
86
if(sock == LIBSSH2_INVALID_SOCKET) {
87
fprintf(stderr, "failed to create socket.\n");
91
sin.sin_family = AF_INET;
92
sin.sin_port = htons(22);
93
sin.sin_addr.s_addr = hostaddr;
94
if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
95
fprintf(stderr, "failed to connect.\n");
100
session = libssh2_session_init();
102
fprintf(stderr, "Could not initialize SSH session.\n");
109
rc = libssh2_session_handshake(session, sock);
111
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
120
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
121
fprintf(stderr, "Fingerprint: ");
122
for(i = 0; i < 20; i++) {
123
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
125
fprintf(stderr, "\n");
129
if(libssh2_userauth_password(session, username, password)) {
130
fprintf(stderr, "Authentication by password failed.\n");
136
if(libssh2_userauth_publickey_fromfile(session, username,
139
fprintf(stderr, "Authentication by public key failed.\n");
144
sftp_session = libssh2_sftp_init(session);
147
fprintf(stderr, "Unable to init SFTP session\n");
152
libssh2_session_set_blocking(session, 1);
155
rc = libssh2_sftp_mkdir(sftp_session, sftppath,
156
LIBSSH2_SFTP_S_IRWXU |
157
LIBSSH2_SFTP_S_IRGRP |
158
LIBSSH2_SFTP_S_IXGRP |
159
LIBSSH2_SFTP_S_IROTH |
160
LIBSSH2_SFTP_S_IXOTH);
162
fprintf(stderr, "libssh2_sftp_mkdir failed: %d\n", rc);
164
libssh2_sftp_shutdown(sftp_session);
169
libssh2_session_disconnect(session, "Normal Shutdown");
170
libssh2_session_free(session);
173
if(sock != LIBSSH2_INVALID_SOCKET) {
175
LIBSSH2_SOCKET_CLOSE(sock);
178
fprintf(stderr, "all done\n");