0

我正在使用 libssh 库来创建与在远程服务器上创建的 ssh 服务器的连接。连接一切顺利,直到代码从服务器获取公钥并询问用户是否要保存它。那里的代码由于某种未知原因而崩溃。我正在使用此链接中给出的教程。

这是我到目前为止编写的代码。任何有关这方面的帮助都会很棒。

#include <libssh/libssh.h>
#include <iostream>


using namespace std;

int verifyServer(ssh_session);

int main(){

    ssh_session my_ssh_session = ssh_new();
    if (my_ssh_session == NULL)
        exit(-1);

    int verbosity = SSH_LOG_PROTOCOL;
    int port = 22;
    int connect;

    ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "hostname");
    ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
    ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
    ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "username");

    connect = ssh_connect(my_ssh_session);
    if(connect != SSH_OK){
        cout<<"Connection error!"<<endl;
        ssh_free(my_ssh_session);
        exit(-1);
    }
    int result = verifyServer(my_ssh_session);
    if(result < 0){
        ssh_disconnect(my_ssh_session);
        exit(-1);
    }
    connect = ssh_userauth_password(my_ssh_session, NULL, "password");

    if(connect != SSH_AUTH_SUCCESS){
        cout<<"Password authentication failed!"<<endl;
        ssh_disconnect(my_ssh_session);
        ssh_free(my_ssh_session);
        exit(-1);
    }



    ssh_free(my_ssh_session);

}

int verifyServer(ssh_session session){
    int state, hlen;
    unsigned char* hash = NULL;
    char* hexa;
    char buffer[10];

    state = ssh_is_server_known(session);

    hlen = ssh_get_pubkey_hash(session, &hash);
    if(hlen < 0){
        cout<<"Server public key hash error!"<<endl;
        return -1;
    }

    switch(state){
        case SSH_SERVER_KNOWN_OK:
            break;

        case SSH_SERVER_KNOWN_CHANGED:
            cout<<"Host key for the server has been changed to: \n";
            ssh_print_hexa("Public Key Hash", hash, hlen);
            cout<<"For security reasons, connection will now be stopped.\n";
            free(hash);
            return -1;

        case SSH_SERVER_FOUND_OTHER:
            cout<< "The host key for this server was not found but an other"
                "type of key exists.\n";
            cout<<"An attacker might change the default server key to"
                "confuse your client into thinking the key does not exist\n";
            free(hash);
            return -1;

        case SSH_SERVER_FILE_NOT_FOUND:
            cout<<"Could not find known host file.\n";
            cout<<"If you accept the host key here, the file will be"
                "automatically created.\n";

        case SSH_SERVER_NOT_KNOWN:
            hexa = ssh_get_hexa(hash, hlen);
            cout<<"The server is unknown. Do you trust the host key?\n";
            cout<<"Public key hash : "<<hexa<<endl;
            free(hexa);
            if (fgets(buffer, sizeof(buffer), stdin) == NULL)
            {
                free(hash);
                return -1;
            }
            if (_strnicmp(buffer, "yes", 3) != 0)
            {
                free(hash);
                return -1;
            }
            if (ssh_write_knownhost(session) < 0)
            {
                fprintf(stderr, "Error %s\n", strerror(errno));
                free(hash);
                return -1;
            }
            break;

        case SSH_SERVER_ERROR:
            cout<<ssh_get_error(session);
            free(hash);
            return -1;
    }

    free(hash);
    return 0;
}

这是给出错误的输出窗口。

在此处输入图像描述

4

1 回答 1

1

我没有看到您调用 ssh_init() 这很重要。你看过http://api.libssh.org上的教程和源代码中的例子吗?

于 2012-12-13T18:42:34.597 回答