通过LIBSSH SCP从远程服务器(Windows)上载文件到Linux

时间:2019-03-04 10:31:46

标签: c++ scp libssh

我正在尝试使用libssh scp库从Windows服务器上载文件。我可以在Windows服务器上成功下载文件,但是在上载文件时无法找到文件路径。下面是引发的代码和错误。该文件位于相同的Windows路径中。我是否必须使用其他方法读取Windows文件?

rssh_scp_pull_request: SCP: Warning  scp: C:/Users/testing.txt: No such file or directory


// in_remotefile = "C:/Users/testing.txt" (windows location path)
//in_localefile = "/data/misc/qmmf/testing.txt" (linux path)

int scp_recv_file(ssh_session in_session, char * in_remotefile, char * in_localfile)
{
    ssh_scp   t_scp = NULL;
    int       t_rc, t_filesize, t_filemode = -1;
    char      *t_filename, *t_buffer;

    t_scp = ssh_scp_new (in_session, SSH_SCP_READ | SSH_SCP_RECURSIVE, in_remotefile);

    if (t_scp == NULL)
    {
        fprintf(stderr, "Error allocating scp session: %s\n",
            ssh_get_error(in_session));
        return SSH_ERROR;
    }

    t_rc = ssh_scp_init(t_scp);
    if (t_rc != SSH_OK)
    {
        fprintf(stderr, "Error initializing scp session: %s\n",
            ssh_get_error(in_session));
        ssh_scp_free(t_scp);
        return t_rc;
    }

    //create ssh pull a file request
    t_rc = ssh_scp_pull_request(t_scp);
    if (t_rc != SSH_SCP_REQUEST_NEWFILE)
    {
        fprintf(stderr, "Error receiving information about file: %s\n",
            ssh_get_error(in_session));
        return SSH_ERROR;
    }

    t_filesize = ssh_scp_request_get_size(t_scp);
    t_filename = strdup(ssh_scp_request_get_filename(t_scp));
    t_filemode = ssh_scp_request_get_permissions(t_scp);

    printf("Receiving file %s, size %d, permisssions%o\n",
        t_filename, t_filesize, t_filemode);

    t_buffer = (char*)malloc(t_filesize);
    if (t_buffer == NULL)
    {
        fprintf(stderr, "Memory allocation error\n");
        return SSH_ERROR;
    }

    ssh_scp_accept_request(t_scp);
    t_rc = ssh_scp_read(t_scp, t_buffer, t_filesize);
    if (t_rc == SSH_ERROR)
    {
        fprintf(stderr, "Error receiving file data: %s\n",
            ssh_get_error(in_session));
        free(t_buffer);
        return t_rc;
    }
    printf("Done!!!!");

    int filedesc = open(in_localfile, O_WRONLY | O_CREAT);

    if (filedesc < 0) {
        return -1;
    }
    write(filedesc, t_buffer, t_filesize);
    free(buffer);
    close(filedesc);

    write buffer to file
    write(in_localfile, t_buffer, t_filesize);

    //free allocated memory
    free(t_buffer);
    free(t_filename);

    t_rc = ssh_scp_pull_request(t_scp);
    if (t_rc != SSH_SCP_REQUEST_EOF)
    {
        fprintf(stderr, "Unexpected request: %s\n",
            ssh_get_error(in_session));
        return SSH_ERROR;
    }

    ssh_scp_close(t_scp);
    ssh_scp_free(t_scp);
    return SSH_OK;
}

0 个答案:

没有答案