Libssh C 包装器 - 离开范围时基本的远程连接segfaults
LibSSH C++ wrapper - Basic remote connection segfaults when leaving scope
我是libssh
的新手,并尝试连接到远程计算机以运行一些命令。所有连接和命令都没有错误返回,但是在离开范围时,程序segfault会返回。我究竟做错了什么?
代码
#include <string>
#include <iostream>
using namespace std;
#define SSH_NO_CPP_EXCEPTIONS
#include <libssh/libsshpp.hpp>
int main()
{
ssh::Session session;
//Set options
session.setOption(SSH_OPTIONS_HOST, "192.168.200.101");
session.setOption( SSH_OPTIONS_USER, "user" );
//Connect to host
session.connect();
//Authenticate user
session.userauthPassword( "password" );
//Open channel
ssh::Channel channel( session );
channel.openSession();
//Do something
channel.requestExec( "ps_aux" );
//Close channel
channel.sendEof();
channel.close();
//Disconnect
session.disconnect();
return 0;
}
GDB跟踪
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff3a70a2f in ssh_channel_free () from /usr/local/lib/libssh.so.4
(gdb) where
#0 0x00007ffff3a70a2f in ssh_channel_free () from /usr/local/lib/libssh.so.4
#1 0x000000000059f436 in ssh::Channel::~Channel() ()
#2 0x000000000059e603 in main ()
在跳动一段时间后,我们有一个工作解决方案。第一个障碍是认识到远程主机必须是"已知主人"。在那之后,对我来说,有必要在运行我的可执行文件之前先来采购环境。最后,我需要给它一些时间来处理sleep( 1 )
。享受。
#include <iostream>
using namespace std;
#include <libssh/libsshpp.hpp>
int main()
{
int port = 22;
//Can use SSH_LOG_PROTOCOL here for verbose output
int verbosity = SSH_LOG_NOLOG;
ssh::Session session;
try
{
session.setOption( SSH_OPTIONS_LOG_VERBOSITY, &verbosity );
session.setOption( SSH_OPTIONS_PORT, &port );
session.setOption( SSH_OPTIONS_USER, "user" );
session.setOption( SSH_OPTIONS_HOST, "192.168.52.101" );
session.connect();
if( session.isServerKnown() != SSH_SERVER_KNOWN_OK )
{
if( session.writeKnownhost() != SSH_OK )
{
cout << "writeKnownHost failed" << endl;
}
else
{
session.connect();
}
}
if( session.userauthPassword( "password" ) != SSH_AUTH_SUCCESS )
{
cout << "failed auth" << endl;
}
ssh::Channel channel( session );
channel.openSession();
//Source environment if necessary, run executable
channel.requestExec( "source /path/to/set_env.sh; /path/to/executable/..." );
channel.close();
channel.sendEof();
//Unfortunate brute force step, the exec call needed some time
sleep( 1 );
}
catch( ssh::SshException e )
{
std::cout << "Error during connection : ";
std::cout << e.getError() << std::endl;
}
return 0;
}
相关文章:
- 使用 ssh 重新连接到远程 Ubuntu 机器后,如何继续使用 gdb 调试 c++ 代码?
- C++ Winsock2 客户端未通过远程 IP 连接到服务器
- 使用 IP 帮助程序的 Windows 中活动 UDP 连接的远程地址
- 连接到远程存储库并获取数据
- 接受套接字,但m_socket.远程终结点引发 传输终结点未连接
- C UDP RecvFrom最终悬挂在远程连接上
- DirectX 11 GetDisplayModeList() 在远程桌面连接中失败
- 通过 gdb 连接到远程 gdb 服务器时出错
- 如何从活动的 udp 连接获取远程 IP 地址和端口
- QtRO - 类 qremoteobjects - 如何连接 2 个和更多远程对等体 beetwen TCP
- 将Raspberry Pi 2与外部远程数据库连接
- 以编程方式连接到远程 SQL Server C++
- Libssh C 包装器 - 离开范围时基本的远程连接segfaults
- MySQL 服务器不接受远程连接
- C++ Winsock 服务器-客户端远程连接
- mysql 远程连接 - mysql CL 客户端工作,C++应用程序不工作
- 无法连接到服务器(通过远程连接)
- MySQL和Qt远程连接
- 远程连接MySQL是安全的吗?
- C++应用程序如何在不让MySQL服务器接受远程连接的情况下访问远程MySQL数据库