Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
libssh+;iPhone实现,多个命令按顺序执行_Iphone_Cocoa Touch_Xcode_Libssl_Remote Client - Fatal编程技术网

libssh+;iPhone实现,多个命令按顺序执行

libssh+;iPhone实现,多个命令按顺序执行,iphone,cocoa-touch,xcode,libssl,remote-client,Iphone,Cocoa Touch,Xcode,Libssl,Remote Client,我的问题似乎很奇怪,我没有得到足够的帮助,但我回来了。 我还有一个奇怪的问题需要在紧急情况下解决 我正在开发一个iPhone应用程序。它使用libssh 2通过iPhone在远程主机上执行命令。如果我用单个命令执行所有的方法和命令,那么它们都可以正常工作 我的问题是, 考虑一系列命令, pwd =>o/p将为/Users/mac01 cd xyz =>不作为o/p pwd =>o/p将为/Users/mac01/xyz 所以,我的问题是保存已执行命令的最后状态。。。但我得到的o/p是 /User

我的问题似乎很奇怪,我没有得到足够的帮助,但我回来了。 我还有一个奇怪的问题需要在紧急情况下解决

我正在开发一个iPhone应用程序。它使用libssh 2通过iPhone在远程主机上执行命令。如果我用单个命令执行所有的方法和命令,那么它们都可以正常工作

我的问题是, 考虑一系列命令,

pwd

=>o/p将为/Users/mac01

cd xyz

=>不作为o/p

pwd

=>o/p将为/Users/mac01/xyz

所以,我的问题是保存已执行命令的最后状态。。。但我得到的o/p是 /Users/mac01 第二次pwd命令执行后,这是错误的

那么,有人能帮我解决这类问题吗。。?提前谢谢。 我正在使用libssh 2.0库

执行命令的方法:

char*cmd_exec(const char*命令行、const char*主机、const char*用户名、const char*密码、int端口){ int sock,rc,字节数=0; char*cmd_内容

if(!he)
{
    struct sockaddr_in sin;
ifdef WIN32 恩迪夫 ifndef WIN32 恩迪夫
}libssh2\u会话\u设置\u阻塞(会话,0)

字符缓冲区[0x4000]; rc=libssh2_channel_read(通道、缓冲区、sizeof(缓冲区)); 对于(i=0;i if(rc==LIBSSH2\u ERROR\u EAGAIN) { waitsocket(sock,session);
}

试试这个,也许有用。。。我还没有试过,但你可以得到足够的想法,也许是正确的解决办法

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); 
  }
libssh2\u session\u set\u阻塞(session,0);
字符缓冲区[0x4000];rc=libssh2_channel_read(通道、缓冲区、sizeof(缓冲区));
对于(i=0;i
快乐编码

    /* Init and Make Socket Connection  */
    /*  Start Socket Connection         */
    sock = socket(AF_INET, SOCK_STREAM, 0);
    fcntl(sock, F_SETFL, 0);
    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;
/*
libssh2_session_disconnect( session, "" );      // closing session
libssh2_session_free( session );                // free up memory
close( sock );                                  // closing socket
*/

return cmd_contents;
            if(cmd_contents){
                free(cmd_contents);
            }

            buffer[i] = '\0';
            cmd_contents = (char*)malloc(sizeof(buffer) + sizeof(cmd_contents));
            strcpy(cmd_contents, buffer);
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); 
  }