[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


Hi,list

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
?

Archive administrator: postmaster@lists.cynapses.org