libssh2使用

在项目中使用libssh2库通过ssh连接远程服务时,遇到因返回数据过大导致的‘--More--’问题。通过模拟putty客户端并调整API,例如增大PTY缓冲区大小(MAX_PTY_SIZE),成功解决了数据格式问题,实现了正常接收服务端返回数据。
摘要由CSDN通过智能技术生成

在项目中,使用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-&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jyj0710

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值