J'essaie d'utiliser libssh2 pour envoyer une commande sur ssh, mais je ne peux pas recevoir la réponse attendue.Envoyer une commande en utilisant ssh avec libssh2 lib
Il se connecte bien avec mon pi de framboise.
fonctionpour envoyer une commande:
char * s7c_hardware::sendCmd(char * cmd, bool out)
{
if (!(channel = libssh2_channel_open_session(session))) return "-1";
libssh2_channel_setenv(channel, "FOO", "bar");
if (libssh2_channel_request_pty(channel, "vanilla")) return "-2";
if (libssh2_channel_shell(channel)) return "-3";
int rc;
while ((rc = libssh2_channel_exec(channel, cmd)) ==
LIBSSH2_ERROR_EAGAIN)
{
waitsocket(sock, session);
}
char * output = "";
if (out)
{
do
{
char buffer[99999];
rc = libssh2_channel_read(channel, buffer, sizeof(buffer));
if (rc > 0)
{
fprintf(stderr, "We read:\n");
for (int i = 0; i < rc; ++i)
fputc(buffer[i], stderr);
fprintf(stderr, "\n");
}
else {
if (rc != LIBSSH2_ERROR_EAGAIN)
fprintf(stderr, "libssh2_channel_read returned %d\n", rc);
}
} while (rc > 0);
}
libssh2_channel_free(channel);
channel = NULL;
return output;
}
Où cmd
est la commande et out
est si veut la sortie ou non.
Et si j'essaie d'envoyer « disponibilité » et recevoir le temps, je reçois ceci:
We read:
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: Sat Jul 1 10:27:51 2017 from desktop-ca160hm.local
We read:
[email protected]:~$
'cmd = cmd + '\ n';' chaînes C ne fonctionnent pas de cette façon. Essayez d'imprimer votre 'cmd' sur la console avant de l'envoyer. – iehrlich
En outre, je supprime la balise 'C++' en faveur de 'C'. – iehrlich
@iehrlich Ok, c'est pour mon application C++, mais cette partie n'est que C, vous avez raison. Le 'cmd = cmd + '\ n'' est parce que mon ancien lib utilisé, mais celui-ci n'en a pas besoin. –