C++ 如何用c++建立简单的ssh连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33789213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:19:07  来源:igfitidea点击:

How to establish a simple ssh connection with c++

c++sshvisual-studio-2015libssh

提问by mac johnsto

I am trying to make a c++ program which will connect to an ssh server (my laptop). The server is ok because I can get connected via putty. Although the program I wrote so far can not. In my code I am using the library libssh.h and for development I used visual studio 2015. The error I get is: crypt_set_algorithms2: no crypto algorithm function found for 3des-cbcI haven't found anything so far so I hope to your help. The code I use:

我正在尝试制作一个将连接到 ssh 服务器(我的笔记本电脑)的 C++ 程序。服务器没问题,因为我可以通过腻子连接。虽然到目前为止我写的程序不能。在我的代码中,我使用库 libssh.h 并进行开发,我使用了 Visual Studio 2015。我得到的错误是:crypt_set_algorithms2: no crypto algorithm function found for 3des-cbc我到目前为止还没有找到任何东西,所以我希望你帮助。我使用的代码:

#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h> 
int main()
{
    ssh_session my_ssh_session;
    int rc;
    int port = 22;
    int verbosity = SSH_LOG_PROTOCOL;
    char *password;
    // Open session and set options
    my_ssh_session = ssh_new();
    if (my_ssh_session == NULL)
        exit(-1);
    ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "192.168.1.6");
    ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "john");
    ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
    ssh_options_set(my_ssh_session, SSH_OPTIONS_CIPHERS_C_S,"aes128-ctr");

    //ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
    // Connect to server
    rc = ssh_connect(my_ssh_session);
    if (rc != SSH_OK)  
    {
        fprintf(stderr, "Error: %s\n", ssh_get_error(my_ssh_session)); //HERE IS WHERE I GET THE ERROR 
        ssh_free(my_ssh_session);
        exit(-1);
    }
    // Verify the server's identity
    // For the source code of verify_knowhost(), check previous example
/*  if (verify_knownhost(my_ssh_session) < 0)
    {
        ssh_disconnect(my_ssh_session);
        ssh_free(my_ssh_session);
        exit(-1);
    }
*/  
    // Authenticate ourselves
    password = "pass";
    rc = ssh_userauth_password(my_ssh_session, NULL, password);
    if (rc != SSH_AUTH_SUCCESS)
    {
        fprintf(stderr, "Error authenticating with password: %s\n",
            ssh_get_error(my_ssh_session));
        ssh_disconnect(my_ssh_session);
        ssh_free(my_ssh_session);
        exit(-1);
    }


        ssh_disconnect(my_ssh_session);
    ssh_free(my_ssh_session);
}

采纳答案by Jakuje

Try to use different cipher. 3des-cbcis broken and probably disabled on your server already.

尝试使用不同的密码。3des-cbc已损坏并且可能已在您的服务器上禁用。

There is really nice tutorialwith simple session.

有简单的会话非常好的教程

Removing the line makes it working for me on Ubuntu (don't know where you found it):

删除该行使其在 Ubuntu 上对我有用(不知道您在哪里找到它):

ssh_options_set(my_ssh_session, SSH_OPTIONS_CIPHERS_C_S,"aes128-ctr");

If not, what version of libsshare you using? Isn't it some obsolete one?

如果没有,libssh您使用的是什么版本?是不是有些过时了?

回答by mac johnsto

Finally got it! I removed the line

终于拿到了!我删除了该行

ssh_options_set(my_ssh_session, SSH_OPTIONS_CIPHERS_C_S,"aes128-ctr");

Then I used the ssh.dll from libssh v0-7.2instead of the ssh.dll of v0-7.1I used before. The ssh.dll is located in libssh-0.7.2\bin. If you take an error like the msvcr120d.dllis missing try to change from debug to release in visual studio.

然后我使用了 libssh v0-7.2 中的ssh.dll而不是我之前使用的 v0-7.1 中的ssh.dll。ssh.dll 位于libssh-0.7.2\bin 中。如果您遇到类似msvcr120d.dll丢失的错误,尝试在 Visual Studio 中从调试更改为发布。