Advertisement
Guest User

tlacuache

a guest
Jan 6th, 2010
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.23 KB | None | 0 0
  1. /*
  2.  * $ gcc -g -O0 -D HAVE_NETINET_IN_H -D HAVE_SYS_SOCKET_H -D HAVE_UNISTD_H -D HAVE_ARPA_INET_H -lssh2 -o ssh2_exec ssh2_exec.c
  3.  * $ ./ssh2_exec 127.0.0.1 user password command keyfile.pub keyfile
  4.  *
  5.  */
  6.  
  7. #include <libssh2.h>
  8.  
  9. #ifdef HAVE_WINSOCK2_H
  10. #include <winsock2.h>
  11. #endif
  12. #ifdef HAVE_SYS_SOCKET_H
  13. #include <sys/socket.h>
  14. #endif
  15. #ifdef HAVE_NETINET_IN_H
  16. #include <netinet/in.h>
  17. #endif
  18. #ifdef HAVE_SYS_SELECT_H
  19. #include <sys/select.h>
  20. #endif
  21. #ifdef HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #endif
  24. #ifdef HAVE_ARPA_INET_H
  25. #include <arpa/inet.h>
  26. #endif
  27.  
  28. #include <sys/time.h>
  29. #include <sys/types.h>
  30. #include <stdlib.h>
  31. #include <fcntl.h>
  32. #include <errno.h>
  33. #include <stdio.h>
  34. #include <ctype.h>
  35.  
  36. static int
  37. waitsocket (
  38.   int socket_fd,
  39.   LIBSSH2_SESSION * session)
  40. {
  41.   struct timeval           timeout;
  42.   int                      rc;
  43.   fd_set                   fd;
  44.   fd_set                  *writefd = NULL;
  45.   fd_set                  *readfd = NULL;
  46.   int                      dir;
  47.  
  48.   timeout.tv_sec = 3;
  49.   timeout.tv_usec = 0;
  50.  
  51.   FD_ZERO (&fd);
  52.   FD_SET (socket_fd, &fd);
  53.  
  54.   /* now make sure we wait in the correct direction */
  55.   dir = libssh2_session_block_directions (session);
  56.  
  57.   if (dir & LIBSSH2_SESSION_BLOCK_INBOUND)
  58.     readfd = &fd;
  59.  
  60.   if (dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
  61.     writefd = &fd;
  62.  
  63.   rc = select (socket_fd + 1, readfd, writefd, NULL, &timeout);
  64.  
  65.   return rc;
  66. }
  67.  
  68. int
  69. main (
  70.   int argc,
  71.   char *argv[])
  72. {
  73.   const char              *hostaddr_str = "127.0.0.1";
  74.   const char              *commandline = "uptime";
  75.   const char              *username = "user";
  76.   const char              *password = "password";
  77.   const char              *keyfile1 = "";
  78.   const char              *keyfile2 = "";
  79.   unsigned long            hostaddr;
  80.   int                      sock;
  81.   struct sockaddr_in       sin;
  82.   const char              *fingerprint;
  83.   LIBSSH2_SESSION         *session;
  84.   LIBSSH2_CHANNEL         *channel;
  85.   int                      rc;
  86.   int                      exitcode;
  87.   int                      bytecount = 0;
  88.   size_t                   len;
  89.   int                      type;
  90.   int                      i;
  91.   char *userauthlist;
  92.   char auth_pw = 0;
  93.  
  94. #ifdef WIN32
  95.   WSADATA                  wsadata;
  96.   WSAStartup (MAKEWORD (2, 0), &wsadata);
  97. #endif
  98.  
  99.   if (argc > 1) {
  100.     hostaddr_str = argv[1];
  101.   }
  102.   if (argc > 2) {
  103.     username = argv[2];
  104.   }
  105.   if (argc > 3) {
  106.     password = argv[3];
  107.   }
  108.   if (argc > 4) {
  109.     commandline = argv[4];
  110.   }
  111.   if (argc > 5) {
  112.     keyfile1 = argv[5];
  113.   }
  114.   if (argc > 6) {
  115.     keyfile2 = argv[6];
  116.   }
  117.  
  118.   hostaddr = inet_addr (hostaddr_str);
  119.  
  120.   /* establish socket connection */
  121.   sock = socket (AF_INET, SOCK_STREAM, 0);
  122.   sin.sin_family = AF_INET;
  123.   sin.sin_port = htons (22);
  124.   sin.sin_addr.s_addr = hostaddr;
  125.   if (connect (sock, (struct sockaddr *)(&sin),
  126.                sizeof (struct sockaddr_in)) != 0) {
  127.     fprintf (stderr, "failed to connect!\n");
  128.     return -1;
  129.   }
  130.  
  131.   /* create session handle */
  132.   session = libssh2_session_init ();
  133.   if (!session)
  134.     return -1;
  135.  
  136.   /* do everything non-blocking */
  137.   libssh2_session_set_blocking (session, 0);
  138.  
  139.   /* statup session */
  140.   while ((rc = libssh2_session_startup (session, sock)) ==
  141.          LIBSSH2_ERROR_EAGAIN);
  142.   if (rc) {
  143.     fprintf (stderr, "Failure establishing SSH session: %d\n", rc);
  144.     return -1;
  145.   }
  146.  
  147.   if ((strlen (keyfile1) != 0) && (strlen (keyfile2) != 0)) {
  148.     while ((rc = libssh2_userauth_publickey_fromfile (session, username,
  149.                                                       keyfile1,
  150.                                                       keyfile2,
  151.                                                       password)) ==
  152.            LIBSSH2_ERROR_EAGAIN);
  153.     if (rc) {
  154.       fprintf (stderr, "Authentication by public key (%s with %s and %s) failed\n", username, keyfile1, keyfile2);
  155.       goto shutdown;
  156.     } else {
  157.       fprintf (stderr, "Authentication by public key succeeded\n");
  158.     }
  159.   } else if (strlen (password) != 0) {
  160.     /* authenticate by password */
  161.     while ((rc = libssh2_userauth_password (session, username, password)) ==
  162.            LIBSSH2_ERROR_EAGAIN) ;
  163.     if (rc) {
  164.       fprintf (stderr, "Authentication by password failed.\n");
  165.       goto shutdown;
  166.     } else {
  167.       fprintf (stderr, "Authentication by password succeeded\n");
  168.     }
  169.   }
  170.   /* create channel and execute command */
  171.   while ((channel = libssh2_channel_open_session (session)) == NULL &&
  172.          libssh2_session_last_error (session, NULL, NULL, 0) ==
  173.          LIBSSH2_ERROR_EAGAIN) {
  174.     waitsocket (sock, session);
  175.   }
  176.   if (channel == NULL) {
  177.     fprintf (stderr, "Error in libssh2_channel_open_session\n");
  178.     exit (1);
  179.   }
  180.   while ((rc = libssh2_channel_exec (channel, commandline)) ==
  181.          LIBSSH2_ERROR_EAGAIN) {
  182.     waitsocket (sock, session);
  183.   }
  184.   if (rc != 0) {
  185.     fprintf (stderr, "Error in libssh2_channel_exec\n");
  186.     exit (1);
  187.   }
  188.   for (;;) {
  189.     /* loop until we block */
  190.     int rc;
  191.     do {
  192.       char buffer[0x4000];
  193.       rc = libssh2_channel_read (channel, buffer, sizeof (buffer));
  194.       if (rc > 0) {
  195.         int i;
  196.         bytecount += rc;
  197.         fprintf (stderr, "We read:\n");
  198.         for (i = 0; i < rc; ++i)
  199.           fputc (buffer[i], stderr);
  200.         fprintf (stderr, "\n");
  201.       } else {
  202.         fprintf (stderr, "libssh2_channel_read returned %d\n", rc);
  203.       }
  204.     }
  205.     while (rc > 0);
  206.     /* wait on LIBSSH2_ERROR_EAGAIN, else we got 0 bytes or an error so break */
  207.     if (rc == LIBSSH2_ERROR_EAGAIN) {
  208.       waitsocket (sock, session);
  209.     } else
  210.       break;
  211.   }
  212.  
  213. exitcode = 127;
  214.   while ((rc = libssh2_channel_close (channel)) == LIBSSH2_ERROR_EAGAIN) {
  215.     waitsocket (sock, session);
  216.   }
  217.  
  218.   if (rc == 0) {
  219.     exitcode = libssh2_channel_get_exit_status (channel);
  220.   }
  221.   printf ("\nEXIT: %d bytecount: %d\n", exitcode, bytecount);
  222.  
  223.   libssh2_channel_free (channel);
  224.   channel = NULL;
  225.  
  226. shutdown:
  227.   libssh2_session_disconnect (session,
  228.                               "Normal Shutdown, Thank you for playing");
  229.   libssh2_session_free (session);
  230.  
  231. #ifdef WIN32
  232.   closesocket (sock);
  233. #else
  234.   close (sock);
  235. #endif
  236.   fprintf (stderr, "all done\n");
  237.   return 0;
  238. }
  239.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement