removed compiler warnings, narrowed some source lines, killed trailing
whitespace
This commit is contained in:
parent
14881b2370
commit
e0254f3936
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* $Id: scp_write.c,v 1.1 2007/06/06 12:34:08 jehousley Exp $
|
* $Id: scp_write.c,v 1.2 2007/07/14 20:54:47 bagder Exp $
|
||||||
*
|
*
|
||||||
* Sample showing how to do a simple SCP transfer.
|
* Sample showing how to do a simple SCP transfer.
|
||||||
*/
|
*/
|
||||||
@ -58,31 +58,31 @@ int main(int argc, char *argv[])
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
hostaddr = inet_addr(argv[1]);
|
hostaddr = inet_addr(argv[1]);
|
||||||
} else {
|
} else {
|
||||||
hostaddr = htonl(0x7F000001);
|
hostaddr = htonl(0x7F000001);
|
||||||
}
|
}
|
||||||
if (argc > 2) {
|
if (argc > 2) {
|
||||||
username = argv[2];
|
username = argv[2];
|
||||||
}
|
}
|
||||||
if (argc > 3) {
|
if (argc > 3) {
|
||||||
password = argv[3];
|
password = argv[3];
|
||||||
}
|
}
|
||||||
if(argc > 4) {
|
if(argc > 4) {
|
||||||
loclfile = argv[4];
|
loclfile = argv[4];
|
||||||
}
|
}
|
||||||
if (argc > 5) {
|
if (argc > 5) {
|
||||||
scppath = argv[5];
|
scppath = argv[5];
|
||||||
}
|
}
|
||||||
|
|
||||||
local = fopen(loclfile, "rb");
|
local = fopen(loclfile, "rb");
|
||||||
if (!local) {
|
if (!local) {
|
||||||
fprintf(stderr, "Can't local file %s\n", loclfile);
|
fprintf(stderr, "Can't local file %s\n", loclfile);
|
||||||
goto shutdown;
|
goto shutdown;
|
||||||
}
|
}
|
||||||
|
|
||||||
stat(loclfile, &fileinfo);
|
stat(loclfile, &fileinfo);
|
||||||
|
|
||||||
/* Ultra basic "connect to port 22 on localhost"
|
/* Ultra basic "connect to port 22 on localhost"
|
||||||
* Your code is responsible for creating the socket establishing the
|
* Your code is responsible for creating the socket establishing the
|
||||||
* connection
|
* connection
|
||||||
@ -93,24 +93,24 @@ int main(int argc, char *argv[])
|
|||||||
sin.sin_port = htons(22);
|
sin.sin_port = htons(22);
|
||||||
sin.sin_addr.s_addr = hostaddr;
|
sin.sin_addr.s_addr = hostaddr;
|
||||||
if (connect(sock, (struct sockaddr*)(&sin),
|
if (connect(sock, (struct sockaddr*)(&sin),
|
||||||
sizeof(struct sockaddr_in)) != 0) {
|
sizeof(struct sockaddr_in)) != 0) {
|
||||||
fprintf(stderr, "failed to connect!\n");
|
fprintf(stderr, "failed to connect!\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Create a session instance
|
/* Create a session instance
|
||||||
*/
|
*/
|
||||||
session = libssh2_session_init();
|
session = libssh2_session_init();
|
||||||
if(!session)
|
if(!session)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
/* ... start it up. This will trade welcome banners, exchange keys,
|
/* ... start it up. This will trade welcome banners, exchange keys,
|
||||||
* and setup crypto, compression, and MAC layers
|
* and setup crypto, compression, and MAC layers
|
||||||
*/
|
*/
|
||||||
rc = libssh2_session_startup(session, sock);
|
rc = libssh2_session_startup(session, sock);
|
||||||
if(rc) {
|
if(rc) {
|
||||||
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
|
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* At this point we havn't yet authenticated. The first thing to do
|
/* At this point we havn't yet authenticated. The first thing to do
|
||||||
@ -121,66 +121,62 @@ int main(int argc, char *argv[])
|
|||||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
|
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
|
||||||
fprintf(stderr, "Fingerprint: ");
|
fprintf(stderr, "Fingerprint: ");
|
||||||
for(i = 0; i < 16; i++) {
|
for(i = 0; i < 16; i++) {
|
||||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||||
}
|
}
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
|
|
||||||
if (auth_pw) {
|
if (auth_pw) {
|
||||||
/* We could authenticate via password */
|
/* We could authenticate via password */
|
||||||
if (libssh2_userauth_password(session, username, password)) {
|
if (libssh2_userauth_password(session, username, password)) {
|
||||||
fprintf(stderr, "Authentication by password failed.\n");
|
fprintf(stderr, "Authentication by password failed.\n");
|
||||||
goto shutdown;
|
goto shutdown;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* Or by public key */
|
/* Or by public key */
|
||||||
if (libssh2_userauth_publickey_fromfile(session, username,
|
if (libssh2_userauth_publickey_fromfile(session, username,
|
||||||
"/home/username/.ssh/id_rsa.pub",
|
"/home/username/.ssh/id_rsa.pub",
|
||||||
"/home/username/.ssh/id_rsa",
|
"/home/username/.ssh/id_rsa",
|
||||||
password)) {
|
password)) {
|
||||||
fprintf(stderr, "\tAuthentication by public key failed\n");
|
fprintf(stderr, "\tAuthentication by public key failed\n");
|
||||||
goto shutdown;
|
goto shutdown;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//libssh2_trace(session, 0xFFFF);
|
|
||||||
|
|
||||||
/* Request a file via SCP */
|
/* Request a file via SCP */
|
||||||
channel = libssh2_scp_send(session, scppath, 0x1FF & fileinfo.st_mode, (unsigned long)fileinfo.st_size);
|
channel = libssh2_scp_send(session, scppath, 0x1FF & fileinfo.st_mode,
|
||||||
|
(unsigned long)fileinfo.st_size);
|
||||||
|
|
||||||
if (!channel) {
|
if (!channel) {
|
||||||
fprintf(stderr, "Unable to open a session\n");
|
fprintf(stderr, "Unable to open a session\n");
|
||||||
goto shutdown;
|
goto shutdown;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "SCP session waiting to send file\n");
|
fprintf(stderr, "SCP session waiting to send file\n");
|
||||||
do {
|
do {
|
||||||
nread = fread(mem, 1, sizeof(mem), local);
|
nread = fread(mem, 1, sizeof(mem), local);
|
||||||
if (nread <= 0) {
|
if (nread <= 0) {
|
||||||
/* end of file */
|
/* end of file */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ptr = mem;
|
ptr = mem;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
/* write data in a loop until we block */
|
/* write data in a loop until we block */
|
||||||
rc = libssh2_channel_write(channel, ptr, nread);
|
rc = libssh2_channel_write(channel, ptr, nread);
|
||||||
ptr += rc;
|
ptr += rc;
|
||||||
nread -= nread;
|
nread -= nread;
|
||||||
} while (rc > 0);
|
} while (rc > 0);
|
||||||
} while (1);
|
} while (1);
|
||||||
|
|
||||||
fprintf(stderr, "Sending EOF\n");
|
fprintf(stderr, "Sending EOF\n");
|
||||||
libssh2_channel_send_eof(channel);
|
libssh2_channel_send_eof(channel);
|
||||||
|
|
||||||
fprintf(stderr, "Waiting for EOF\n");
|
fprintf(stderr, "Waiting for EOF\n");
|
||||||
libssh2_channel_wait_eof(channel);
|
libssh2_channel_wait_eof(channel);
|
||||||
|
|
||||||
fprintf(stderr, "Waiting for channel to close\n");
|
fprintf(stderr, "Waiting for channel to close\n");
|
||||||
libssh2_channel_wait_closed(channel);
|
libssh2_channel_wait_closed(channel);
|
||||||
|
|
||||||
// fprintf(stderr, "Closing channel\n");
|
|
||||||
// libssh2_channel_close(channel);
|
|
||||||
|
|
||||||
libssh2_channel_free(channel);
|
libssh2_channel_free(channel);
|
||||||
channel = NULL;
|
channel = NULL;
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* $Id: scp_write_nonblock.c,v 1.3 2007/07/05 11:08:17 jehousley Exp $
|
* $Id: scp_write_nonblock.c,v 1.4 2007/07/14 20:54:47 bagder Exp $
|
||||||
*
|
*
|
||||||
* Sample showing how to do a simple SCP transfer.
|
* Sample showing how to do a simple SCP transfer.
|
||||||
*/
|
*/
|
||||||
@ -58,31 +58,31 @@ int main(int argc, char *argv[])
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
hostaddr = inet_addr(argv[1]);
|
hostaddr = inet_addr(argv[1]);
|
||||||
} else {
|
} else {
|
||||||
hostaddr = htonl(0x7F000001);
|
hostaddr = htonl(0x7F000001);
|
||||||
}
|
}
|
||||||
if (argc > 2) {
|
if (argc > 2) {
|
||||||
username = argv[2];
|
username = argv[2];
|
||||||
}
|
}
|
||||||
if (argc > 3) {
|
if (argc > 3) {
|
||||||
password = argv[3];
|
password = argv[3];
|
||||||
}
|
}
|
||||||
if(argc > 4) {
|
if(argc > 4) {
|
||||||
loclfile = argv[4];
|
loclfile = argv[4];
|
||||||
}
|
}
|
||||||
if (argc > 5) {
|
if (argc > 5) {
|
||||||
scppath = argv[5];
|
scppath = argv[5];
|
||||||
}
|
}
|
||||||
|
|
||||||
local = fopen(loclfile, "rb");
|
local = fopen(loclfile, "rb");
|
||||||
if (!local) {
|
if (!local) {
|
||||||
fprintf(stderr, "Can't local file %s\n", loclfile);
|
fprintf(stderr, "Can't local file %s\n", loclfile);
|
||||||
goto shutdown;
|
goto shutdown;
|
||||||
}
|
}
|
||||||
|
|
||||||
stat(loclfile, &fileinfo);
|
stat(loclfile, &fileinfo);
|
||||||
|
|
||||||
/* Ultra basic "connect to port 22 on localhost"
|
/* Ultra basic "connect to port 22 on localhost"
|
||||||
* Your code is responsible for creating the socket establishing the
|
* Your code is responsible for creating the socket establishing the
|
||||||
* connection
|
* connection
|
||||||
@ -93,13 +93,13 @@ int main(int argc, char *argv[])
|
|||||||
sin.sin_port = htons(22);
|
sin.sin_port = htons(22);
|
||||||
sin.sin_addr.s_addr = hostaddr;
|
sin.sin_addr.s_addr = hostaddr;
|
||||||
if (connect(sock, (struct sockaddr*)(&sin),
|
if (connect(sock, (struct sockaddr*)(&sin),
|
||||||
sizeof(struct sockaddr_in)) != 0) {
|
sizeof(struct sockaddr_in)) != 0) {
|
||||||
fprintf(stderr, "failed to connect!\n");
|
fprintf(stderr, "failed to connect!\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We set the socket non-blocking. We do it after the connect just to
|
/* We set the socket non-blocking. We do it after the connect just to
|
||||||
simplify the example code. */
|
simplify the example code. */
|
||||||
#ifdef F_SETFL
|
#ifdef F_SETFL
|
||||||
/* FIXME: this can/should be done in a more portable manner */
|
/* FIXME: this can/should be done in a more portable manner */
|
||||||
rc = fcntl(sock, F_GETFL, 0);
|
rc = fcntl(sock, F_GETFL, 0);
|
||||||
@ -107,24 +107,24 @@ int main(int argc, char *argv[])
|
|||||||
#else
|
#else
|
||||||
#error "add support for setting the socket non-blocking here"
|
#error "add support for setting the socket non-blocking here"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Create a session instance
|
/* Create a session instance
|
||||||
*/
|
*/
|
||||||
session = libssh2_session_init();
|
session = libssh2_session_init();
|
||||||
if(!session)
|
if(!session)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
/* Since we have set non-blocking, tell libssh2 we are non-blocking */
|
/* Since we have set non-blocking, tell libssh2 we are non-blocking */
|
||||||
libssh2_session_set_blocking(session, 0);
|
libssh2_session_set_blocking(session, 0);
|
||||||
|
|
||||||
/* ... start it up. This will trade welcome banners, exchange keys,
|
/* ... start it up. This will trade welcome banners, exchange keys,
|
||||||
* and setup crypto, compression, and MAC layers
|
* and setup crypto, compression, and MAC layers
|
||||||
*/
|
*/
|
||||||
while ((rc = libssh2_session_startup(session, sock))
|
while ((rc = libssh2_session_startup(session, sock))
|
||||||
== LIBSSH2_ERROR_EAGAIN);
|
== LIBSSH2_ERROR_EAGAIN);
|
||||||
if(rc) {
|
if(rc) {
|
||||||
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
|
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* At this point we havn't yet authenticated. The first thing to do
|
/* At this point we havn't yet authenticated. The first thing to do
|
||||||
@ -135,82 +135,82 @@ int main(int argc, char *argv[])
|
|||||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
|
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
|
||||||
fprintf(stderr, "Fingerprint: ");
|
fprintf(stderr, "Fingerprint: ");
|
||||||
for(i = 0; i < 16; i++) {
|
for(i = 0; i < 16; i++) {
|
||||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||||
}
|
}
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
|
|
||||||
if (auth_pw) {
|
if (auth_pw) {
|
||||||
/* We could authenticate via password */
|
/* We could authenticate via password */
|
||||||
while ((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN);
|
while ((rc = libssh2_userauth_password(session, username, password)) ==
|
||||||
if (rc) {
|
LIBSSH2_ERROR_EAGAIN);
|
||||||
fprintf(stderr, "Authentication by password failed.\n");
|
if (rc) {
|
||||||
goto shutdown;
|
fprintf(stderr, "Authentication by password failed.\n");
|
||||||
}
|
goto shutdown;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
/* Or by public key */
|
/* Or by public key */
|
||||||
while ((rc = libssh2_userauth_publickey_fromfile(session, username,
|
while ((rc = libssh2_userauth_publickey_fromfile(session, username,
|
||||||
"/home/username/.ssh/id_rsa.pub",
|
"/home/username/.ssh/id_rsa.pub",
|
||||||
"/home/username/.ssh/id_rsa",
|
"/home/username/.ssh/id_rsa",
|
||||||
password)) == LIBSSH2_ERROR_EAGAIN);
|
password)) == LIBSSH2_ERROR_EAGAIN);
|
||||||
if (rc) {
|
if (rc) {
|
||||||
fprintf(stderr, "\tAuthentication by public key failed\n");
|
fprintf(stderr, "\tAuthentication by public key failed\n");
|
||||||
goto shutdown;
|
goto shutdown;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// libssh2_trace(session, 0xFF7D);
|
|
||||||
|
|
||||||
/* Request a file via SCP */
|
/* Request a file via SCP */
|
||||||
do {
|
do {
|
||||||
channel = libssh2_scp_send(session, scppath, 0x1FF & fileinfo.st_mode, (unsigned long)fileinfo.st_size);
|
channel = libssh2_scp_send(session, scppath, 0x1FF & fileinfo.st_mode,
|
||||||
|
(unsigned long)fileinfo.st_size);
|
||||||
if ((!channel) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
|
|
||||||
|
if ((!channel) && (libssh2_session_last_errno(session) !=
|
||||||
|
LIBSSH2_ERROR_EAGAIN)) {
|
||||||
char *err_msg;
|
char *err_msg;
|
||||||
|
|
||||||
libssh2_session_last_error(session, &err_msg, NULL, 0);
|
libssh2_session_last_error(session, &err_msg, NULL, 0);
|
||||||
fprintf(stderr, "%s\n", err_msg);
|
fprintf(stderr, "%s\n", err_msg);
|
||||||
goto shutdown;
|
goto shutdown;
|
||||||
}
|
}
|
||||||
} while (!channel);
|
} while (!channel);
|
||||||
|
|
||||||
fprintf(stderr, "SCP session waiting to send file\n");
|
fprintf(stderr, "SCP session waiting to send file\n");
|
||||||
do {
|
do {
|
||||||
nread = fread(mem, 1, sizeof(mem), local);
|
nread = fread(mem, 1, sizeof(mem), local);
|
||||||
if (nread <= 0) {
|
if (nread <= 0) {
|
||||||
/* end of file */
|
/* end of file */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ptr = mem;
|
ptr = mem;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
/* write data in a loop until we block */
|
/* write data in a loop until we block */
|
||||||
while ((rc = libssh2_channel_write(channel, ptr, nread)) == LIBSSH2_ERROR_EAGAIN);
|
while ((rc = libssh2_channel_write(channel, ptr, nread)) ==
|
||||||
if (rc < 0) {
|
LIBSSH2_ERROR_EAGAIN);
|
||||||
fprintf(stderr, "ERROR %d\n", rc);
|
if (rc < 0) {
|
||||||
}
|
fprintf(stderr, "ERROR %d\n", rc);
|
||||||
ptr += rc;
|
}
|
||||||
nread -= rc;
|
ptr += rc;
|
||||||
} while (nread > 0);
|
nread -= rc;
|
||||||
|
} while (nread > 0);
|
||||||
} while (1);
|
} while (1);
|
||||||
|
|
||||||
fprintf(stderr, "Sending EOF\n");
|
fprintf(stderr, "Sending EOF\n");
|
||||||
while (libssh2_channel_send_eof(channel) == LIBSSH2_ERROR_EAGAIN);
|
while (libssh2_channel_send_eof(channel) == LIBSSH2_ERROR_EAGAIN);
|
||||||
|
|
||||||
fprintf(stderr, "Waiting for EOF\n");
|
fprintf(stderr, "Waiting for EOF\n");
|
||||||
while (libssh2_channel_wait_eof(channel) == LIBSSH2_ERROR_EAGAIN);
|
while (libssh2_channel_wait_eof(channel) == LIBSSH2_ERROR_EAGAIN);
|
||||||
|
|
||||||
fprintf(stderr, "Waiting for channel to close\n");
|
fprintf(stderr, "Waiting for channel to close\n");
|
||||||
while (libssh2_channel_wait_closed(channel) == LIBSSH2_ERROR_EAGAIN);
|
while (libssh2_channel_wait_closed(channel) == LIBSSH2_ERROR_EAGAIN);
|
||||||
|
|
||||||
// fprintf(stderr, "Closing channel\n");
|
|
||||||
// while (libssh2_channel_close(channel) == LIBSSH2_ERROR_EAGAIN);
|
|
||||||
|
|
||||||
libssh2_channel_free(channel);
|
libssh2_channel_free(channel);
|
||||||
channel = NULL;
|
channel = NULL;
|
||||||
|
|
||||||
shutdown:
|
shutdown:
|
||||||
|
|
||||||
while ((rc = libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing")) == LIBSSH2_ERROR_EAGAIN);
|
while ((rc = libssh2_session_disconnect(session,
|
||||||
|
"Normal Shutdown, Thank you for playing")) == LIBSSH2_ERROR_EAGAIN);
|
||||||
libssh2_session_free(session);
|
libssh2_session_free(session);
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* $Id: sftp_write.c,v 1.4 2007/06/06 12:34:09 jehousley Exp $
|
* $Id: sftp_write.c,v 1.5 2007/07/14 20:54:47 bagder Exp $
|
||||||
*
|
*
|
||||||
* Sample showing how to do SFTP write transfers.
|
* Sample showing how to do SFTP write transfers.
|
||||||
*
|
*
|
||||||
@ -61,30 +61,30 @@ int main(int argc, char *argv[])
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
hostaddr = inet_addr(argv[1]);
|
hostaddr = inet_addr(argv[1]);
|
||||||
} else {
|
} else {
|
||||||
hostaddr = htonl(0x7F000001);
|
hostaddr = htonl(0x7F000001);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(argc > 2) {
|
if(argc > 2) {
|
||||||
username = argv[2];
|
username = argv[2];
|
||||||
}
|
}
|
||||||
if(argc > 3) {
|
if(argc > 3) {
|
||||||
password = argv[3];
|
password = argv[3];
|
||||||
}
|
}
|
||||||
if(argc > 4) {
|
if(argc > 4) {
|
||||||
loclfile = argv[4];
|
loclfile = argv[4];
|
||||||
}
|
}
|
||||||
if(argc > 5) {
|
if(argc > 5) {
|
||||||
sftppath = argv[5];
|
sftppath = argv[5];
|
||||||
}
|
}
|
||||||
|
|
||||||
local = fopen(loclfile, "rb");
|
local = fopen(loclfile, "rb");
|
||||||
if (!local) {
|
if (!local) {
|
||||||
printf("Can't local file %s\n", loclfile);
|
printf("Can't local file %s\n", loclfile);
|
||||||
goto shutdown;
|
goto shutdown;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The application code is responsible for creating the socket
|
* The application code is responsible for creating the socket
|
||||||
* and establishing the connection
|
* and establishing the connection
|
||||||
@ -94,28 +94,28 @@ int main(int argc, char *argv[])
|
|||||||
sin.sin_family = AF_INET;
|
sin.sin_family = AF_INET;
|
||||||
sin.sin_port = htons(22);
|
sin.sin_port = htons(22);
|
||||||
sin.sin_addr.s_addr = hostaddr;
|
sin.sin_addr.s_addr = hostaddr;
|
||||||
if (connect(sock, (struct sockaddr*)(&sin),
|
if (connect(sock, (struct sockaddr*)(&sin),
|
||||||
sizeof(struct sockaddr_in)) != 0) {
|
sizeof(struct sockaddr_in)) != 0) {
|
||||||
fprintf(stderr, "failed to connect!\n");
|
fprintf(stderr, "failed to connect!\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Create a session instance
|
/* Create a session instance
|
||||||
*/
|
*/
|
||||||
session = libssh2_session_init();
|
session = libssh2_session_init();
|
||||||
if(!session)
|
if(!session)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
/* Since we have set non-blocking, tell libssh2 we are blocking */
|
/* Since we have set non-blocking, tell libssh2 we are blocking */
|
||||||
libssh2_session_set_blocking(session, 1);
|
libssh2_session_set_blocking(session, 1);
|
||||||
|
|
||||||
/* ... start it up. This will trade welcome banners, exchange keys,
|
/* ... start it up. This will trade welcome banners, exchange keys,
|
||||||
* and setup crypto, compression, and MAC layers
|
* and setup crypto, compression, and MAC layers
|
||||||
*/
|
*/
|
||||||
rc = libssh2_session_startup(session, sock);
|
rc = libssh2_session_startup(session, sock);
|
||||||
if(rc) {
|
if(rc) {
|
||||||
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
|
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* At this point we havn't yet authenticated. The first thing to do
|
/* At this point we havn't yet authenticated. The first thing to do
|
||||||
@ -126,67 +126,65 @@ int main(int argc, char *argv[])
|
|||||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
|
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
|
||||||
printf("Fingerprint: ");
|
printf("Fingerprint: ");
|
||||||
for(i = 0; i < 16; i++) {
|
for(i = 0; i < 16; i++) {
|
||||||
printf("%02X ", (unsigned char)fingerprint[i]);
|
printf("%02X ", (unsigned char)fingerprint[i]);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
if (auth_pw) {
|
if (auth_pw) {
|
||||||
/* We could authenticate via password */
|
/* We could authenticate via password */
|
||||||
if (libssh2_userauth_password(session, username, password)) {
|
if (libssh2_userauth_password(session, username, password)) {
|
||||||
printf("Authentication by password failed.\n");
|
printf("Authentication by password failed.\n");
|
||||||
goto shutdown;
|
goto shutdown;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* Or by public key */
|
/* Or by public key */
|
||||||
if (libssh2_userauth_publickey_fromfile(session, username,
|
if (libssh2_userauth_publickey_fromfile(session, username,
|
||||||
"/home/username/.ssh/id_rsa.pub",
|
"/home/username/.ssh/id_rsa.pub",
|
||||||
"/home/username/.ssh/id_rsa",
|
"/home/username/.ssh/id_rsa",
|
||||||
password)) {
|
password)) {
|
||||||
printf("\tAuthentication by public key failed\n");
|
printf("\tAuthentication by public key failed\n");
|
||||||
goto shutdown;
|
goto shutdown;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//libssh2_trace(session, 0xFFFF);
|
|
||||||
|
|
||||||
fprintf(stderr, "libssh2_sftp_init()!\n");
|
fprintf(stderr, "libssh2_sftp_init()!\n");
|
||||||
sftp_session = libssh2_sftp_init(session);
|
sftp_session = libssh2_sftp_init(session);
|
||||||
|
|
||||||
if (!sftp_session) {
|
if (!sftp_session) {
|
||||||
fprintf(stderr, "Unable to init SFTP session\n");
|
fprintf(stderr, "Unable to init SFTP session\n");
|
||||||
goto shutdown;
|
goto shutdown;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Since we have not set non-blocking, tell libssh2 we are blocking */
|
/* Since we have not set non-blocking, tell libssh2 we are blocking */
|
||||||
libssh2_session_set_blocking(session, 1);
|
libssh2_session_set_blocking(session, 1);
|
||||||
|
|
||||||
fprintf(stderr, "libssh2_sftp_open()!\n");
|
fprintf(stderr, "libssh2_sftp_open()!\n");
|
||||||
/* Request a file via SFTP */
|
/* Request a file via SFTP */
|
||||||
sftp_handle =
|
sftp_handle =
|
||||||
libssh2_sftp_open(sftp_session, sftppath,
|
libssh2_sftp_open(sftp_session, sftppath,
|
||||||
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
|
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
|
||||||
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
|
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
|
||||||
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
|
||||||
|
|
||||||
if (!sftp_handle) {
|
if (!sftp_handle) {
|
||||||
fprintf(stderr, "Unable to open file with SFTP\n");
|
fprintf(stderr, "Unable to open file with SFTP\n");
|
||||||
goto shutdown;
|
goto shutdown;
|
||||||
}
|
}
|
||||||
fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n");
|
fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n");
|
||||||
do {
|
do {
|
||||||
nread = fread(mem, 1, sizeof(mem), local);
|
nread = fread(mem, 1, sizeof(mem), local);
|
||||||
if (nread <= 0) {
|
if (nread <= 0) {
|
||||||
/* end of file */
|
/* end of file */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ptr = mem;
|
ptr = mem;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
/* write data in a loop until we block */
|
/* write data in a loop until we block */
|
||||||
rc = libssh2_sftp_write(sftp_handle, ptr, nread);
|
rc = libssh2_sftp_write(sftp_handle, ptr, nread);
|
||||||
ptr += rc;
|
ptr += rc;
|
||||||
nread -= nread;
|
nread -= nread;
|
||||||
} while (rc > 0);
|
} while (rc > 0);
|
||||||
} while (1);
|
} while (1);
|
||||||
|
|
||||||
fclose(local);
|
fclose(local);
|
||||||
@ -195,7 +193,8 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
shutdown:
|
shutdown:
|
||||||
|
|
||||||
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
|
libssh2_session_disconnect(session,
|
||||||
|
"Normal Shutdown, Thank you for playing");
|
||||||
libssh2_session_free(session);
|
libssh2_session_free(session);
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* $Id: ssh2.c,v 1.3 2007/04/26 23:59:15 gknauf Exp $
|
* $Id: ssh2.c,v 1.4 2007/07/14 20:54:47 bagder Exp $
|
||||||
*
|
*
|
||||||
* Sample showing how to do SSH2 connect.
|
* Sample showing how to do SSH2 connect.
|
||||||
*
|
*
|
||||||
@ -25,6 +25,9 @@
|
|||||||
# ifdef HAVE_UNISTD_H
|
# ifdef HAVE_UNISTD_H
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
# ifdef HAVE_ARPA_INET_H
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user