3

I have created one session and one channel. A request (ssh_channel_request_exec) is supposed to be sent about every second and I want to read the answer (ssh_channel_read) to this request. However, I could not find an example on how to make multiple requests, the api only contains an example on how to make a request, read the answer and close the channel.

When I try requesting and reading from the channel twice in sequence, ssh_channel_request_exec returns an error the second time. Is it necessary to open a new channel for each request?

int ret = ssh_channel_request_exec(m_channel, request.c_str());
if (ret != SSH_OK)
{
    ssh_channel_close(m_channel);
    ssh_channel_free(m_channel);
    return false;
}

std::stringstream sstream;
nbytes = ssh_channel_read(m_channel, buffer, sizeof(buffer), 0);
while (nbytes > 0)
{
    sstream.write(buffer, nbytes);
    nbytes = ssh_channel_read(m_channel, buffer, sizeof(buffer), 0);
}

if (nbytes < 0) // 'nbytes' becomes zero the first time, the channel is not closed the second time!
{
    ssh_channel_close(m_channel);
    ssh_channel_free(m_channel);
    return false;
}

response = sstream.str();

The api also contains a ssh_channel_send_eof function that is used after reading from the channel in an example. What is good for? The api only says "Send an end of file on the channel. This doesn't close the channel. You may still read from it but not write."

A check for ssh_channel_is_eof (api: "Check if remote has sent an EOF") shows that the channel is not eof the first time, but the second time it is.

Jakuje
  • 24,773
  • 12
  • 69
  • 75
Ires Mari
  • 59
  • 8

1 Answers1

0

Yes. Non-interactive exec is intended for a single command to be executed and therefore you can run only on in single channel, as visible in the official documentation.

Jakuje
  • 24,773
  • 12
  • 69
  • 75