SSH - 输出处理
SSH - Treatment of output
我正在使用 chilkat lib 为我的 raspberry pi 创建 ssh。
我想使用 ssh 发送命令并接收输出。
问题总是我发送一个命令我收到一堆文本。
示例:
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
我已经尝试使用:
output = output.substr( output.find_first_of(str) + str.length(), 1);
其中 str
是带有命令“./status.sh”的变量,output
是带有来自 ssh 的文本的字符串。
但是output.find_first_of(str)
总是return0
.
全功能:
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 "";
}
已解决!!!
当我将 var str
传递给函数时,我添加了 \n
来模拟输入。
并且当我用 string.find_fist_of();
计算时 \n
但没有找到,所以 return 0。
我正在使用 chilkat lib 为我的 raspberry pi 创建 ssh。 我想使用 ssh 发送命令并接收输出。 问题总是我发送一个命令我收到一堆文本。
示例:
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
我已经尝试使用:
output = output.substr( output.find_first_of(str) + str.length(), 1);
其中 str
是带有命令“./status.sh”的变量,output
是带有来自 ssh 的文本的字符串。
但是output.find_first_of(str)
总是return0
.
全功能:
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 "";
}
已解决!!!
当我将 var str
传递给函数时,我添加了 \n
来模拟输入。
并且当我用 string.find_fist_of();
计算时 \n
但没有找到,所以 return 0。