Search code examples
c++sshchilkat

SSH - Treatment of output


I'm using chilkat lib to make ssh to my raspberry pi. I want to send a command using ssh and receive the output. The problem is always I send a command I receive a pile of text.

Example:

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Fri Jun 30 23:17:28 2017 from desktop-ca160hm.local
./status.sh
echo 'end'
pi@rasp:~$ ./status.sh
0
pi@rasp:~$ echo 'end'
end
pi@rasp:~$

But I only want to grab the 0 from the command ./status.sh

I already try to use:

output = output.substr( output.find_first_of(str) + str.length(), 1);

Where str is the var with the command "./status.sh" and output is the string with the text from ssh.

But output.find_first_of(str) always return 0.

Full function:

string s7c_SFTP::SendCmd(string str, bool out)
{
    if (s7c_SFTP::bloked) return "blocked";
    int channelNum = s7c_SFTP::ssh.QuickShell();
    if (channelNum < 0) return s7c_SFTP::ssh.lastErrorText();
    CkString cmd;
    str = str + "\n";
    cmd.append((char *)str.c_str());
    cmd.append("echo 'end'\n");
    if (!s7c_SFTP::ssh.ChannelSendString(channelNum, cmd.getStringAnsi(), "ansi"))
    {
        cout << "[WARNING] Unable to contact with the device!" << endl;
        return s7c_SFTP::ssh.lastErrorText();
    }
    if (!s7c_SFTP::ssh.ChannelSendEof(channelNum)) return s7c_SFTP::ssh.lastErrorText();
    if (!s7c_SFTP::ssh.ChannelReceiveUntilMatch(channelNum, "end", "ansi", true)) return s7c_SFTP::ssh.lastErrorText();
    if (!s7c_SFTP::ssh.ChannelSendClose(channelNum)) return s7c_SFTP::ssh.lastErrorText();
    if (!s7c_SFTP::ssh.ChannelReceiveToClose(channelNum)) return s7c_SFTP::ssh.lastErrorText();
    if (out)
    {
        string output = s7c_SFTP::ssh.getReceivedText(channelNum, "ansi");
        output = output.substr( output.find_first_of(str) + str.length(), 1);
        cout << output.c_str() << endl; // Only for debug
        return output;
    }
    else return "";
}

Solution

  • Solved!!!

    When I'm passing the var str to the function I add \n to simulate the enter.

    And when I make string.find_fist_of(); is counting with the \n and don't find, so return 0.