Connect Sftp in C, show error : SSH session startup failed. Error code: -43, Error message: Failed sending banner

54 views Asked by At

My code

#include <stdio.h>
#include <libssh2.h>
#include <libssh2_sftp.h>

// Define the SSH connection parameters
const char *ssh_host = "localhost";
const int ssh_port = 22;  // Default SSH port
const char *ssh_username = "septian";
const char *ssh_password = "septian";

int sftp_test() {
    LIBSSH2_SESSION *session;
    LIBSSH2_SFTP *sftp_session;
    int rc;
    int sock;

    /* Initialize libssh2 */
    if (libssh2_init(0)) {
        fprintf(stderr, "libssh2 initialization failed\n");
        return 1;
    }

    /* Create an SSH session */
    session = libssh2_session_init();

    /* Set up your SSH session and authentication here */
    if (libssh2_session_startup(session, sock)) {
        char *error_msg = NULL;
        int error_code = libssh2_session_last_error(session, &error_msg, NULL, 0);
        fprintf(stderr, "SSH session startup failed. Error code: %d, Error message: %s\n", error_code, error_msg);

        //fprintf(stderr, "SSH session startup failed\n");
        return 1;
    }

    /* Authenticate with a username and password */
    if (libssh2_userauth_password(session, ssh_username, ssh_password)) {
        char *error_msg = NULL;
        int error_code = libssh2_session_last_error(session, &error_msg, NULL, 0);
        fprintf(stderr, "SSH session startup failed. Error code: %d, Error message: %s\n", error_code, error_msg);
        fprintf(stderr, "SSH authentication failed\n");
        return 1;
    }

    /* Create an SFTP session */
    sftp_session = libssh2_sftp_init(session);
    if (!sftp_session) {
        char *error_msg;
        libssh2_session_last_error(session, &error_msg, NULL, 0);
        fprintf(stderr, "SFTP session initialization failed: %s\n", error_msg);
        return 1;
    }

    // Rest of the code for listing files

    /* Cleanup and disconnect */
    libssh2_sftp_shutdown(sftp_session);
    libssh2_session_disconnect(session, "Bye bye, thanks for playing");
    libssh2_session_free(session);
    libssh2_exit();
    return 0;
}

after compile and run show error : SSH session startup failed. Error code: -43, Error message: Failed sending banner

my sftp use openssh-server on ubuntu.

If i use filezilla or terminal command : sftp septian@localhost, i can connect sftp well.

How to connect sftp in c?, what's failed sending banner ?

Example code sftp connect in c and how to avoid error Failed sending banner?

0

There are 0 answers