1

私の質問は奇妙なようで、十分な助けが得られていませんが、戻ってきました。緊急時に解決する必要のある別の奇妙な質問があります。

私はiPhoneアプリを開発しています。これは、リモートホストを介したiPhoneを介したコマンド実行にlibssh2を使用します。単一で実行すれば、すべてのメソッドとコマンドは問題なく機能します。

私の問題は、一連のコマンドを検討することです。

pwd

=> o/pは/Users/mac01になります

cd xyz

=> o/pとしては何もありません

pwd

=> o/pは/Users/ mac01/xyzになります

だから、私の質問は、実行されたコマンドの最後の状態を保存することです...しかし、o/pとして取得しているのは 2回目のpwdコマンド実行後の/Users/mac01です。これは間違っています。

だから、誰かがそのようなタイプの問題で私を助けることができますか..?前もって感謝します。libssh2.0ライブラリを使用しています。

コマンドを実行するメソッド:

char * cmd_exec(const char * commandline、const char * host、const char * username、const char * password、int port){int sock、rc、bytecount = 0; char * cmd_contents;

if(!he)
{
    struct sockaddr_in sin;

ifdef WIN32

    WSADATA wsadata;
    WSAStartup(MAKEWORD(2,0), &wsadata);

endif

    /* Init and Make Socket Connection  */
    /*  Start Socket Connection         */
    sock = socket(AF_INET, SOCK_STREAM, 0);

ifndef WIN32

    fcntl(sock, F_SETFL, 0);

endif

    sin.sin_family = AF_INET;
    sin.sin_port = htons(port);
    /*sin.sin_addr.s_addr = inet_addr(host);

     if (connect(sock, (struct sockaddr*)(&sin),
     sizeof(struct sockaddr_in)) != 0) {    // in case connection failure
     fprintf(stderr, "Internet connection is required!\n");
     return "NETWORKFAILURE";
     }*/
    //const char *c = getIPFromHost("pepsi");
    //sin.sin_addr.s_addr = inet_addr(c);

    /*      IP Address Calculation  */

    he = gethostbyname(host);

    if(!he)
        return "Invalid hostname";

    struct in_addr **addr_list;
    addr_list = (struct in_addr **)he->h_addr_list;
    //for(int i = 0; addr_list[i] != NULL; i++) {
    if(addr_list != NULL){
        sin.sin_addr.s_addr = inet_addr(inet_ntoa(*addr_list[0]));
        //printf("%s", inet_ntoa(*addr_list[0]));
        if (connect(sock, (struct sockaddr*)(&sin),
                    sizeof(struct sockaddr_in)) != 0) { // in case connection failure
            fprintf(stderr, "Internet connection is required!\n");
            return "NETWORKFAILURE";
        }
    }
}
/*  End Socket Connection           */

// Initialize and create Session Instance
if(!session)
{
    session = libssh2_session_init();
    if ( !session ) 
    { 
        fprintf( stderr, "Error initializing SSH session\n" ); 
        return "SESSIONFAILURE"; 
    }


    /* Since we have set non-blocking, tell libssh2 we are non-blocking */
    //libssh2_session_set_blocking(session, 0);

    // Session starting
    if (libssh2_session_startup(session, sock)) {
        fprintf(stderr, "Failure establishing SSH session\n");
        return "SESSIONFAILURE";
    }

    /* Authenticate via password */ 
    if(strlen(password) != 0){
        if ( libssh2_userauth_password( session, username, password ) ) 
        { 
            fprintf( stderr, "Unable to authenticate user [%s]" 
                    "(wrong password specified?)\n", username ); 
            return "AUTHENTICATIONFAILURE";
        } 
    }else{
        while ((rc = libssh2_userauth_publickey_fromfile(session, username,
                                                         "/home/user/"
                                                         ".ssh/id_rsa.pub",
                                                         "/home/user/"
                                                         ".ssh/id_rsa",
                                                         password)) ==
               LIBSSH2_ERROR_EAGAIN);
        if (rc) {
            fprintf(stderr, "\tAuthentication by public key failed\n");
            return "AUTHENTICATIONFAILURE";
        }
    }

    //libssh2_session_set_blocking(session, 1);

}

// Open a session channel for command execution
if(!channel)

{
    channel = libssh2_channel_open_session(session);
if (!channel) {
    fprintf(stderr, "Unable to open a session\n");
    return "SESSIONFAILURE";
}

// Execute a command through channel

while( (rc = libssh2_channel_shell(channel)) ==

//while( (rc = libssh2_channel_exec(channel, commandline)) ==
      LIBSSH2_ERROR_EAGAIN )
{
    waitsocket(sock, session);
}

if( rc != 0 )   // if command execution failed
{
    fprintf(stderr,"Error\n");
    return "CMDFAILURE";
}
}

//libssh2_channel_write(channel,commandline,strlen(commandline));
do {
    /* write the same data over and over, until error or completion */
    rc = libssh2_channel_write(channel, commandline, sizeof(commandline));
    if (rc < 0) {
        fprintf(stderr, "ERROR %d\n", rc);
    }
} while (rc == 0);


while (libssh2_channel_send_eof(channel) == LIBSSH2_ERROR_EAGAIN);

/* read channel output  */
/* Start channel read   */
for( ;; )
{
    /* loop until we block */
    int rc;
    do
    {
        char buffer[0x4000];
//      char *tcontents = (char*)malloc(sizeof(buffer) + sizeof(cmd_contents));
        rc = libssh2_channel_read( channel, buffer, sizeof(buffer) );
        if( rc > 0 )
        {
            int i;
            bytecount += rc;
            for( i=0; i < rc; ++i )
                fputc( buffer[i], stderr);
            if(cmd_contents){
                free(cmd_contents);
            }
            cmd_contents = (char*)malloc(sizeof(buffer) + sizeof(cmd_contents));
            strcpy(cmd_contents, buffer);
            fprintf(stderr, "\n");
        }
        else {
            //fprintf(stderr, "libssh2_channel_read returned %d\n", rc);
        }
    }

    while( rc > 0 );

    /* this is due to blocking that would occur otherwise so we loop on
     this condition */
    if( rc == LIBSSH2_ERROR_EAGAIN )
    {
        waitsocket(sock, session);
    }
    else
        break;
}

/* End channel read */
 while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN );

/*      closing channel */
int exitcode = 127;

// while((rc = libssh2_channel_close(channel))== LIBSSH2_ERROR_EAGAIN); if(rc == 0){exitcode = libssh2_channel_get_exit_status(channel); } //
libssh2_channel_free(channel); //メモリチャネルを解放=NULL;

/*
libssh2_session_disconnect( session, "" );      // closing session
libssh2_session_free( session );                // free up memory
close( sock );                                  // closing socket
*/

return cmd_contents;

}

4

2 に答える 2

3

これを試してみてください多分それはうまくいきます...私はそれを試していませんが、あなたは十分なアイデアを得ることができ、適切な解決策かもしれません

libssh2_session_set_blocking(session, 0);
char buffer[0x4000]; rc = libssh2_channel_read( channel, buffer, sizeof(buffer) ); 
for( i=0; i < rc; ++i ) 
   fputc( buffer[i], stderr);
  if(cmd_contents)
  {

    free(cmd_contents);     
 }

  buffer[i] = '\0';
  cmd_contents = (char*)malloc(sizeof(buffer) + sizeof(cmd_contents));
  strcpy(cmd_contents, buffer);
  if( rc == LIBSSH2_ERROR_EAGAIN ) 
  {
    waitsocket(sock, session); 
  }

ハッピーコーディング...

于 2010-02-25T07:10:53.197 に答える
0

libssh2_session_set_blocking(session、0);

charバッファ[0x4000]; rc = libssh2_channel_read(channel、buffer、sizeof(buffer)); for(i = 0; i <rc; ++ i)fputc(buffer [i]、stderr);

            if(cmd_contents){
                free(cmd_contents);
            }

            buffer[i] = '\0';
            cmd_contents = (char*)malloc(sizeof(buffer) + sizeof(cmd_contents));
            strcpy(cmd_contents, buffer);

if(rc == LIBSSH2_ERROR_EAGAIN){waitsocket(sock、session); }

于 2010-02-13T08:48:57.543 に答える