Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
923 views
in Technique[技术] by (71.8m points)

ssh - Libssh channel request exec failed in C

I'm using Libssh to log into an ethernet switch and run commands. I can connect and log in just fine but commands cannot be run. The program just returns "Channel request exec failed" as the ssh error. Normal ssh outside of the program works as expected and commands can be executed. The program can run commands on my localhost just fine. I've tried different commands as well, including just the help command or things that would never produce errors if run. This is the function I'm using to send the command:

int send_command(ssh_session session, char *command){
    int rc;
    ssh_channel channel;
    
    channel = ssh_channel_new(session);
    if (channel == NULL){
        fprintf(stderr, "***Error in channel creation: %s***
", ssh_get_error(session));
        exit(-1);
    }

    rc = ssh_channel_open_session(channel);
    if (rc != SSH_OK){
        fprintf(stderr, "***Error opening channel: %s***
", ssh_get_error(session));
        exit(-1);
    }
    
    rc = ssh_channel_request_exec(channel, command);
    if (rc != SSH_OK){
        fprintf(stderr, "***Error sending command: %d, %s***
", rc, ssh_get_error(session));
        exit(-1);
    }   

    char buffer[256];
    int nbytes;
    
    nbytes = ssh_channel_read(channel, buffer, sizeof(buffer),0);
    while (nbytes > 0){
        if (fwrite(buffer, 1, nbytes, stdout) != nbytes){
            fprintf(stderr,"***Error writing result: %s***
", ssh_get_error(session));
            exit(-1);
        }
        nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    }

    ssh_channel_send_eof(channel);
    ssh_channel_close(channel);
    ssh_channel_free(channel);

    return SSH_OK;
}

I think it might have something to do with the custom CLI the switch is using - it's not a true linux terminal. The switch processor I'm logging into is running some kind of simplistic linux OS that I can't find information on. Are there different types of consoles I need to account for? Should I be opening a shell, even though the commands are programmatic and won't require user input? I've mostly been following the libssh tutorial to get this working so I don't know much about why things work or don't work, just that they do. I'm running this from a Cygwin enviroment on a windows machine if that matters.

question from:https://stackoverflow.com/questions/65890673/libssh-channel-request-exec-failed-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...