在项目中,使用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);
该设置可能会增加缓冲区大小(猜测)因此可解决以上问题。
#ifndef __LIB_SSH_H__
#define __LIB_SSH_H__
#include <libssh2.h>
#include <libssh2_sftp.h>
#ifdef WIN32 // WINDOWS
#include <windows.h>
#include <winsock2.h>
#else // UNIX
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#endif
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include <mutex>
#include <sstream>
#include <iostream>
#include <atomic>
#include <iomanip>
#include "factory.h"
namespace libssh2
{
static std::atomic_uint __libshh2_session_count;
static std::mutex __libshh2_session_count_mutex;
static const unsigned int MAX_BUF_SIZE =(128*1024);
typedef unsigned char auth_methods_t;
class auth_methods
{
public:
static const auth_methods_t PASSWORD = 1;
static const auth_methods_t KEYS = 2;
static const auth_methods_t INTERACTIVE = 4;
};
Exceptions
class exception : std::exception
{
public:
exception()
{
this->_what = "A generic exception has occurred.";
}
exception(std::string what)
{
this->_what = what;
}
const char* what()
{
return this->_what.c_str();
}
protected:
std::string _what;
};
class connect_exception : public exception
{
public:
connect_exception(int error_number)
{
// Convert error number to string
this->_errorno = error_number;
std::stringstream w;
w << "Connection on socket failed with " << error_number;
this->_what = w.str();
}
int get_socket_error()
{
return this->_errorno;
}
private:
int _errorno;
};
class authentication_exception : public exception
{
public:
authentication_exception()
{
this-&