基于libssh库实现SSH的Linux客户端程序
来源:互联网 发布:php 抓取 天猫 商品 编辑:程序博客网 时间:2024/05/30 04:13
代码如下:
#include <string.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <sys/types.h> #include <libgen.h> #include <fcntl.h> #include <errno.h> #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <termios.h> #include <libssh2.h> #define COPYRIGHT "SSH2 v1.0\nCopyright (C) 2014 \n\n" struct termios _saved_tio; int tio_saved = 0; static int _raw_mode(void) { int rc; struct termios tio; rc = tcgetattr(fileno(stdin), &tio); if (rc != -1) { _saved_tio = tio; tio_saved = 1; cfmakeraw(&tio); rc = tcsetattr(fileno(stdin), TCSADRAIN, &tio); } return rc; } static int _normal_mode(void) { if (tio_saved) return tcsetattr(fileno(stdin), TCSADRAIN, &_saved_tio); return 0; } int main (int argc, char *argv[]) { int sock = 0; unsigned long hostaddr = 0; short port = 22; char *username = NULL; char *password = NULL; struct sockaddr_in sin; LIBSSH2_SESSION *session; LIBSSH2_CHANNEL *channel; int nfds = 1; char buf; LIBSSH2_POLLFD *fds = NULL; /* Struct winsize for term size */ struct winsize w_size; struct winsize w_size_bck; /* For select on stdin */ fd_set set; struct timeval timeval_out; timeval_out.tv_sec = 0; timeval_out.tv_usec = 10; printf(COPYRIGHT); if (argc > 4) { hostaddr = inet_addr(argv[1]); port = htons(atoi(argv[2])); username = argv[3]; password = argv[4]; } else { fprintf(stderr, "Usage: %s ip port user password\n", basename(argv[0])); return -1; } if (libssh2_init (0) != 0) { fprintf (stderr, "libssh2 initialization failed\n"); return -1; } sock = socket (AF_INET, SOCK_STREAM, 0); sin.sin_family = AF_INET; sin.sin_port = port; sin.sin_addr.s_addr = hostaddr; if (connect(sock, (struct sockaddr *) &sin, sizeof(struct sockaddr_in)) != 0) { fprintf (stderr, "Failed to established connection!\n"); return -1; } /* Open a session */ session = libssh2_session_init(); if (libssh2_session_startup (session, sock) != 0) { fprintf(stderr, "Failed Start the SSH session\n"); return -1; } /* Authenticate via password */ if (libssh2_userauth_password(session, username, password) != 0) { fprintf(stderr, "Failed to authenticate\n"); close(sock); goto ERROR; } /* Open a channel */ channel = libssh2_channel_open_session(session); if ( channel == NULL ) { fprintf(stderr, "Failed to open a new channel\n"); close(sock); goto ERROR; } /* Request a PTY */ if (libssh2_channel_request_pty( channel, "xterm") != 0) { fprintf(stderr, "Failed to request a pty\n"); close(sock); goto ERROR; } /* Request a shell */ if (libssh2_channel_shell(channel) != 0) { fprintf(stderr, "Failed to open a shell\n"); close(sock); goto ERROR; } if (_raw_mode() != 0) { fprintf(stderr, "Failed to entered in raw mode\n"); close(sock); goto ERROR; } while (1) { FD_ZERO(&set); FD_SET(fileno(stdin),&set); ioctl(fileno(stdin), TIOCGWINSZ, &w_size); if ((w_size.ws_row != w_size_bck.ws_row) || (w_size.ws_col != w_size_bck.ws_col)) { w_size_bck = w_size; libssh2_channel_request_pty_size(channel, w_size.ws_col, w_size.ws_row); } if ((fds = malloc (sizeof (LIBSSH2_POLLFD))) == NULL) break; fds[0].type = LIBSSH2_POLLFD_CHANNEL; fds[0].fd.channel = channel; fds[0].events = LIBSSH2_POLLFD_POLLIN; fds[0].revents = LIBSSH2_POLLFD_POLLIN; if (libssh2_poll(fds, nfds, 0) >0) { libssh2_channel_read(channel, &buf, 1); fprintf(stdout, "%c", buf); fflush(stdout); } if (select(fileno(stdin)+1,&set,NULL,NULL,&timeval_out) > 0) if (read(fileno(stdin), &buf, 1) > 0) libssh2_channel_write(channel, &buf, 1); free (fds); if (libssh2_channel_eof(channel) == 1) break; } if (channel) { libssh2_channel_free (channel); channel = NULL; } _normal_mode(); libssh2_exit(); return 0; ERROR: libssh2_session_disconnect(session, "Session Shutdown, Thank you for playing"); libssh2_session_free(session); return -1; }
0 0
- 基于libssh库实现SSH的Linux客户端程序
- libssh
- 基于TCP协议实现服务器和客户端的通信程序
- 【Linux网络编程】基于UDP的客户端/服务器程序
- 基于linux的文件传输器实现详解----客户端实现详解
- 基于linux的文件传输器实现详解----客户端实现详解
- 基于 HTML5 的 SSH 客户端 GateOne
- 基于linux的socket编程实现ftp客户端
- 基于ssh分页的实现
- linux C++ poll实现的聊天室程序 客户端----服务端
- Linux SSH客户端命令的使用
- Linux系统在SSH客户端关闭后继续运行程序
- 基于TCP的客户端程序示例
- 基于UDP协议的服务器/客户端程序
- 基于TCP协议的服务器/客户端程序
- 基于UDP协议的服务器/客户端程序
- 基于TCP协议的服务器/客户端程序
- 【CSDN开源夏令营】 基于Web的SSH客户端,实现跨平台的云服务管理(一)前言
- 《Spring 2.0技术手册》 读书笔记七-Spring的DAO框架(2)-JDBC支持
- 程序调试和消息菜单汇总知识
- GCD 的使用与理解(1)
- Leetcode#4||Median of Two Sorted Arrays
- 算法竞赛入门经典: 第三章 数组和字符串 3.1逆序输出
- 基于libssh库实现SSH的Linux客户端程序
- 【codevs2287】火车站,第一个A掉的钻石题(迟来的解题报告)
- TCPClient
- php5.4和apache2.4.9不在同一台服务器的配置注意事项
- 自学编程从哪开始
- python正则表达式 re (二)escape
- 求大于200的最小质数,java
- 21-IO流-08-IO流(字符流-练习-复制文本文件_1)
- Codeforces Round #315 (Div. 2) A. Music