example.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // example.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "example.h"
  5. #include "libssh2_config.h"
  6. #include <libssh2.h>
  7. #include "libssh2_sftp.h"
  8. #include <winsock2.h>
  9. #include <sys/types.h>
  10. #ifdef HAVE_STDLIB_H
  11. #include <stdlib.h>
  12. #endif
  13. #include <fcntl.h>
  14. #include <errno.h>
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #pragma comment(lib,"libssh2.lib")
  18. #pragma comment(lib, "ws2_32.lib")
  19. #include "WinSsh2Proc.h"
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #endif
  23. // 唯一的应用程序对象
  24. CWinApp theApp;
  25. using namespace std;
  26. int download(std::string ip, unsigned short port, std::string username, std::string password, std::string sftppath, std::string localpath);
  27. static int waitsocket(int socket_fd, LIBSSH2_SESSION *session)
  28. {
  29. struct timeval timeout;
  30. int rc;
  31. fd_set fd;
  32. fd_set *writefd = NULL;
  33. fd_set *readfd = NULL;
  34. int dir;
  35. timeout.tv_sec = 10;
  36. timeout.tv_usec = 0;
  37. FD_ZERO(&fd);
  38. FD_SET(socket_fd, &fd);
  39. /* now make sure we wait in the correct direction */
  40. dir = libssh2_session_block_directions(session);
  41. if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
  42. readfd = &fd;
  43. if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
  44. writefd = &fd;
  45. rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout);
  46. return rc;
  47. }
  48. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  49. {
  50. int nRetCode = 0;
  51. #ifdef WIN32
  52. WSADATA wsadata;
  53. int err;
  54. err = WSAStartup(MAKEWORD(2, 0), &wsadata);
  55. if(err != 0) {
  56. fprintf(stderr, "WSAStartup failed with error: %d\n", err);
  57. return 1;
  58. }
  59. #endif
  60. // 初始化 MFC 并在失败时显示错误
  61. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  62. {
  63. // TODO: 更改错误代码以符合您的需要
  64. _tprintf(_T("错误: MFC 初始化失败\n"));
  65. nRetCode = 1;
  66. }
  67. else
  68. {
  69. // TODO: 在此处为应用程序的行为编写代码。
  70. /*
  71. download("10.201.251.254", 22,
  72. "wjf",
  73. "wjf2019",
  74. "/scbc_data/wjf/rt2851/Buildimg/V8-T841T01-LF1V001/Images/USB/V8-T841T01-LF1V001.img",
  75. "D:\\sat\\a.img");
  76. */
  77. CWinSsh2Proc winssh2;
  78. if ( winssh2.InitSocket() )
  79. {
  80. if ( winssh2.ssh2_connect("10.201.251.254", "wjf", "wjf2019") )
  81. {
  82. std::string str;
  83. // sha256sum
  84. winssh2.ssh2_execute_command("ls", str);
  85. winssh2.ssh2_execute_command("md5sum rt2851/Buildimg/V8-T841T01-LF1V001/Images/USB/V8-T841T01-LF1V001.img", str, 8000);
  86. winssh2.ssh2_execute_command("sha1sum rt2851/Buildimg/V8-T841T01-LF1V001/Images/USB/V8-T841T01-LF1V001.img", str, 10000);
  87. winssh2.ssh2_execute_command("sha256sum rt2851/Buildimg/V8-T841T01-LF1V001/Images/USB/V8-T841T01-LF1V001.img", str, 12000);
  88. str.size();
  89. //printf("MD5=%s\n", str.c_str());
  90. }
  91. winssh2.ssh2_sftp_download("rt2851/Buildimg/V8-T841T01-LF1V001/Images/USB/build.prop", "D:\\sat\\b.img");
  92. }
  93. }
  94. system("pause");
  95. return nRetCode;
  96. }
  97. int download(std::string ip, unsigned short port, std::string username, std::string password, std::string sftppath, std::string localpath)
  98. {
  99. unsigned long hostaddr;
  100. int sock, i, auth_pw = 0;
  101. struct sockaddr_in sin;
  102. const char *fingerprint;
  103. char *userauthlist;
  104. LIBSSH2_SESSION *session;
  105. int rc;
  106. LIBSSH2_SFTP *sftp_session;
  107. LIBSSH2_SFTP_HANDLE *sftp_handle;
  108. hostaddr = inet_addr(ip.c_str()); //hostaddr = htonl(0x7F000001);
  109. /*
  110. * The application code is responsible for creating the socket
  111. * and establishing the connection
  112. */
  113. sock = socket(AF_INET, SOCK_STREAM, 0);
  114. sin.sin_family = AF_INET;
  115. sin.sin_port = htons(port);
  116. sin.sin_addr.s_addr = hostaddr;
  117. if (connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) {
  118. return -1;
  119. }
  120. /* Create a session instance
  121. */
  122. session = libssh2_session_init();
  123. if (!session)
  124. return -1;
  125. /* Since we have set non-blocking, tell libssh2 we are blocking */
  126. libssh2_session_set_blocking(session, 1);
  127. /* ... start it up. This will trade welcome banners, exchange keys,
  128. * and setup crypto, compression, and MAC layers
  129. */
  130. rc = libssh2_session_handshake(session, sock);
  131. if (rc) {
  132. return -1;
  133. }
  134. /* At this point we havn't yet authenticated. The first thing to do
  135. * is check the hostkey's fingerprint against our known hosts Your app
  136. * may have it hard coded, may go to a file, may present it to the
  137. * user, that's your call
  138. */
  139. fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  140. for (int i = 0; i < 20; i++) {
  141. unsigned char c = fingerprint[i];
  142. int nT = c;
  143. }
  144. /* check what authentication methods are available */
  145. userauthlist = libssh2_userauth_list(session, username.c_str(), username.length());
  146. if (strstr(userauthlist, "password") == NULL)
  147. {
  148. goto shutdown;
  149. }
  150. /* We could authenticate via password */
  151. if (libssh2_userauth_password(session, username.c_str(), password.c_str())) {
  152. goto shutdown;
  153. }
  154. sftp_session = libssh2_sftp_init(session);
  155. if (!sftp_session) {
  156. goto shutdown;
  157. }
  158. /* Request a file via SFTP */
  159. sftp_handle = libssh2_sftp_open(sftp_session, sftppath.c_str(), LIBSSH2_FXF_READ, 0);
  160. if (!sftp_handle) {
  161. goto shutdown;
  162. }
  163. FILE *stream;
  164. if (fopen_s(&stream, localpath.c_str(), "wb") == 0)
  165. {
  166. do {
  167. char mem[1024];
  168. /* loop until we fail */
  169. rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem));
  170. if (rc > 0) {
  171. //从内存到磁盘
  172. fwrite(mem, 1, rc, stream);
  173. }
  174. else {
  175. break;
  176. }
  177. } while (1);
  178. fclose(stream);
  179. }
  180. else {
  181. }
  182. libssh2_sftp_close(sftp_handle);
  183. libssh2_sftp_shutdown(sftp_session);
  184. shutdown:
  185. libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
  186. libssh2_session_free(session);
  187. closesocket(sock);//INVALID_SOCKET
  188. return 0;
  189. }