#endif#i_libssh2 编译">
当前位置:   article > 正文

libssh2编译安装及例程_libssh2 编译

libssh2 编译

一、下载源代码:

官网:https://www.libssh2.org/

githubhttps://github.com/libssh2/libssh2

二、编译安装:

1.安装依赖库(选一个即可)

  • OpenSSL
  • Libgcrypt
  • WinCNG
  • mbedTLS

2.安装libssh2,生成静态库和动态库

三、例程

  1. #include "libssh2.h"
  2. #ifdef HAVE_WINSOCK2_H
  3. # include <winsock2.h>
  4. #endif
  5. #ifdef HAVE_SYS_SOCKET_H
  6. # include <sys/socket.h>
  7. #endif
  8. #ifdef HAVE_NETINET_IN_H
  9. # include <netinet/in.h>
  10. #endif
  11. # ifdef HAVE_UNISTD_H
  12. #include <unistd.h>
  13. #endif
  14. #ifdef HAVE_ARPA_INET_H
  15. # include <arpa/inet.h>
  16. #endif
  17. #ifdef HAVE_SYS_TIME_H
  18. # include <sys/time.h>
  19. #endif
  20. #include <sys/types.h>
  21. #include <fcntl.h>
  22. #include <errno.h>
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. int main()
  26. {
  27. unsigned long hostaddr;
  28. int sock, i, auth_pw = 1;
  29. struct sockaddr_in sin;
  30. const char *fingerprint;
  31. LIBSSH2_SESSION *session;
  32. LIBSSH2_CHANNEL *channel;
  33. const char *username = "用户名";
  34. const char *password = "密码";
  35. int rc;
  36. /init_slot()
  37. #ifdef WIN32
  38. WSADATA wsadata;
  39. int err;
  40. err = WSAStartup(MAKEWORD(2, 0), &wsadata);
  41. if(err != 0) {
  42. fprintf(stderr, "WSAStartup failed with error: %d\n", err);
  43. return 1;
  44. }
  45. #endif
  46. rc = libssh2_init(0);
  47. if(rc != 0) {
  48. fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
  49. return 1;
  50. }
  51. hostaddr = inet_addr("IP地址");
  52. sin.sin_family = AF_INET;
  53. sin.sin_port = htons(22);
  54. sin.sin_addr.s_addr = hostaddr;
  55. /* Ultra basic "connect to port 22 on localhost"
  56. * Your code is responsible for creating the socket establishing the
  57. * connection
  58. */
  59. sock = socket(AF_INET, SOCK_STREAM, 0);
  60. if(sock == -1)
  61. {
  62. qDebug() << "failed to create socket!";
  63. return -1;
  64. }
  65. if(::connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) {
  66. fprintf(stderr, "failed to connect!\n");
  67. return -1;
  68. }
  69. fprintf(stderr, "sock connected!\n");
  70. /* Create a session instance
  71. */
  72. session = libssh2_session_init();
  73. if(!session)
  74. return -1;
  75. fprintf(stderr, "session inited!\n");
  76. /* ... start it up. This will trade welcome banners, exchange keys,
  77. * and setup crypto, compression, and MAC layers
  78. */
  79. rc = libssh2_session_handshake(session, sock);
  80. if(rc != 0) {
  81. qDebug() << rc;
  82. fprintf(stderr, " %d\n", rc);
  83. return -1;
  84. }
  85. fprintf(stderr, "session handshake!\n");
  86. /* At this point we havn't yet authenticated. The first thing to do
  87. * is check the hostkey's fingerprint against our known hosts Your app
  88. * may have it hard coded, may go to a file, may present it to the
  89. * user, that's your call
  90. */
  91. fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
  92. fprintf(stderr, "Fingerprint: ");
  93. for(i = 0; i < 20; i++) {
  94. fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
  95. }
  96. fprintf(stderr, "\n");
  97. /* We could authenticate via password */
  98. if(libssh2_userauth_password(session, username, password)) {
  99. fprintf(stderr, "Authentication by password failed.\n");
  100. goto shutdown;
  101. }
  102. qDebug() << "Authentication by password successful";
  103. /
  104. /
  105. /* Request a file via SCP */
  106. const char* loclfile = "源文件";
  107. const char *scppath = "目标文件";
  108. FILE *local = fopen(loclfile, "rb");
  109. if(!local)
  110. {
  111. qDebug() << "Can't open local file: " << loclfile;
  112. return -1;
  113. }
  114. struct stat fileinfo;
  115. stat(loclfile, &fileinfo);
  116. channel = libssh2_scp_send(session, scppath, fileinfo.st_mode & 0777, (unsigned long)fileinfo.st_size);
  117. if(!channel)
  118. {
  119. char *errmsg;
  120. int errlen;
  121. int err = libssh2_session_last_error(session, &errmsg, &errlen, 0);
  122. qDebug() << "Create channel failed. " << err;
  123. goto shutdown;
  124. }
  125. qDebug() << "SCP session waiting to send file";
  126. do
  127. {
  128. char mem[1024];
  129. size_t nread = fread(mem, 1, sizeof(mem), local);
  130. if(nread <= 0) /* end of file */
  131. {
  132. break;
  133. }
  134. char *ptr = mem;
  135. do
  136. {
  137. /* write the same data over and over, until error or completion */
  138. int rc = libssh2_channel_write(channel, ptr, nread);
  139. if(rc < 0) // error
  140. {
  141. qDebug() << "ERROR " << rc;
  142. break;
  143. }
  144. else /* rc indicates how many bytes were written this time */
  145. {
  146. ptr += rc;
  147. nread -= rc;
  148. }
  149. } while(nread);
  150. } while(1);
  151. libssh2_channel_free(channel);
  152. channel = NULL;
  153. shutdown:
  154. libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
  155. libssh2_session_free(session);
  156. #ifdef WIN32
  157. closesocket(sock);
  158. #else
  159. close(sock);
  160. #endif
  161. fprintf(stderr, "all done\n");
  162. libssh2_exit();
  163. return 0;
  164. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/894697
推荐阅读
相关标签
  

闽ICP备14008679号