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

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


On 2/7/21 2:17 AM, Bruno Buzzi Brassesco wrote:
Hi,
I do not have my notebook now.
But you have to open a channel per each command.

Right,
or you will need to send separate commands in separate channel, because after ssh_channel_request_exec() shorthand, the underlying channel is closed.

The other possibility is to handle the IO for yourself and run the separate commands in a single shell as described in the following chapter of a tutorial:

https://api.libssh.org/stable/libssh_tutor_shell.html#opening_shell

Regards,
Jakub

Regards,
Bruno

El sáb., 6 de febrero de 2021 22:14, 孙世龙 sunshilong <sunshilong369@xxxxxxxxx <mailto:sunshilong369@xxxxxxxxx>> escribió:

    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
    ?



--
Jakub Jelen
Senior Software Engineer
Crypto Team, Security Engineering
Red Hat, Inc.


Archive administrator: postmaster@lists.cynapses.org