首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SSH -输出处理

SSH -输出处理
EN

Stack Overflow用户
提问于 2017-07-03 16:25:46
回答 1查看 73关注 0票数 1

我正在使用chilkat lib对我的覆盆子pi进行ssh。我想使用ssh发送一个命令并接收输出。问题是,我总是发送一个命令,收到一堆文本。

示例:

代码语言:javascript
复制
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:~$

但我只想从命令./status.sh获取0

我已经尝试使用:

代码语言:javascript
复制
output = output.substr( output.find_first_of(str) + str.length(), 1);

其中str是带有命令"./status.sh“的var,output是带有ssh文本的字符串。

但是output.find_first_of(str)总是返回0

全功能:

代码语言:javascript
复制
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 "";
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-03 17:58:42

解决了!

当我将var enter传递给函数时,我添加了\n来模拟str

当我用\n计算string.find_fist_of();的时候,没有找到,所以返回0。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44880883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档