I'm using libssh and I want to get some output from an executed command. It works for the most part, but I'm getting unwanted characters in the output. What am I doing wrong?
Example output for the command "test -f "/path/to/file" && echo found || echo not found"
not found
t foun
I want "not found", but not the line below it -- "t foun"
Where I think the problem is:
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
while (nbytes > 0)
{
    output.append(buffer, sizeof(buffer));
    nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
}
Here's my function.
std::string exec_command(ssh_session session, const std::string& command)
{
    ssh_channel channel;
    int rc;
    char* buffer;
    std::string output;
    int nbytes;
    channel = ssh_channel_new(ssh_session);
    if (channel == NULL)
        return "Error";
    rc = ssh_channel_open_session(channel);
    if (rc != SSH_OK)
    {
        ssh_channel_free(channel);
        return "Not Ok";
    }
    rc = ssh_channel_request_exec(channel, command.c_str());
    if (rc != SSH_OK)
    {
        ssh_channel_close(channel);
        ssh_channel_free(channel);
        return "Not Ok";
    }
    nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    while (nbytes > 0)
    {
        output.append(buffer, sizeof(buffer));
        nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    }
    if (nbytes < 0)
    {
        ssh_channel_close(channel);
        ssh_channel_free(channel);
        return "Error";
    }
    ssh_channel_send_eof(channel);
    ssh_channel_close(channel);
    ssh_channel_free(channel);
    return output;
}
 
    