2
* $Id: scp_write.c,v 1.7 2009/04/28 10:35:30 bagder Exp $
4
* Sample showing how to do a simple SCP transfer.
7
#include "libssh2_config.h"
10
#ifdef HAVE_WINSOCK2_H
11
# include <winsock2.h>
13
#ifdef HAVE_SYS_SOCKET_H
14
# include <sys/socket.h>
16
#ifdef HAVE_NETINET_IN_H
17
# include <netinet/in.h>
22
#ifdef HAVE_ARPA_INET_H
23
# include <arpa/inet.h>
25
#ifdef HAVE_SYS_TIME_H
26
# include <sys/time.h>
29
#include <sys/types.h>
35
int main(int argc, char *argv[])
37
unsigned long hostaddr;
38
int sock, i, auth_pw = 1;
39
struct sockaddr_in sin;
40
const char *fingerprint;
41
LIBSSH2_SESSION *session;
42
LIBSSH2_CHANNEL *channel;
43
const char *username="username";
44
const char *password="password";
45
const char *loclfile="scp_write.c";
46
const char *scppath="/tmp/TEST";
57
WSAStartup(MAKEWORD(2,0), &wsadata);
61
hostaddr = inet_addr(argv[1]);
63
hostaddr = htonl(0x7F000001);
78
local = fopen(loclfile, "rb");
80
fprintf(stderr, "Can't local file %s\n", loclfile);
84
stat(loclfile, &fileinfo);
86
/* Ultra basic "connect to port 22 on localhost"
87
* Your code is responsible for creating the socket establishing the
90
sock = socket(AF_INET, SOCK_STREAM, 0);
92
sin.sin_family = AF_INET;
93
sin.sin_port = htons(22);
94
sin.sin_addr.s_addr = hostaddr;
95
if (connect(sock, (struct sockaddr*)(&sin),
96
sizeof(struct sockaddr_in)) != 0) {
97
fprintf(stderr, "failed to connect!\n");
101
/* Create a session instance
103
session = libssh2_session_init();
107
/* ... start it up. This will trade welcome banners, exchange keys,
108
* and setup crypto, compression, and MAC layers
110
rc = libssh2_session_startup(session, sock);
112
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
116
/* At this point we havn't yet authenticated. The first thing to do
117
* is check the hostkey's fingerprint against our known hosts Your app
118
* may have it hard coded, may go to a file, may present it to the
119
* user, that's your call
121
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
122
fprintf(stderr, "Fingerprint: ");
123
for(i = 0; i < 20; i++) {
124
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
126
fprintf(stderr, "\n");
129
/* We could authenticate via password */
130
if (libssh2_userauth_password(session, username, password)) {
131
fprintf(stderr, "Authentication by password failed.\n");
135
/* Or by public key */
136
if (libssh2_userauth_publickey_fromfile(session, username,
137
"/home/username/.ssh/id_rsa.pub",
138
"/home/username/.ssh/id_rsa",
140
fprintf(stderr, "\tAuthentication by public key failed\n");
145
/* Send a file via scp. The mode parameter must only have permissions! */
146
channel = libssh2_scp_send(session, scppath, fileinfo.st_mode & 0777,
147
(unsigned long)fileinfo.st_size);
150
fprintf(stderr, "Unable to open a session\n");
154
fprintf(stderr, "SCP session waiting to send file\n");
156
nread = fread(mem, 1, sizeof(mem), local);
165
/* write the same data over and over, until error or completion */
166
rc = libssh2_channel_write(channel, ptr, nread);
168
fprintf(stderr, "ERROR %d\n", rc);
170
/* rc indicates how many bytes were written this time */
173
} while (rc > 0 && sent < nread);
178
fprintf(stderr, "Sending EOF\n");
179
libssh2_channel_send_eof(channel);
181
fprintf(stderr, "Waiting for EOF\n");
182
libssh2_channel_wait_eof(channel);
184
fprintf(stderr, "Waiting for channel to close\n");
185
libssh2_channel_wait_closed(channel);
187
libssh2_channel_free(channel);
192
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
193
libssh2_session_free(session);
200
fprintf(stderr, "all done\n");