Chinaunix首页 | 论坛 | 博客
  • 博客访问: 133098
  • 博文数量: 94
  • 博客积分: 1572
  • 博客等级: 上尉
  • 技术积分: 925
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-04 00:03
文章分类

全部博文(94)

文章存档

2011年(94)

我的朋友

分类: C/C++

2011-04-14 23:43:15

  1. /*
  2.  *    可以使用密码参数的 SSH 客户端程序 v1.0
  3.  * Copyright (c) 2011 Niu.Chenguang <chrisniu1984@gmail.com>
  4.  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5.  *
  6.  * [makefile]
  7.  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8.  * CFLAGS=-Wall -c -g
  9.  * LDFLAGS=-Wall -lssh2
  10.  * OBJS=jnSSH2.o
  11.  *
  12.  * jnSSH2: $(OBJS)
  13.  *     gcc $(LDFLAGS) $< -o $@
  14.  *
  15.  * %.o: %.c
  16.  *     gcc $(CFLAGS) -o $@ $^
  17.  *
  18.  * install: jnSSH2
  19.  *     sudo cp ./jnSSH2 /usr/bin/
  20.  *
  21.  * clean:
  22.  *     rm -f jnSSH2
  23.  *     rm -f $(OBJS)
  24.  *
  25.  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  26.  * [README]
  27.  * 一、ubuntu 编译并使用
  28.  * 1、安装 libssh2-dev 库
  29.  *     sudo aptitued install libssh2-1-dev
  30.  * 2、编译
  31.  *     make
  32.  * 3、测试
  33.  *     ./jnSSH2 ip port user passwd
  34.  * 4、安装
  35.  *     make install
  36.  */

  37. #include <string.h>
  38. #include <sys/ioctl.h>
  39. #include <netinet/in.h>
  40. #include <arpa/inet.h>
  41. #include <unistd.h>
  42. #include <sys/types.h>
  43. #include <libgen.h>
  44. #include <fcntl.h>
  45. #include <errno.h>
  46. #include <stdio.h>
  47. #include <ctype.h>
  48. #include <stdlib.h>
  49. #include <termios.h>
  50.  
  51. #include <libssh2.h>

  52. #define COPYRIGHT "jnSSH2 v1.0\nCopyright (C) 2011 Niu.Chenguang \n\n"
  53.  
  54. struct termios _saved_tio;
  55. int tio_saved = 0;
  56.  
  57. static int _raw_mode(void)
  58. {
  59.     int rc;
  60.     struct termios tio;

  61.     rc = tcgetattr(fileno(stdin), &tio);
  62.     if (rc != -1) {
  63.         _saved_tio = tio;
  64.         tio_saved = 1;
  65.         cfmakeraw(&tio);
  66.         rc = tcsetattr(fileno(stdin), TCSADRAIN, &tio);
  67.     }

  68.     return rc;
  69. }
  70.  
  71. static int _normal_mode(void)
  72. {
  73.     if (tio_saved)
  74.         return tcsetattr(fileno(stdin), TCSADRAIN, &_saved_tio);

  75.     return 0;
  76. }
  77.  
  78. int main (int argc, char *argv[])
  79. {
  80.     int sock = 0;
  81.     unsigned long hostaddr = 0;
  82.     short port = 22;
  83.     char *username = NULL;
  84.     char *password = NULL;
  85.     struct sockaddr_in sin;
  86.     LIBSSH2_SESSION *session;
  87.     LIBSSH2_CHANNEL *channel;
  88.     int nfds = 1;
  89.     char buf;
  90.     LIBSSH2_POLLFD *fds = NULL;
  91.  
  92.     /* Struct winsize for term size */
  93.     struct winsize w_size;
  94.     struct winsize w_size_bck;

  95.     /* For select on stdin */
  96.     fd_set set;
  97.     struct timeval timeval_out;
  98.     timeval_out.tv_sec = 0;
  99.     timeval_out.tv_usec = 10;

  100.     printf(COPYRIGHT);

  101.     if (argc > 4) {
  102.         hostaddr = inet_addr(argv[1]);
  103.         port = htons(atoi(argv[2]));
  104.         username = argv[3];
  105.         password = argv[4];
  106.     }
  107.     else {
  108.         fprintf(stderr, "Usage: %s ip port user password\n", basename(argv[0]));
  109.         return -1;
  110.     }

  111.     if (libssh2_init (0) != 0) {
  112.         fprintf (stderr, "libssh2 initialization failed\n");
  113.         return -1;
  114.     }

  115.     sock = socket (AF_INET, SOCK_STREAM, 0);
  116.     sin.sin_family = AF_INET;
  117.     sin.sin_port = port;
  118.     sin.sin_addr.s_addr = hostaddr;
  119.     if (connect(sock, (struct sockaddr *) &sin, sizeof(struct sockaddr_in)) != 0) {
  120.         fprintf (stderr, "Failed to established connection!\n");
  121.         return -1;
  122.     }

  123.     /* Open a session */
  124.     session = libssh2_session_init();
  125.     if (libssh2_session_startup (session, sock) != 0) {
  126.         fprintf(stderr, "Failed Start the SSH session\n");
  127.         return -1;
  128.     }
  129.     
  130.     /* Authenticate via password */
  131.     if (libssh2_userauth_password(session, username, password) != 0) {
  132.         fprintf(stderr, "Failed to authenticate\n");
  133.         close(sock);
  134.         goto ERROR;
  135.     }
  136.     
  137.     /* Open a channel */
  138.     channel = libssh2_channel_open_session(session);
  139.     
  140.     if ( channel == NULL ) {
  141.         fprintf(stderr, "Failed to open a new channel\n");
  142.         close(sock);
  143.         goto ERROR;
  144.     }
  145.     
  146.     /* Request a PTY */
  147.     if (libssh2_channel_request_pty( channel, "xterm") != 0) {
  148.         fprintf(stderr, "Failed to request a pty\n");
  149.         close(sock);
  150.         goto ERROR;
  151.     }
  152.     
  153.     /* Request a shell */
  154.     if (libssh2_channel_shell(channel) != 0) {
  155.         fprintf(stderr, "Failed to open a shell\n");
  156.         close(sock);
  157.         goto ERROR;
  158.     }
  159.     
  160.     if (_raw_mode() != 0) {
  161.         fprintf(stderr, "Failed to entered in raw mode\n");
  162.         close(sock);
  163.         goto ERROR;
  164.     }
  165.     
  166.     while (1) {
  167.         FD_ZERO(&set);
  168.         FD_SET(fileno(stdin),&set);
  169.     
  170.         ioctl(fileno(stdin), TIOCGWINSZ, &w_size);
  171.         if ((w_size.ws_row != w_size_bck.ws_row) ||
  172.             (w_size.ws_col != w_size_bck.ws_col)) {
  173.             w_size_bck = w_size;
  174.             libssh2_channel_request_pty_size(channel, w_size.ws_col, w_size.ws_row);
  175.         }
  176.     
  177.         if ((fds = malloc (sizeof (LIBSSH2_POLLFD))) == NULL)
  178.             break;
  179.     
  180.         fds[0].type = LIBSSH2_POLLFD_CHANNEL;
  181.         fds[0].fd.channel = channel;
  182.         fds[0].events = LIBSSH2_POLLFD_POLLIN;
  183.         fds[0].revents = LIBSSH2_POLLFD_POLLIN;
  184.     
  185.         if (libssh2_poll(fds, nfds, 0) >0) {
  186.             libssh2_channel_read(channel, &buf, 1);
  187.             fprintf(stdout, "%c", buf);
  188.             fflush(stdout);
  189.         }
  190.     
  191.         if (select(fileno(stdin)+1,&set,NULL,NULL,&timeval_out) > 0)
  192.             if (read(fileno(stdin), &buf, 1) > 0)
  193.                 libssh2_channel_write(channel, &buf, 1);
  194.     
  195.         free (fds);
  196.     
  197.         if (libssh2_channel_eof(channel) == 1)
  198.          break;
  199.     }
  200.     
  201.     if (channel) {
  202.         libssh2_channel_free (channel);
  203.         channel = NULL;
  204.     }
  205.     
  206.     _normal_mode();
  207.     
  208.     libssh2_exit();

  209.     return 0;
  210. ERROR:
  211.     libssh2_session_disconnect(session, "Session Shutdown, Thank you for playing");
  212.     libssh2_session_free(session);
  213.     return -1;
  214. }
阅读(6927) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~