赞
踩
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=/opt/libssh/ ..
make -j6
sudo make install
ubuntu系统安装
./configure --prefix=/opt/libssh2
make -j6
sudo make install
MacOS系统安装
首先安装openssl
openssl官方下载链接
./config --prefix=/opt/openssl
make -j6
sudo make install
安装libssh2
./configure --prefix=/opt/libssh2 --with-libssl-prefix=/opt/openssl
make -j6
sudo make install
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}) # 链接库
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; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。