http://blog.csdn.net/wyc6668205/article/details/9179197
Xmanager Enterprise 4 putty and other tools are all features of the use of LIBSSH2 to achieve. SFTP SSH2, etc.
Integration:
Download libssh2-1.6.0
http://www.libssh2.org/
D:\libssh2-1.6.0\libssh2-1.6.0\win32\libssh2.dsw
.. \win32,.. \include,d:\openssl-1.0.0c\openssl-1.0.0c\include,d:\openssl-1.0.0c\openssl-1.0.0c\inc32
(You need to compile SSL in advance.)
Build DLLs and Lib according to project type, respectively
In the Session.c file, an error occurs:
/* Windows? */
unsigned int option_value;
socklen_t Option_len = sizeof (option_value);
if (getsockopt
(SOCKFD, Sol_socket, So_error, (void *) &option_value, &option_len)) {
/* Assume blocking On Error */
return 1;
}
Add this sentence in front
typedef int SOCKLEN_T;
Application: Sftp Uploading Files
Produce files using the generated static libraries and d:\libssh2-1.6.0\libssh2-1.6.0\win32,d:\libssh2-1.6.0\libssh2-1.6.0\include.
/*
* Sample showing how to do SFTP transfers.
*
* The sample code has default values for host name, user name, password
* and path to copy, but can specify them on the command line like:
*
* "sftp 192.168.0.1 user Password/tmp/secrets-p|-i|-k"
*/
#include "Libssh2_config.h"
#include <libssh2.h>
#include <libssh2_sftp.h> #ifdef have_winsock2_h
#include <winsock2.h>
#ifdef Have_sys_socket_h
#include <sys/socket.h>
#endif
#ifdef Have_netinet_in_h
#include <netinet/in.h>
#endif
#ifdef Have_unistd_h
#include <unistd.h>
#endif
#ifdef Have_arpa_inet_h
#include <arpa/inet.h>
#endif
#ifdef Have_sys_time_h
#include <sys/time.h>
#endif #include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include <io.h>
#include <conio.h>
const char *keyfile1= "~/.ssh/id_rsa.pub"; const char *keyfile2= "~/.SSH/ID_RSA"; const char *username= "username"; const char *password= "password"; const char *sftppath= "/tmp/test";
static void Kbd_callback (const char *name, int name_len,
const char *instruction, int instruction_len, int num_prompts,
Const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
Libssh2_userauth_kbdint_response *responses,
void **abstract)
{
int i;
size_t N;
Char buf[1024];
(void) abstract;
printf ("Performing keyboard-interactive authentication.\n");
printf ("Authentication name: '");
Fwrite (name, 1, Name_len, stdout);
printf ("' \ n");
printf ("Authentication instruction: '");
fwrite (instruction, 1, Instruction_len, stdout);
printf ("' \ n");
printf ("Number of prompts:%d\n\n", num_prompts);
for (i = 0; i < num_prompts; i++) {
printf ("Prompt%d from server: '", I);
Fwrite (Prompts[i].text, 1, prompts[i].length, stdout);
printf ("' \ n");
printf ("Please type response:");
Fgets (buf, sizeof (BUF), stdin);
n = strlen (BUF);
while (> 0 && strchr ("\ r \ n", buf[n-1]))
n--;
Buf[n] = 0;
Responses[i].text = StrDup (BUF);
Responses[i].length = n;
printf ("Response%d from user is '", I);
Fwrite (Responses[i].text, 1, responses[i].length, stdout);
printf ("' \ n \ nthe");
}
printf ("Done. Sending keyboard-interactive responses to server now.\n ");
}
Ssh2.exe 10.11.0.33 Aspect aspect/recordings/audio/locstore1/epro/m3-userdef/
int main (int argc, char *argv[])
{
Char filename[100]={0};
int k=0;
unsigned long hostaddr;
int sock, I, AUTH_PW = 0;
struct sockaddr_in sin;
const char *fingerprint;
Char *userauthlist;
Libssh2_session *session;
int RC;
Libssh2_sftp *sftp_session;
Libssh2_sftp_handle *sftp_handle;
#ifdef WIN32
Wsadata Wsadata;
WSAStartup (Makeword (2,2), &wsadata);
#endif
if (argc > 1) {
HOSTADDR = inet_addr (argv[1]);
} else {
HOSTADDR = htonl (0x7f000001);
}
if (argc > 2) {
Username = argv[2];
}
if (argc > 3) {
Password = argv[3];
}
if (argc > 4) {
Sftppath = argv[4];
}
rc = libssh2_init (0);
if (rc! = 0) {
fprintf (stderr, "LIBSSH2 initialization failed (%d) \ n", RC);
return 1;
}
/*
* The application code is responsible for creating the socket
* and establishing the connection
*/
Sock = socket (af_inet, sock_stream, 0);
sin.sin_family = af_inet;
Sin.sin_port = htons (22);
SIN.SIN_ADDR.S_ADDR = hostaddr;
if (Connect (sock, struct sockaddr*) (&sin),
sizeof (struct sockaddr_in))! = 0) {
fprintf (stderr, "failed to connect!\n");
return-1;
}
/* Create A session instance
*/
Session = Libssh2_session_init ();
if (!session)
return-1;
/* Since We have the set non-blocking, tell Libssh2 we is blocking */
Libssh2_session_set_blocking (session, 1);
/* ... start it up. This would trade welcome banners, Exchange keys,
* and setup crypto, compression, and MAC layers
*/
rc = Libssh2_session_handshake (session, sock);
if (RC) {
fprintf (stderr, "Failure establishing SSH Session:%d\n", RC);
return-1;
}
/* At the this point we Havn ' t yet authenticated. The first thing to do
* is check the Hostkey ' s fingerprint against our known hosts Your app
* May has it hard coded, could go to a file, could present it to the
* User, that's your call
*/
Fingerprint = Libssh2_hostkey_hash (session, LIBSSH2_HOSTKEY_HASH_SHA1);
fprintf (stderr, "fingerprint:");
for (i = 0; i <; i++) {
fprintf (stderr, "%02x", (unsigned char) fingerprint[i]);
}
fprintf (stderr, "\ n");
/* Check what authentication methods is available */
Userauthlist = libssh2_userauth_list (session, username, strlen (username));
printf ("Authentication Methods:%s\n", userauthlist);
if (Strstr (userauthlist, "password") = NULL) {
AUTH_PW |= 1;
}
if (Strstr (userauthlist, "keyboard-interactive") = NULL) {
AUTH_PW |= 2;
}
if (Strstr (userauthlist, "publickey") = NULL) {
AUTH_PW |= 4;
}
/* If we got an 4. Argument we set this option if supported */
if (argc > 5) {
if ((AUTH_PW & 1) &&!strcasecmp (argv[5], "-P")) {
AUTH_PW = 1;
}
if ((AUTH_PW & 2) &&!strcasecmp (argv[5], "-i")) {
AUTH_PW = 2;
}
if ((AUTH_PW & 4) &&!strcasecmp (argv[5], "-K")) {
AUTH_PW = 4;
}
}
printf ("--------------------%d", AUTH_PW);
if (AUTH_PW & 1) {
printf ("---------ddd-----------%d", AUTH_PW);
/* We could authenticate via password */
if (Libssh2_userauth_password (session, username, password)) {
fprintf (stderr, "Authentication by Password failed.\n");
Goto shutdown;
}
} else if (AUTH_PW & 2) {
/* Or via keyboard-interactive */
if (Libssh2_userauth_keyboard_interactive (session, username, &kbd_callback)) {
printf ("\tauthentication by Keyboard-interactive failed!\n");
Goto shutdown;
} else {
printf ("\tauthentication by Keyboard-interactive succeeded.\n");
}
} else if (AUTH_PW & 4) {
/* Or by Public key */
if (Libssh2_userauth_publickey_fromfile (session, username, keyfile1, keyfile2, password)) {
printf ("\tauthentication by Public key failed!\n");
Goto shutdown;
} else {
printf ("\tauthentication by Public key succeeded.\n");
}
} else {
printf ("No Supported authentication Methods found!\n");
Goto shutdown;
}
fprintf (stderr, "Libssh2_sftp_init ()!\n");
Sftp_session = Libssh2_sftp_init (session);
if (!sftp_session) {
fprintf (stderr, "Unable to init SFTP session\n");
Goto shutdown;
}
fprintf (stderr, "Libssh2_sftp_open ()!\n");
for (; k<2;k++)
{
sprintf (filename, "%s/%d.txt", sftppath,k);
printf ("---%s", filename);
/* Request a file via SFTP */
Sftp_handle =
Libssh2_sftp_open (sftp_session, filename, libssh2_fxf_read, 0);
if (!sftp_handle) {
fprintf (stderr, "Unable to open file with SFTP:%ld\n",
Libssh2_sftp_last_error (sftp_session));
Goto shutdown;
}
fprintf (stderr, "Libssh2_sftp_open () is do, now receive data!\n");
do {
Char mem[1024]={0};
/* Loop until we fail */
fprintf (stderr, "Libssh2_sftp_read ()!\n");
rc = Libssh2_sftp_read (sftp_handle, mem, sizeof (MEM));
Libssh2_sftp_write (Sftp_handle,)
if (rc > 0) {
Write (1, mem, RC);
} else {
Break
}
} while (1);
}
Libssh2_sftp_close (Sftp_handle);
Libssh2_sftp_shutdown (sftp_session);
printf ("-----------------------down");
Shutdown
Libssh2_session_disconnect (Session, "Normal Shutdown, Thank for playing");
Libssh2_session_free (session);
#ifdef WIN32
Closesocket (sock);
#else
Close (sock);
#endif fprintf (stderr, "all done\n");
Libssh2_exit ();
Getch ();
return 0;
}
Integration and application of LIBSSH2