2017-07-04 74 views
0

libssh2を使用してssh経由でコマンドを送信しようとしましたが、期待される応答を受け取れません。libssh2 libでsshを使用してコマンドを送信する

これは私のラズベリーパイとよく繋がっています。 cmdコマンドで、出力を望んでいるかどうoutがある

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

機能は、コマンドを送信します。

そして、私は「稼働時間」を送信し、アップタイムを受信しようとした場合、私は代わりに、これを受け取る:

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]:~$ 
+0

'cmd = cmd + '\ n';' Cの文字列はこのようには動作しません。 'cmd'をコンソールに出力してから送信してください。 – iehrlich

+0

また、私は 'C'のために' C++ 'タグを削除しています。 – iehrlich

+0

@iehrlichそれは私のC++アプリケーション用ですが、その部分はCだけです、あなたは正しいです。 'cmd = cmd + '\ n''は私の古いライブラリを使用したためですが、これは必要ありません。 –

答えて

0

私はシェルを要求し、単一のコマンドの実行を要求すると考えているが、相互に排他的です。コマンドを実行し、その出力を取得し、チャネルを終了する必要がある場合は、libssh2_channel_execでなく、libssh2_channel_shellを実行する必要があります。

+0

それはなぜです!今すぐ仕事しました! –

関連する問題