EmreS
EmreS

Reputation: 187

How can I open one channel and than execute several different commands in libssh?

For example i want to open one session and one channel and then execute severeal different commands. I have InitializeSSHSession and Initialize SSH channel methods can be seen below.

 void Computer::InitializeSSHSession()
 {
 ssh_session remoteSSHSession = ssh_new();

if ( remoteSSHSession )
{
    QString password = this->GetPassword();
    QString remoteIP = this->GetIP();
    QString userName = this->GetUserNameW();

    ssh_options_set(remoteSSHSession, SSH_OPTIONS_HOST, remoteIP.toStdString().c_str());
    ssh_options_set(remoteSSHSession, SSH_OPTIONS_LOG_VERBOSITY, &sessionVerbosity);
    ssh_options_set(remoteSSHSession, SSH_OPTIONS_PORT, &sessionPort);
    ssh_options_set(remoteSSHSession, SSH_OPTIONS_USER, userName.toStdString().c_str());

    int remoteConnection = ssh_connect(remoteSSHSession);
    if ( remoteConnection == SSH_OK )
    {
        int authenticateControl = ssh_userauth_password(remoteSSHSession, NULL, password.toStdString().c_str());
        if ( authenticateControl == SSH_AUTH_SUCCESS )
        {
            InitializeSSHChannel(remoteSSHSession);
        }
        else
        {
            remoteSSHSession = NULL;
        }
    }
    else
    {
        remoteSSHSession = NULL;
    }
}
else
{
     remoteSSHChannel = NULL;
}
}

void Computer::InitializeSSHChannel(ssh_session remoteSSHSession)
{
remoteSSHChannel = ssh_channel_new(remoteSSHSession);

if ( remoteSSHChannel )
{
    int channelControl = ssh_channel_open_session(remoteSSHChannel);
    if ( channelControl != SSH_OK )
    {
        EventLogger::LogMessage(true, "SSH channel sesssion failed!");
        ssh_channel_free(remoteSSHChannel);
    }
}
 }

And in the main, I am initializing session and getting channel like this:

 InitializeSSHSession();
 ssh_channel channel = GetSSHChannel(); 

And then

  int rc = ssh_channel_request_exec(channel, "ls -l");

It is okay it is executed correctly but when i want to execute another command it is not execute, I should return the beginning of process first initialize session and getting channel again.

It is not good solution for every command. Is it possible to do that once initialize session than use it again and again for every command ?

Upvotes: 5

Views: 463

Answers (1)

Valdez
Valdez

Reputation: 86

According to the LibSSH doc, the ssh_channel_request_exec() command can only be used once per channel (not sure why they would make this design choice). So yes, you would have to open a brand new session (I've tried just creating new channels using the same session, that doesn't seem to work either) and then creating a new channel.

The alternative to doing this (and the way that worked for me) is by opening a shell (follow their tutorial #3 https://api.libssh.org/stable/libssh_tutor_shell.html#opening_shell). It requires a few more extra steps but this allows you to send more commands. However just remember to add the new line character at the end of every command ("\n") otherwise these won't be executed.

Upvotes: 0

Related Questions