[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?


>>If so, then one more question raises, how the "ssh"(i.e. a command
>>that could execute on Linux) could achieve this goal?
>No sure about your question.
Sorry for my poor English.
Here is an example about how to execute several commands:

#either bash or dash is ok
velk@localhost:~$ ssh velk@192.168.69.130
velk@192.168.69.130's password:
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.15.0-128-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage


40 packages can be updated.
0 updates are security updates.

Last login: Sat Feb  6 18:32:28 2021 from 192.168.69.1
velk@localhost:~$ date
Sat Feb  6 18:32:59 PST 2021
velk@localhost:~$ time

real    0m0.000s
user    0m0.000s
sys     0m0.000s
velk@localhost:~$


You see I could execute the commands one by one other than executing
"date;time".

Best regards
sunshilong

On Sun, Feb 7, 2021 at 10:14 AM Bruno Buzzi Brassesco
<bruno.brasesco@xxxxxxxxx> wrote:
>
> No sure about your question. But you can send multiple commands separated by ; (semicolon) in one channel. But once the channel is used it has to be closed and released. If you need other command open a new channel. The session is always the same.
>
> El sáb., 6 de febrero de 2021 22:44, 孙世龙 sunshilong <sunshilong369@xxxxxxxxx> escribió:
>>
>> Bruno, Thank you for your reply.
>> >I do not have my notebook now.
>> >But you have to open a channel per each command.
>>
>> If so, then one more question raises, how the "ssh"(i.e. a command
>> that could execute on Linux) could achieve this goal?
>>
>> Best regards.
>>
>> On Sun, Feb 7, 2021 at 9:17 AM Bruno Buzzi Brassesco
>> <bruno.brasesco@xxxxxxxxx> wrote:
>> >
>> > Hi,
>> > I do not have my notebook now.
>> > But you have to open a channel per each command.
>> >
>> > Regards,
>> > Bruno
>> >
>> > El sáb., 6 de febrero de 2021 22:14, 孙世龙 sunshilong <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
>> >> ?
>> >>

Archive administrator: postmaster@lists.cynapses.org