|
@@ -11,35 +11,193 @@
|
|
|
#pragma once
|
|
|
|
|
|
#define SHUTDOWN(session, sock) \
|
|
|
-libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");\
|
|
|
-libssh2_session_free(session);\
|
|
|
-closesocket(sock);\
|
|
|
-libssh2_exit();
|
|
|
+ libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");\
|
|
|
+ libssh2_session_free(session);\
|
|
|
+ closesocket(sock);\
|
|
|
+ libssh2_exit();
|
|
|
|
|
|
class CWinSsh2Proc
|
|
|
{
|
|
|
public:
|
|
|
- CWinSsh2Proc(void);
|
|
|
- ~CWinSsh2Proc(void);
|
|
|
+ CWinSsh2Proc(void);
|
|
|
+ ~CWinSsh2Proc(void);
|
|
|
|
|
|
private:
|
|
|
- int m_port;
|
|
|
- std::string m_host;
|
|
|
- std::string m_user;
|
|
|
- std::string m_pwd;
|
|
|
- sockaddr_in m_sin;
|
|
|
-
|
|
|
- void ssh2_read_channel(LIBSSH2_CHANNEL* pChannel, std::string &buffer);
|
|
|
- bool ssh2_init_session(LIBSSH2_SESSION** pSession, SOCKET &sock);
|
|
|
- void ssh2_free(LIBSSH2_SESSION* pSession, LIBSSH2_CHANNEL* pChannel);
|
|
|
+ int m_port;
|
|
|
+ std::string m_host;
|
|
|
+ std::string m_user;
|
|
|
+ std::string m_pwd;
|
|
|
+ sockaddr_in m_sin;
|
|
|
+
|
|
|
+ void ssh2_read_channel(LIBSSH2_CHANNEL* pChannel, std::string &buffer);
|
|
|
+ bool ssh2_init_session(LIBSSH2_SESSION** pSession, SOCKET &sock);
|
|
|
+ void ssh2_free(LIBSSH2_SESSION* pSession, LIBSSH2_CHANNEL* pChannel);
|
|
|
+public:
|
|
|
+ static bool InitSocket();
|
|
|
+ static int waitsocket(int socket_fd, LIBSSH2_SESSION *session);
|
|
|
+ bool ssh2_connect(std::string host, std::string user, std::string pwd, int port = 22);
|
|
|
+ void ssh2_disconnect();
|
|
|
+ bool ssh2_execute_command(std::string cmd, std::string &result, DWORD dwTimeout = 1000);
|
|
|
+ bool ssh2_sftp_download(std::string sftppath, std::string localpath);
|
|
|
+};
|
|
|
+
|
|
|
+// 读超时值;
|
|
|
+const int CHANNEL_READ_TIMEOUT = 3000;
|
|
|
+class Channel
|
|
|
+{
|
|
|
+public:
|
|
|
+ Channel(LIBSSH2_CHANNEL *channel);
|
|
|
+ ~Channel(void);
|
|
|
+
|
|
|
+ std::string Read( const std::string &strend = "$", int timeout = CHANNEL_READ_TIMEOUT );
|
|
|
+ bool Write(const std::string &data);
|
|
|
+private:
|
|
|
+ Channel(const Channel&);
|
|
|
+ Channel operator=(const Channel&);
|
|
|
+private:
|
|
|
+ LIBSSH2_CHANNEL *m_channel;
|
|
|
+};
|
|
|
+
|
|
|
+class CWindSSH2Proc
|
|
|
+{
|
|
|
public:
|
|
|
- static bool InitSocket();
|
|
|
- static int waitsocket(int socket_fd, LIBSSH2_SESSION *session);
|
|
|
- bool ssh2_connect(std::string host, std::string user, std::string pwd, int port = 22);
|
|
|
- void ssh2_disconnect();
|
|
|
- bool ssh2_execute_command(std::string cmd, std::string &result, DWORD dwTimeout = 1000);
|
|
|
- bool ssh2_sftp_download(std::string sftppath, std::string localpath);
|
|
|
+ CWindSSH2Proc(const std::string &ip, int port=22);
|
|
|
+ ~CWindSSH2Proc();
|
|
|
+
|
|
|
+ bool Connect(const std::string &userName, const std::string &password);
|
|
|
+ bool Disconnect();
|
|
|
+
|
|
|
+ Channel *CreateChannel(const std::string &ptyType = "vanilla");
|
|
|
+public:
|
|
|
+ static void KbdCallback(const char*, int, const char*, int, int, const LIBSSH2_USERAUTH_KBDINT_PROMPT *, LIBSSH2_USERAUTH_KBDINT_RESPONSE*, void **a);
|
|
|
+ static std::string s_password;
|
|
|
+
|
|
|
+private:
|
|
|
+ std::string m_hostIP;
|
|
|
+ int m_hostPort;
|
|
|
+ std::string m_userName;
|
|
|
+ std::string m_password;
|
|
|
+ SOCKET m_sock;
|
|
|
+ LIBSSH2_SESSION *m_session;
|
|
|
};
|
|
|
|
|
|
|
|
|
-#endif //__WIN_SSH2_20200506__
|
|
|
+namespace SSH2
|
|
|
+{
|
|
|
+ // 初始化SOKCE库;
|
|
|
+ bool InitSocket()
|
|
|
+ {
|
|
|
+ WSADATA wsadata;
|
|
|
+ int err = WSAStartup(MAKEWORD(2, 0), &wsadata);
|
|
|
+ if( err != 0 ) {
|
|
|
+ _ftprintf(stderr, _T("WSAStartup failed with error: %d\n"), err);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 初始化LIBSSH2;
|
|
|
+ bool InitSSH2()
|
|
|
+ {
|
|
|
+ int rc = libssh2_init(0);
|
|
|
+ if ( rc != LIBSSH2_ERROR_NONE ) {
|
|
|
+ _ftprintf(stderr, _T("libssh2 initialization failed (%d)\n"), rc);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 释放LIBSSH2;
|
|
|
+ void ExitSSH2()
|
|
|
+ {
|
|
|
+ libssh2_exit();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 单Session、单Channel;
|
|
|
+ // 一个session拖多个channel,会不会有性能问题,拖几比较合适.
|
|
|
+ class CWindSSH2Proc
|
|
|
+ {
|
|
|
+ public:
|
|
|
+ CWindSSH2Proc();
|
|
|
+ ~CWindSSH2Proc();
|
|
|
+
|
|
|
+ public:
|
|
|
+ bool InitSession();
|
|
|
+ bool LoginSession();
|
|
|
+ bool OpenSession();
|
|
|
+ // create channel;
|
|
|
+ LIBSSH2_CHANNEL *CreateChannel();
|
|
|
+ /*
|
|
|
+ 一个channel对应一次读写操作,并发时就需要多个channel;
|
|
|
+ 所以channel结构如下:
|
|
|
+ typedef struct __CHANNEL__
|
|
|
+ {
|
|
|
+ LIBSSH2_CHANNEL *pChannel; // Channel对象;
|
|
|
+ std::string strCommand; // 要执行的命令;
|
|
|
+ std::string strResult; // 执行结果;
|
|
|
+ };
|
|
|
+
|
|
|
+ 如果扩展,那么一个Channel其实可以串行执行多组命令,扩展后如下
|
|
|
+ typedef struct __CHANNEL__
|
|
|
+ {
|
|
|
+ LIBSSH2_CHANNEL *pChannel;
|
|
|
+ int nCommandCount; 该Channel的所有命令总数
|
|
|
+ typedef struct __CMDPKG__
|
|
|
+ {
|
|
|
+ int nExecuteStatus; // 命令执行状态; 0-未执行,1-执行中,2-执行完;
|
|
|
+ int nCommandId; // 命令索引,自增;
|
|
|
+ std::string strCommand; // 要执行的命令;
|
|
|
+ std::string strResult; // 执行结果;
|
|
|
+ }CmdPkg, *pCmdPkg;
|
|
|
+ // 串行要执行的命令集;
|
|
|
+ std::vector<CmdPkg> vtCommandPackage;
|
|
|
+ };
|
|
|
+
|
|
|
+ 再次扩展,那么可以多个Channel并发处理任务,需要为每个Channel定义ID、名称;
|
|
|
+ typedef struct __CHANNEL__
|
|
|
+ {
|
|
|
+ int ID; // 该Channel的ID;
|
|
|
+ std::string strName; // 该Channel的名称,描述Channel专职某项功能;
|
|
|
+ LIBSSH2_CHANNEL *pChannel; // 该Channel实例对象;
|
|
|
+ int nCommandCount; // 该Channel的所有命令总数
|
|
|
+ typedef struct __CMDPKG__
|
|
|
+ {
|
|
|
+ int nExecuteStatus; // 命令执行状态; 0-未执行,1-执行中,2-执行完;
|
|
|
+ int nCommandId; // 命令索引,自增;
|
|
|
+ std::string strCommand; // 要执行的命令;
|
|
|
+ std::string strResult; // 执行结果;
|
|
|
+ }CmdPkg, *pCmdPkg;
|
|
|
+ // 串行要执行的命令集;
|
|
|
+ std::vector<CmdPkg> vtCommandPackage;
|
|
|
+ };
|
|
|
+
|
|
|
+ 多个Channel不能在同一个目录,时间相差不远的情况下执行同一条.sh编译脚本,所以,命令细分下来,可以有以下几部分组成:
|
|
|
+ 1、命令要执行的路径;
|
|
|
+ 2、命令类型:sh脚本、文件、python脚本等为一类;bin命令为一类;
|
|
|
+ 3、命令执行有无输出日志到指定文件
|
|
|
+
|
|
|
+ // 创建新的通道;
|
|
|
+ LIBSSH2_CHANNEL CreateChannel();
|
|
|
+ // 获取空闲的通道;
|
|
|
+ LIBSSH2_CHANNEL GetFreeChannel();
|
|
|
+ // 添加执行命令到指定通道的命令集尾部;
|
|
|
+ bool AppendCommand(LIBSSH2_CHANNEL *pChannel, const std::string cmd);
|
|
|
+ // 插入执行命令到指定通道的未执行命令集头部;
|
|
|
+ bool InsertCommand(LIBSSH2_CHANNEL *pChannel, const std::string cmd);
|
|
|
+ // 删除指定通道的某条命令(未执行的);
|
|
|
+ void DeleteCommand(LIBSSH2_CHANNEL *pChannel, const std::string cmd);
|
|
|
+ // 查询某条命令执行状态和结果;
|
|
|
+ bool QueryCommand(LIBSSH2_CHANNEL *pChannel, int &nStatus, std::string &strResult);
|
|
|
+ */
|
|
|
+ };
|
|
|
+
|
|
|
+ // 一个sftp多次使用;
|
|
|
+ class CWindSFTPProc
|
|
|
+ {
|
|
|
+
|
|
|
+ };
|
|
|
+};
|
|
|
+
|
|
|
+#endif //__WIN_SSH2_20200506__
|