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 *loclfile = "sftp_write.c";
37
static const char *sftppath = "/tmp/TEST";
39
int main(int argc, char *argv[])
42
libssh2_socket_t sock;
44
struct sockaddr_in sin;
45
const char *fingerprint;
47
LIBSSH2_SESSION *session = NULL;
48
LIBSSH2_SFTP *sftp_session;
49
LIBSSH2_SFTP_HANDLE *sftp_handle;
59
rc = WSAStartup(MAKEWORD(2, 0), &wsadata);
61
fprintf(stderr, "WSAStartup failed with error: %d\n", rc);
67
hostaddr = inet_addr(argv[1]);
70
hostaddr = htonl(0x7F000001);
87
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
91
local = fopen(loclfile, "rb");
93
fprintf(stderr, "Cannot open local file %s\n", loclfile);
101
sock = socket(AF_INET, SOCK_STREAM, 0);
102
if(sock == LIBSSH2_INVALID_SOCKET) {
103
fprintf(stderr, "failed to create socket.\n");
107
sin.sin_family = AF_INET;
108
sin.sin_port = htons(22);
109
sin.sin_addr.s_addr = hostaddr;
110
if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
111
fprintf(stderr, "failed to connect.\n");
116
session = libssh2_session_init();
118
fprintf(stderr, "Could not initialize SSH session.\n");
123
libssh2_session_set_blocking(session, 1);
128
rc = libssh2_session_handshake(session, sock);
130
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
139
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
140
fprintf(stderr, "Fingerprint: ");
141
for(i = 0; i < 20; i++) {
142
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
144
fprintf(stderr, "\n");
148
if(libssh2_userauth_password(session, username, password)) {
149
fprintf(stderr, "Authentication by password failed.\n");
155
if(libssh2_userauth_publickey_fromfile(session, username,
158
fprintf(stderr, "Authentication by public key failed.\n");
163
fprintf(stderr, "libssh2_sftp_init().\n");
164
sftp_session = libssh2_sftp_init(session);
167
fprintf(stderr, "Unable to init SFTP session\n");
171
fprintf(stderr, "libssh2_sftp_open().\n");
173
sftp_handle = libssh2_sftp_open(sftp_session, sftppath,
177
LIBSSH2_SFTP_S_IRUSR |
178
LIBSSH2_SFTP_S_IWUSR |
179
LIBSSH2_SFTP_S_IRGRP |
180
LIBSSH2_SFTP_S_IROTH);
182
fprintf(stderr, "Unable to open file with SFTP: %ld\n",
183
libssh2_sftp_last_error(sftp_session));
187
fprintf(stderr, "libssh2_sftp_open() is done, now send data.\n");
189
nread = fread(mem, 1, sizeof(mem), local);
198
nwritten = libssh2_sftp_write(sftp_handle, ptr, nread);
202
nread -= (size_t)nwritten;
204
} while(nwritten > 0);
206
libssh2_sftp_close(sftp_handle);
207
libssh2_sftp_shutdown(sftp_session);
212
libssh2_session_disconnect(session, "Normal Shutdown");
213
libssh2_session_free(session);
216
if(sock != LIBSSH2_INVALID_SOCKET) {
218
LIBSSH2_SOCKET_CLOSE(sock);
224
fprintf(stderr, "all done\n");