2017-04-20 35 views
0

免责声明:谷歌-fu'ing通过这个我的方式...无法打开远程文件:SCP状态代码1D无效libssh 0.7.3

我想知道如果任何人都能够去看看在这&指向我在正确的方向&谢谢你的任何人能够解决......我刚刚从想法的新鲜...

与长度变量关联的文件大小函数是这个;

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

我从这件作品中看到了“scp状态码1d无效”

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]是唯一返回的内容,没有横幅等,它是一个已知的主机,共享的pub文件。对于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; 
}