WinSsh2Proc.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #ifndef __WIN_SSH2_20200506__
  2. #define __WIN_SSH2_20200506__
  3. #include "libssh2_config.h"
  4. #include "libssh2.h"
  5. #include "libssh2_sftp.h"
  6. #include <winsock2.h>
  7. #pragma comment(lib, "ws2_32.lib")
  8. #pragma once
  9. #define SHUTDOWN(session, sock) \
  10. libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");\
  11. libssh2_session_free(session);\
  12. closesocket(sock);\
  13. libssh2_exit();
  14. class CWinSsh2Proc
  15. {
  16. public:
  17. CWinSsh2Proc(void);
  18. ~CWinSsh2Proc(void);
  19. private:
  20. int m_port;
  21. std::string m_host;
  22. std::string m_user;
  23. std::string m_pwd;
  24. sockaddr_in m_sin;
  25. void ssh2_read_channel(LIBSSH2_CHANNEL* pChannel, std::string &buffer);
  26. bool ssh2_init_session(LIBSSH2_SESSION** pSession, SOCKET &sock);
  27. void ssh2_free(LIBSSH2_SESSION* pSession, LIBSSH2_CHANNEL* pChannel);
  28. public:
  29. static bool InitSocket();
  30. static int waitsocket(int socket_fd, LIBSSH2_SESSION *session);
  31. bool ssh2_connect(std::string host, std::string user, std::string pwd, int port = 22);
  32. void ssh2_disconnect();
  33. bool ssh2_execute_command(std::string cmd, std::string &result, DWORD dwTimeout = 1000);
  34. bool ssh2_sftp_download(std::string sftppath, std::string localpath);
  35. };
  36. #endif //__WIN_SSH2_20200506__