1、目标
近期项目做文件传输都是基于sftp,这里我选择了libcurl。不过要注意的是libcurl默认下并不支持SFTP,需要在编译的时候添加libssh2依赖项,而libssh2又依赖于openssl和zlib。注意讲一下目录,下载上传网上很多。
2、编译库
由于一些基建库的原因,我自己的c++环境用的是vs2010, 我的curl库。
这里我推荐链接: 柴承训编译libcurl的博客
3、获取linux目录
这里都是基于libcurl官方提供的example。
注意点1
curl_easy_setopt(curl, CURLOPT_URL,“sftp://127.0.0.1:22/home/”);
sftp://127.0.0.1:22/usr/ 定位到目录一定记得加斜杠,否则指定就不同了。
注意点2
是否使用 CURLOPT_DIRLISTONLY,如果你单纯的只想拿到目录的名称请调用curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1);
如果,你想获取所有的信息,请千万不要加这一句;
调用CURLOPT_DIRLISTONLY结果:
..
user
docker
chry
dmdba
.
不调用CURLOPT_DIRLISTONLY结果:
drwxrwxr-x 25 root root 4096 Nov 3 10:45 ..
drwx------ 42 user user 4096 Nov 17 19:11 user
drwxrwxrwx 11 root root 4096 Nov 8 16:59 docker
drwxr-xr-x 3 root root 4096 Jul 28 17:29 chry
drwxr-xr-x 3 root root 4096 Nov 2 13:53 dmdba
drwxr-xr-x 6 root root 4096 Nov 2 11:13 .
整体代码如下:
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Gets a file using an SFTP URL.
* </DESC>
*/
#include <stdio.h>
#include <curl/curl.h>
/* define this to switch off the use of ssh-agent in this program */
#undef DISABLE_SSH_AGENT
/*
* This is an example showing how to get a single file from an SFTP server.
* It delays the actual destination file creation until the first write
* callback so that it will not create an empty file in case the remote file
* does not exist or something else fails.
*/
struct FtpFile {
const char* filename;
FILE* stream;
};
static size_t my_fwrite(void* buffer, size_t size, size_t nmemb,
void* stream)
{
struct FtpFile* out = (struct FtpFile*)stream;
if (!out->stream) {
/* open file for writing */
out->stream = fopen(out->filename, "wb");
if (!out->stream)
return -1; /* failure, cannot open file to write */
}
return fwrite(buffer, size, nmemb, out->stream);
}
int main(void)
{
CURL* curl;
CURLcode res;
struct FtpFile ftpfile = {
"D:\\download\\1.txt", /* name to store the file as if successful */
NULL
};
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
/*
* You better replace the URL with one that works!
*/
curl_easy_setopt(curl, CURLOPT_URL,
"sftp://127.0.0.1:22/home/");
curl_easy_setopt(curl, CURLOPT_USERPWD, "username:password");
/* Define our callback to get called when there's data to be written */
/* Enable SSL/TLS for FTP, pick one of:
CURLUSESSL_TRY - try using SSL, proceed anyway otherwise
CURLUSESSL_CONTROL - SSL for the control connection or fail
CURLUSESSL_ALL - SSL for all communication or fail
*/
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_TRY);
/* talk a lot */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
/* DIRLISTONLY */
//curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
/* Set a pointer to our struct to pass to the callback */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
#ifndef DISABLE_SSH_AGENT
/* We activate ssh agent. For this to work you need
to have ssh-agent running (type set | grep SSH_AGENT to check) or
pageant on Windows (there is an icon in systray if so) */
curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_PASSWORD);
#endif
/* Switch on full protocol/debug output */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
if (CURLE_OK != res) {
/* we failed */
fprintf(stderr, "curl told us %d\n", res);
}
}
if (ftpfile.stream)
fclose(ftpfile.stream); /* close the local file */
curl_global_cleanup();
return 0;
}
4、下载上传文件
这个网上的例子很多,推荐一个,SFTP+FTP上传下载。