如何利用C++接入华为云服务器
#include <iostream>
#include <libssh/libssh.h>
int main() {
ssh_session session;
int verbosity = SSH_LOG_PROTOCOL;
const char* host = "your_server_ip";
const char* user = "your_username";
const char* password = "your_password";
// 创建SSH会话
session = ssh_new();
if (session == NULL) {
std::cerr << "SSH session creation failed." << std::endl;
return 1;
}
// 设置SSH选项
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
ssh_options_set(session, SSH_OPTIONS_HOST, host);
ssh_options_set(session, SSH_OPTIONS_USER, user);
// 连接到服务器
int rc = ssh_connect(session);
if (rc != SSH_OK) {
std::cerr << "SSH connection failed: " << ssh_get_error(session) << std::endl;
ssh_free(session);
return 1;
}
// 使用密码进行身份验证
rc = ssh_userauth_password(session, NULL, password);
if (rc != SSH_AUTH_SUCCESS) {
std::cerr << "SSH authentication failed: " << ssh_get_error(session) << std::endl;
ssh_disconnect(session);
ssh_free(session);
return 1;
}
std::cout << "Successfully connected to the server!" << std::endl;
// 在这里,你可以执行远程命令或进行其他操作
// 断开连接并释放资源
ssh_disconnect(session);
ssh_free(session);
return 0;
}
- 点赞
- 收藏
- 关注作者
评论(0)