Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème avec la création d'un tunnel SSH

    27 janvier 2024 à 13:27:11

    Bonjour à tous,
    Je travaille actuellement sur un programme en C++ qui vise à établir un tunnel SSH vers un serveur distant. Cependant, je rencontre un problème, car le canal SSH (ssh_channel) est toujours nul après l'appel à ssh_channel_new.
    J'ai déjà inclus des vérifications d'erreur, mais le message d'erreur que j'obtiens de ssh_get_error ne m'aide pas beaucoup. Je suis à la recherche de conseils sur la résolution de ce problème.
    #include <iostream>
    #include <libssh/libssh.h>
    
    int main() {
        // Set SSH connection info
        const char* sshHost = "127.0.0.1";
        int sshPort = 36;
        const char* sshUsername = "orion";
        const char* sshPassword = "lol";
    
        const char* targetHost = "116.138.1.240";
        int targetPort = 956;
    
        if (ssh_init() != SSH_OK)
        {
            std::cerr << "Failed to initialize SSH library" << std::endl;
            return 1;
        }
    
        ssh_session sshSession = ssh_new();
        if (sshSession == nullptr) {
            std::cerr << "Failed to create SSH session" << std::endl;
            return 1;
        }
    
        // Set up connection options
        ssh_options_set(sshSession, SSH_OPTIONS_HOST, targetHost);
        ssh_options_set(sshSession, SSH_OPTIONS_PORT, &targetPort);
        ssh_options_set(sshSession, SSH_OPTIONS_USER, sshUsername);
    
        // Connect to SSH server
        int rc = ssh_connect(sshSession);
        if (rc != SSH_OK) {
            std::cerr << "Error connecting to SSH server: " << ssh_get_error(sshSession) << std::endl;
            ssh_free(sshSession);
            return 1;
        }
    
        std::cout << "SSH connection established." << std::endl;
    
        std::cout << "Session status: " << ssh_is_connected(sshSession) << std::endl;
    
        // Set up port forwarding
        ssh_channel channel = ssh_channel_new(sshSession);
        if (channel == nullptr)
        {
            std::cerr << "Error on ssh_channel_new : " << ssh_get_error(sshSession) << std::endl;
            ssh_disconnect(sshSession);
            ssh_free(sshSession);
            return 1;
        }
    
        rc = ssh_channel_open_forward(channel, targetHost, targetPort, sshHost, sshPort);
        if (rc != SSH_OK)
        {
            std::cerr << "Error on ssh_channel_open_forward: " << ssh_get_error(sshSession) << std::endl;
            ssh_channel_free(channel);
            ssh_disconnect(sshSession);
            ssh_free(sshSession);
            return 1;
        }
    
        std::cout << "Port forwarding from localhost:" << sshPort << " to " << targetHost << ":" << targetPort << " is established." << std::endl;
    
        // Send the "pwd" command
        rc = ssh_channel_request_exec(channel, "pwd");
        if (rc != SSH_OK) {
            std::cerr << "Error executing command: " << ssh_get_error(sshSession) << std::endl;
        }
        else {
            char buffer[256];
            int nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
            if (nbytes > 0) {
                std::cout << "Command Result:\n" << std::string(buffer, nbytes) << std::endl;
            }
        }
    
        // Keep the program running
        std::cout << "Press 'Q' to quit." << std::endl;
        while (std::cin.get() != 'Q') {
            // Keep the program running until 'Q' is pressed
        }
    
        // Clean up
        ssh_channel_send_eof(channel);
        ssh_channel_free(channel);
        ssh_disconnect(sshSession);
        ssh_free(sshSession);
        ssh_finalize();
    
        std::cout << "SSH connection closed." << std::endl;
    
        return 0;
    }
    

    -
    Edité par JEANBAPTISTECOMTE1 27 janvier 2024 à 14:29:23

    • Partager sur Facebook
    • Partager sur Twitter

    Problème avec la création d'un tunnel SSH

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    • Editeur
    • Markdown