2017-07-03 2 views
1

J'utilise chilkat lib pour faire ssh à mon pi de framboise. Je veux envoyer une commande en utilisant ssh et recevoir la sortie. Le problème est toujours J'envoie une commande Je reçois une pile de texte.SSH - Traitement de sortie

Exemple:

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' 
[email protected]:~$ ./status.sh 
0 
[email protected]:~$ echo 'end' 
end 
[email protected]:~$ 

Mais je veux seulement saisir la 0 de la commande ./status.sh

J'essaie déjà d'utiliser:

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

str est le var avec la commande "./status.sh" et output est la chaîne avec le texte de ssh.

Mais output.find_first_of(str) retourne toujours 0.

fonction complète:

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 ""; 
} 

Répondre

0

!!! Résolu

Lorsque je passe le var str à la fonction, j'ajoute \n pour simuler l'entrée.

Et quand je fais string.find_fist_of(); est comptage avec le \n et ne trouvent pas, retournez donc 0.