当前位置:   article > 正文

MacOS、ubuntu安装libssh、libssh2_ubuntu安装libssh2

ubuntu安装libssh2

libssh安装

libssh官方下载链接

mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=/opt/libssh/ ..
make -j6
sudo make install
  • 1
  • 2
  • 3
  • 4
  • 5

libssh2安装

libssh2官方下载链接

ubuntu系统安装

./configure --prefix=/opt/libssh2
make -j6
sudo make install
  • 1
  • 2
  • 3

MacOS系统安装
首先安装openssl
openssl官方下载链接

./config --prefix=/opt/openssl
make -j6
sudo make install
  • 1
  • 2
  • 3

安装libssh2

./configure --prefix=/opt/libssh2 --with-libssl-prefix=/opt/openssl
make -j6
sudo make install
  • 1
  • 2
  • 3
cmake_minimum_required(VERSION 3.0)		        	# CMake最低版本要求,低于2.6的构建过程会被终止
set(CMAKE_CXX_STANDARD 14)							#opencv4以上加此句
project(ssh_project)				        		# 定义工程名称
include_directories(/opt/libssh2/include/) 			# 头文件路径
file(GLOB LibSSH_LIBS /opt/libssh2/lib/lib*.dylib)
add_executable(ssh_project src/main.cpp)
target_link_libraries(ssh_project ${LibSSH_LIBS}) 	# 链接库
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

main.cpp

#include <stdio.h>
#include <iostream>
#include <libssh2.h>
#include <libssh2_sftp.h>
#include <sys/socket.h>
#include <arpa/inet.h>

int main() 
{
    // 初始化libssh2库
    int rc = libssh2_init(0);
    if (rc != 0) 
    {
        std::cerr << "libssh初始化失败. Error code: " << rc << std::endl;
        return 0;
    }

    // 建立SSH连接
    LIBSSH2_SESSION* session = libssh2_session_init();
    if (!session) {
        std::cerr << "SSH建立连接失败." << std::endl;
        return 1;
    }

    // 设置远程主机的IP地址和端口号
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0)
    {
        std::cerr << "socket failed" << std::endl;
        return 0;
    }
        
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(22);
    sin.sin_addr.s_addr = inet_addr("remote_ip");

    rc = connect(sock, (struct sockaddr*)&sin, sizeof(sin));
    if (rc != 0)
    {
        std::cerr << "connect failed" << std::endl;
        return 0;
    }
        
    // 连接远程主机
    rc = libssh2_session_startup(session, sock);
    if (rc != 0) 
    {
        std::cerr << "无法与远程服务器建立SSH连接. Error code: " << rc << std::endl;
        return 1;
    }

    // 进行身份验证(用户名和密码)
    const char* username = "name";
    const char* password = "password";
    rc = libssh2_userauth_password(session, username, password);
    if (rc != 0) 
    {
        std::cerr << "远程服务器验证失败. Error code: " << rc << std::endl;
        return 0;
    }

    // 清理libssh2库
    libssh2_exit();
    
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/894681
推荐阅读
相关标签
  

闽ICP备14008679号