2017-04-20 13 views
0

면책 조항 : 사람이 모양을 가지고 갈 수있을 것인지 궁금 해서요이를 통해 내 방식 ...원격 파일을 열 수 없습니다 : SCP 상태 코드 1D 유효하지 libssh 0.7.3

을 구글 - fu'ing 이 & 올바른 방향으로 나를 가리 키십시오 & 해결할 수있는 사람을 보내 주셔서 감사합니다 ... 나는 다시 아이디어가 신선합니다 ...

길이 변수와 관련된 파일 크기 기능은 다음과 같습니다.

ifstream::pos_type filesize(const char* filename) 
{ 
    ifstream in(filename, ios::binary | ios::ate); 
    return in.tellg(); 
} 

나는이 조각에서 "scp status code 1d not valid"를 잡니다.

rc = ssh_scp_push_file(scp, "samp_batch", length, 0766); 
if (rc != SSH_OK) 
{ 
    fprintf(stderr, "Can't open remote file: %s\n", ssh_get_error(my_ssh_session)); 
    ssh_free(my_ssh_session); 
    exit(-1); 
} 

이 함수의;

int ssh() 
{ 
    ssh_session my_ssh_session; 
    ssh_scp scp; 
    int port = 22; 
    int rc; 
    int method; 
    const long int length = filesize("samp_batch"); 
    char password[128] = { 0 }; 
    my_ssh_session = ssh_new(); 
    if (my_ssh_session == NULL) 
     exit(-1); 
    ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "10.52.136.185"); 
    ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port); 
    ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "security"); 

    //connect to server 
    rc = ssh_connect(my_ssh_session); 
    if (rc != SSH_OK) 
    { 
     fprintf(stderr, "Error connecting to host: %s\n", ssh_get_error(my_ssh_session)); 
     ssh_free(my_ssh_session); 
     exit(-1); 
    } 

    //verify the servers identity 
    if (verify_knownHost(my_ssh_session) < 0) 
    { 
     fprintf(stdout, "unkown host\n"); 
     ssh_disconnect(my_ssh_session); 
     ssh_free(my_ssh_session); 
     exit(-1); 
    } 


    // Try to authenticate 
    rc = ssh_userauth_none(my_ssh_session, NULL); 
    if (rc == SSH_AUTH_ERROR) { 
     error(my_ssh_session); 
     return rc; 
    } 

    method = ssh_auth_list(my_ssh_session); 
    while (rc != SSH_AUTH_SUCCESS) { 
     // Try to authenticate with public key first 
     if (method & SSH_AUTH_METHOD_PUBLICKEY) { 
      rc = ssh_userauth_autopubkey(my_ssh_session, NULL); 
      if (rc == SSH_AUTH_ERROR) { 
       error(my_ssh_session); 
       return rc; 
      } 
      else if (rc == SSH_AUTH_SUCCESS) { 
       break; 
      } 
     } 

     // Try to authenticate with keyboard interactive"; 
     if (method & SSH_AUTH_METHOD_INTERACTIVE) { 
      rc = authenticate_kbdint(my_ssh_session, NULL); 
      if (rc == SSH_AUTH_ERROR) { 
       error(my_ssh_session); 
       return rc; 
      } 
      else if (rc == SSH_AUTH_SUCCESS) { 
       break; 
      } 
     } 

     if (ssh_getpass("Password: ", password, sizeof(password), 0, 0) < 0) { 
      return SSH_AUTH_ERROR; 
     } 

     // Try to authenticate with password 
     if (method & SSH_AUTH_METHOD_PASSWORD) { 
      rc = ssh_userauth_password(my_ssh_session, NULL, password); 
      if (rc == SSH_AUTH_ERROR) { 
       error(my_ssh_session); 
       return rc; 
      } 
      else if (rc == SSH_AUTH_SUCCESS) { 
       break; 
      } 
     } 
    } 

    //SCP samp_batch file here 
    scp = ssh_scp_new(my_ssh_session, SSH_SCP_WRITE, "/"); 
    if (scp == NULL) 
    { 
     fprintf(stderr, "Error allocating scp session: %s\n", ssh_get_error(my_ssh_session)); 
     return SSH_ERROR; 
    } 
    rc = ssh_scp_init(scp); 
    if (rc != SSH_OK) 
    { 
     fprintf(stderr, "Error initializing scp session: %s\n", ssh_get_error(my_ssh_session)); 
     ssh_scp_free(scp); 
     return rc; 
    } 

    rc = ssh_scp_push_file(scp, "samp_batch", length, 0766); 
    if (rc != SSH_OK) 
    { 
     fprintf(stderr, "Can't open remote file: %s\n", ssh_get_error(my_ssh_session)); 
     ssh_free(my_ssh_session); 
     exit(-1); 
    } 

    const char *contents = "samp_batch"; 
    rc = ssh_scp_write(scp, contents, length); 
    if (rc != SSH_OK) 
    { 
     fprintf(stderr, "Cant write to remote file: %s\n", ssh_get_error(my_ssh_session)); 
     ssh_free(my_ssh_session); 
     exit(-1); 
    } 

    ssh_scp_close(scp); 
    ssh_scp_free(scp); 
    return SSH_OK; 

    //execute remote command here 


    ssh_free(my_ssh_session); 
    return 0; 
} 

다시 말하면, 어떤 통찰력도 좋을 것입니다. 심지어 상태 코드 1d가 무엇인지 설명하는 것을 찾을 수 없습니다./

+1

이 프로세스에 사용하는 것과 동일한 사용자 및 호스트를 사용하여'ssh user @ remotehost echo foo'를 실행 해보십시오. 원격 서버에서 "foo"외에 다른 것을 얻었습니까? 특히, 환영 배너 등을 얻습니까? 또한'scp' 유틸리티를 대화식으로 실행하여이 파일을 복사 할 수 있습니까? – Kenster

+0

@Kenster "ssh user @ remotehost echo foo"의 경우 [foo] 만 반환되며 배너는 없습니다. 공유 키가있는 알려진 호스트입니다. scp에 대해, [scp samp_batch user @ remotehost :/home/user/samp_batch는 매력처럼 작동했습니다. – Snow

+0

@Kenster [ssh_scp_new (session, ssh_scp_write, "filepath")]; 달랐다! 이제 실제로 상대편에 파일을 생성하고 있습니다. D. (나는 또한 세분화 결함을 지금 얻고있다. 그러나 나는 내일 그 일을 할 것이다.) 도와 주셔서 감사합니다. – Snow

답변

0
scp = ssh_scp_new(my_ssh_session, SSH_SCP_WRITE, "/"); 
if (scp == NULL) 
{ 
    fprintf(stderr, "Error allocating scp session: %s\n", ssh_get_error(my_ssh_session)); 
    return SSH_ERROR; 
} 

해야 할 일 :

scp = ssh_scp_new(my_ssh_session, SSH_SCP_WRITE, "/home/user/"); 
if (scp == NULL) 
{ 
    fprintf(stderr, "Error allocating scp session: %s\n", ssh_get_error(my_ssh_session)); 
    return SSH_ERROR; 
}