当前位置:   article > 正文

libssh2使用_libssh2 pty自动

libssh2 pty自动

在项目中,使用ssh连接远程服务过程中,由于服务端底层修改,导致ssh执行命令异常,方案采用模拟putty客户端方式抓取服务端回返的数据。

出现以下问题:

1,由于返回数据过大,因此会出现“--More--”问题,该问题,可通过模拟手动数据enter键使返回数据正常。

2,当时操作步骤1时,会出现数据格式存在问题,因此可以修改API,

#define MAX_PTY_SIZE (4096000)

  libssh2_channel_request_pty_size(this->_chan, MAX_PTY_SIZE, MAX_PTY_SIZE);

该设置可能会增加缓冲区大小(猜测)因此可解决以上问题。

 

 

  1. #ifndef __LIB_SSH_H__
  2. #define __LIB_SSH_H__
  3. #include <libssh2.h>
  4. #include <libssh2_sftp.h>
  5. #ifdef WIN32 // WINDOWS
  6. #include <windows.h>
  7. #include <winsock2.h>
  8. #else // UNIX
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <unistd.h>
  12. #include <arpa/inet.h>
  13. #endif
  14. #include <sys/types.h>
  15. #include <fcntl.h>
  16. #include <errno.h>
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include <mutex>
  20. #include <sstream>
  21. #include <iostream>
  22. #include <atomic>
  23. #include <iomanip>
  24. #include "factory.h"
  25. namespace libssh2
  26. {
  27. static std::atomic_uint __libshh2_session_count;
  28. static std::mutex __libshh2_session_count_mutex;
  29. static const unsigned int MAX_BUF_SIZE =(128*1024);
  30. typedef unsigned char auth_methods_t;
  31. class auth_methods
  32. {
  33. public:
  34. static const auth_methods_t PASSWORD = 1;
  35. static const auth_methods_t KEYS = 2;
  36. static const auth_methods_t INTERACTIVE = 4;
  37. };
  38. Exceptions
  39. class exception : std::exception
  40. {
  41. public:
  42. exception()
  43. {
  44. this->_what = "A generic exception has occurred.";
  45. }
  46. exception(std::string what)
  47. {
  48. this->_what = what;
  49. }
  50. const char* what()
  51. {
  52. return this->_what.c_str();
  53. }
  54. protected:
  55. std::string _what;
  56. };
  57. class connect_exception : public exception
  58. {
  59. public:
  60. connect_exception(int error_number)
  61. {
  62. // Convert error number to string
  63. this->_errorno = error_number;
  64. std::stringstream w;
  65. w << "Connection on socket failed with " << error_number;
  66. this->_what = w.str();
  67. }
  68. int get_socket_error()
  69. {
  70. return this->_errorno;
  71. }
  72. private:
  73. int _errorno;
  74. };
  75. class authentication_exception : public exception
  76. {
  77. public:
  78. authentication_exception()
  79. {
  80. this-&
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/960827
推荐阅读
相关标签
  

闽ICP备14008679号