我在c ++代码中使用curl库。我想使用sftp和用户身份验证将文件上传到服务器。虽然我设法从Windows命令行使用
curl -k -T f:/temp/openvpn-config.zip -u user:password sftp://fabrika/tmp/
Run Code Online (Sandbox Code Playgroud)
用选项调用curl
struct stat file_info;
FILE *fd = fopen(f:/temp/openvpn-config.zip,"rb");
int res_st = fstat(fileno(fd), &file_info);
fclose(fd);
ffile.open(f:/temp/openvpn-config.zip,ios::in | ios::binary);
curl_easy_setopt(h_curl, CURLOPT_URL, "sftp://fabrika/tmp/");
curl_easy_setopt(h_curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(h_curl, CURLOPT_READDATA, (void*)&ffile);
curl_easy_setopt(h_curl, CURLOPT_READFUNCTION, &ReadCallback);
curl_easy_setopt(h_curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
curl_easy_setopt(h_curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(h_curl, CURLOPT_USERPWD, "user:password");
Run Code Online (Sandbox Code Playgroud)
给我错误代码79,将其翻译为“ ssh层中的错误”。我检查了fstat结果,传递给curl的文件大小正确。
编辑:忘记从详细模式添加调试信息,这是
Hostname was NOT found in DNS cache
Adding handle: conn: 0x4236ef0
Adding handle: send: 0
Adding handle: recv: 0
Curl_addHandleToPipeline: length: 1
- Conn 0 (0x4236ef0) send_pipe: 1, recv_pipe: 0
Trying 192.168.3.1...
Connected to fabrika (192.168.3.1) port 22 (#0)
SSH MD5 fingerprint: 9e73dade666cbbac9a82adfeffbd9f18
SSH authentication methods available: publickey,password
Using ssh public key file id_dsa.pub
Using ssh private key file id_dsa
SSH public key authentication failed: Unable to open public key file
Initialized password authentication
Authentication complete
Upload failed: Operation failed (4/-31)
Connection #0 to host fabrika left intact
Run Code Online (Sandbox Code Playgroud)
显然,我缺少命令行curl使用的某些参数,该参数可以解决问题,对于任何建议,我将不胜感激。
彼得
答:我通过curl邮件列表得到了答案,所以这里是为了防止有人像我一样被卡住。上传文件时,curl需要完整的目标路径,包括文件名。所以
curl_easy_setopt(h_curl, CURLOPT_URL, "sftp://fabrika/tmp/openvpn-config.zip");
Run Code Online (Sandbox Code Playgroud)
做到了。
| 归档时间: |
|
| 查看次数: |
4689 次 |
| 最近记录: |