LibSSH C ++包装器

编程入门 行业动态 更新时间:2024-10-19 17:40:18
LibSSH C ++包装器 - 离开范围时的基本远程连接段错误(LibSSH C++ wrapper - Basic remote connection segfaults when leaving scope)

我是libssh并试图连接到远程机器来运行一些命令。 所有连接和命令都会返回而不会出现错误,但是在离开范围时程序会出现段错误。 我究竟做错了什么?

#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 ()

I'm new to libssh and trying to connect to a remote machine to run some commands. All of the connections and commands return without error, but then the program segfaults when leaving scope. What am I doing wrong?

Code

#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 Trace

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; }

After beating on it awhile, we have a working solution. The first hurdle was recognizing that the remote host must be a "known host". After that, for me, it was necessary to then source the environment before running my executable. Last, I needed to give it some time to crunch with sleep( 1 ). Enjoy.

#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; }

更多推荐

本文发布于:2023-07-31 00:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340510.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:LibSSH

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!