simple.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* Copyright (C) 2007 The Written Word, Inc.
  2. * Copyright (C) 2008, 2010 Simon Josefsson
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms,
  6. * with or without modification, are permitted provided
  7. * that the following conditions are met:
  8. *
  9. * Redistributions of source code must retain the above
  10. * copyright notice, this list of conditions and the
  11. * following disclaimer.
  12. *
  13. * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials
  16. * provided with the distribution.
  17. *
  18. * Neither the name of the copyright holder nor the names
  19. * of any other contributors may be used to endorse or
  20. * promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  24. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  25. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  26. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  33. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  36. * OF SUCH DAMAGE.
  37. */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include "libssh2.h"
  41. #pragma comment(lib, "ws2_32.lib")
  42. static int waitsocket(int socket_fd, LIBSSH2_SESSION *session)
  43. {
  44. struct timeval timeout;
  45. int rc;
  46. fd_set fd;
  47. fd_set *writefd = NULL;
  48. fd_set *readfd = NULL;
  49. int dir;
  50. timeout.tv_sec = 10;
  51. timeout.tv_usec = 0;
  52. FD_ZERO(&fd);
  53. FD_SET(socket_fd, &fd);
  54. /* now make sure we wait in the correct direction */
  55. dir = libssh2_session_block_directions(session);
  56. if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
  57. readfd = &fd;
  58. if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
  59. writefd = &fd;
  60. rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout);
  61. return rc;
  62. }
  63. int main(int argc, char *argv[])
  64. {
  65. const char *hostname = "127.0.0.1";
  66. const char *commandline = "uptime";
  67. const char *username = "user";
  68. const char *password = "password";
  69. unsigned long hostaddr;
  70. int sock;
  71. struct sockaddr_in sin;
  72. const char *fingerprint;
  73. LIBSSH2_SESSION *session;
  74. LIBSSH2_CHANNEL *channel;
  75. int rc;
  76. int exitcode;
  77. char *exitsignal = (char *)"none";
  78. int bytecount = 0;
  79. size_t len;
  80. LIBSSH2_KNOWNHOSTS *nh;
  81. int type;
  82. char buffer2[0x4000];
  83. #ifdef WIN32
  84. WSADATA wsadata;
  85. int err;
  86. err = WSAStartup(MAKEWORD(2, 0), &wsadata);
  87. if(err != 0) {
  88. fprintf(stderr, "WSAStartup failed with error: %d\n", err);
  89. return 1;
  90. }
  91. #endif
  92. #if 0
  93. if(argc > 1)
  94. /* must be ip address only */
  95. hostname = argv[1];
  96. if(argc > 2) {
  97. username = argv[2];
  98. }
  99. if(argc > 3) {
  100. password = argv[3];
  101. }
  102. if(argc > 4) {
  103. commandline = argv[4];
  104. }
  105. #else
  106. hostname = "10.201.251.254";
  107. username = "wjf";
  108. password = "wjf2019";
  109. #endif
  110. rc = libssh2_init(0);
  111. if(rc != 0) {
  112. fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
  113. return 1;
  114. }
  115. hostaddr = inet_addr(hostname);
  116. /* Ultra basic "connect to port 22 on localhost"
  117. * Your code is responsible for creating the socket establishing the
  118. * connection
  119. */
  120. sock = socket(AF_INET, SOCK_STREAM, 0);
  121. sin.sin_family = AF_INET;
  122. sin.sin_port = htons(22);
  123. sin.sin_addr.s_addr = hostaddr;
  124. if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) {
  125. fprintf(stderr, "failed to connect!\n");
  126. return -1;
  127. }
  128. #if 1
  129. /* Create a session instance */
  130. session = libssh2_session_init();
  131. if(!session)
  132. return -1;
  133. /* tell libssh2 we want it all done non-blocking */
  134. libssh2_session_set_blocking(session, 0);
  135. /* ... start it up. This will trade welcome banners, exchange keys,
  136. * and setup crypto, compression, and MAC layers
  137. */
  138. while((rc = libssh2_session_handshake(session, sock)) == LIBSSH2_ERROR_EAGAIN);
  139. if(rc) {
  140. fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
  141. return -1;
  142. }
  143. nh = libssh2_knownhost_init(session);
  144. if(!nh) {
  145. /* eeek, do cleanup here */
  146. return 2;
  147. }
  148. #if 0
  149. /* read all hosts from here */
  150. libssh2_knownhost_readfile(nh, "known_hosts", LIBSSH2_KNOWNHOST_FILE_OPENSSH);
  151. /* store all known hosts to here */
  152. libssh2_knownhost_writefile(nh, "dumpfile", LIBSSH2_KNOWNHOST_FILE_OPENSSH);
  153. fingerprint = libssh2_session_hostkey(session, &len, &type);
  154. if(fingerprint) {
  155. struct libssh2_knownhost *host;
  156. #if LIBSSH2_VERSION_NUM >= 0x010206
  157. /* introduced in 1.2.6 */
  158. int check = libssh2_knownhost_checkp(nh, hostname, 22, fingerprint, len, LIBSSH2_KNOWNHOST_TYPE_PLAIN| LIBSSH2_KNOWNHOST_KEYENC_RAW, &host);
  159. #else
  160. /* 1.2.5 or older */
  161. int check = libssh2_knownhost_check(nh, hostname, fingerprint, len,LIBSSH2_KNOWNHOST_TYPE_PLAIN|LIBSSH2_KNOWNHOST_KEYENC_RAW,&host);
  162. #endif
  163. fprintf(stderr, "Host check: %d, key: %s\n", check, (check <= LIBSSH2_KNOWNHOST_CHECK_MISMATCH)?host->key:"<none>");
  164. /*****
  165. * At this point, we could verify that 'check' tells us the key is
  166. * fine or bail out.
  167. *****/
  168. }
  169. else {
  170. /* eeek, do cleanup here */
  171. return 3;
  172. }
  173. libssh2_knownhost_free(nh);
  174. #endif
  175. if(strlen(password) != 0) {
  176. /* We could authenticate via password */
  177. while((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN);
  178. if(rc) {
  179. fprintf(stderr, "Authentication by password failed.\n");
  180. goto shutdown;
  181. }
  182. }
  183. else {
  184. /* Or by public key */
  185. while((rc = libssh2_userauth_publickey_fromfile(session, username,
  186. "/home/user/"
  187. ".ssh/id_rsa.pub",
  188. "/home/user/"
  189. ".ssh/id_rsa",
  190. password)) ==
  191. LIBSSH2_ERROR_EAGAIN);
  192. if(rc) {
  193. fprintf(stderr, "\tAuthentication by public key failed\n");
  194. goto shutdown;
  195. }
  196. }
  197. #if 0
  198. libssh2_trace(session, ~0);
  199. #endif
  200. /* Exec non-blocking on the remove host */
  201. while((channel = libssh2_channel_open_session(session)) == NULL && libssh2_session_last_error(session, NULL, NULL, 0) == LIBSSH2_ERROR_EAGAIN) {
  202. waitsocket(sock, session);
  203. }
  204. if(channel == NULL) {
  205. fprintf(stderr, "Error\n");
  206. exit(1);
  207. }
  208. // ½¨Á¢Ò»¸öÖÕ¶ËÁ¬½Ó(pty)
  209. if ( libssh2_channel_request_pty(channel, "VT100") != 0 )
  210. {
  211. return -1;
  212. }
  213. commandline = "ls rt2851";
  214. while((rc = libssh2_channel_exec(channel, commandline)) == LIBSSH2_ERROR_EAGAIN) {
  215. waitsocket(sock, session);
  216. }
  217. memset(buffer2, 0, sizeof(buffer2));
  218. rc = libssh2_channel_read(channel, buffer2, sizeof(buffer2) );
  219. //commandline = "ls";
  220. commandline = "ls rt2851/Buildimg/V8-T841T01-LF1V001/Images/USB";
  221. while((rc = libssh2_channel_exec(channel, commandline)) == LIBSSH2_ERROR_EAGAIN) {
  222. waitsocket(sock, session);
  223. }
  224. Sleep(5000);
  225. memset(buffer2, 0, sizeof(buffer2));
  226. rc = libssh2_channel_read(channel, buffer2, sizeof(buffer2) );
  227. if(rc != 0) {
  228. fprintf(stderr, "Error\n");
  229. exit(1);
  230. }
  231. for(;;) {
  232. /* loop until we block */
  233. int rc;
  234. do {
  235. char buffer[0x4000];
  236. rc = libssh2_channel_read(channel, buffer, sizeof(buffer) );
  237. if(rc > 0) {
  238. int i;
  239. bytecount += rc;
  240. fprintf(stderr, "We read:\n");
  241. for(i = 0; i < rc; ++i)
  242. fputc(buffer[i], stderr);
  243. fprintf(stderr, "\n");
  244. }
  245. else {
  246. if(rc != LIBSSH2_ERROR_EAGAIN)
  247. /* no need to output this for the EAGAIN case */
  248. fprintf(stderr, "libssh2_channel_read returned %d\n", rc);
  249. }
  250. }
  251. while(rc > 0);
  252. /* this is due to blocking that would occur otherwise so we loop on
  253. this condition */
  254. if(rc == LIBSSH2_ERROR_EAGAIN) {
  255. waitsocket(sock, session);
  256. }
  257. else
  258. break;
  259. }
  260. exitcode = 127;
  261. while((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN)
  262. waitsocket(sock, session);
  263. if(rc == 0) {
  264. exitcode = libssh2_channel_get_exit_status(channel);
  265. libssh2_channel_get_exit_signal(channel, &exitsignal,NULL, NULL, NULL, NULL, NULL);
  266. }
  267. if(exitsignal)
  268. fprintf(stderr, "\nGot signal: %s\n", exitsignal);
  269. else
  270. fprintf(stderr, "\nEXIT: %d bytecount: %d\n", exitcode, bytecount);
  271. libssh2_channel_free(channel);
  272. channel = NULL;
  273. shutdown:
  274. libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
  275. libssh2_session_free(session);
  276. #ifdef WIN32
  277. closesocket(sock);
  278. #else
  279. close(sock);
  280. #endif
  281. fprintf(stderr, "all done\n");
  282. #endif
  283. libssh2_exit();
  284. system("pause");
  285. return 0;
  286. }