windows c++ (1) 基于libcurl的SFTP获取linux目录、下载、上传

2023-05-16

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上传下载。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

windows c++ (1) 基于libcurl的SFTP获取linux目录、下载、上传 的相关文章

  • 从 Java 读取 /dev/input/js0

    我正在尝试阅读 dev input js0来自Java 但我不断得到 java io IOException Invalid argument at java io FileInputStream read0 Native Method a
  • 如何为 Windows 和 macOS 更新 PyQT5 应用程序?

    我有一个使用 PyQT5 为 Windows 和 macOS 构建的应用程序 目前 用户通过单击按钮检查更新 当有可用的新更新时 我将它们重定向到浏览器到我的服务器以下载最新的 exe Windows 或 pkg macOS 问题在于 如果
  • 如何在Python中打印颜色/颜色?

    我对 Python 和 StackOverflow 都是新手 需要一点帮助 我想用 Python 打印颜色 并在 Google 上搜索过 但运气不佳 每次我都很困惑 但都没有成功 这是我输入的代码 answer input Wanna go
  • Qt + win32 + mingw 上的原生 Windows API 链接问题

    我正在尝试使用 mingw 工具集将本机 Windows API 与 Qt 结合使用 部分功能存在链接问题 会发生什么 这是 mingw 名称修改的错误吗 ifdef Q WS WIN HWND hwnd QWidget winId HDC
  • 批处理:在特定程序中打开特定文件?

    当记事本是 txt 文件的默认程序时 如何告诉 Windows 在写字板中打开 C test test txt 接受的答案对我不起作用 我不确定这是因为我试图运行的程序 还是因为路径中有空格 即使我用引号引起来 或者其他原因 不管怎样 我可
  • 干净地销毁System V共享内存段

    我在用shmget shmat and shmctl分别获取和创建共享内存段 将其附加到进程地址空间中并删除它 我想知道进程是否仍然可以使用共享内存段 即使它已被分离并要求使用删除 shmctl id IPC RMID 在一个过程中 我无法
  • 类似 wget 的 BitTorrent 客户端或库? [关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions 是否有任何
  • Scala 和 Spark:Windows 上的 Dataframe.write._

    有人设法使用 Spark 写入文件 尤其是 CSV 吗 数据框 http spark apache org docs latest api scala index html org apache spark sql Dataset在 Win
  • 如何随时暂停 pthread?

    最近我开始将 ucos ii 移植到 Ubuntu PC 上 我们知道 在pthread的回调函数中的 while 循环中简单地添加一个标志来执行暂停和恢复是不可能模拟ucos ii中的 进程 的 如下解决方案 因为ucos ii中的 进程
  • alter Windows 文件中的 krb5.ini 文件哪里去了?

    至少在 Windows XP 之前 如果您加入具有 Kerberos 领域特定设置的域 就会有一个 krb5 ini 文件 从 Vista 或 7 开始 不再需要此文件 我试图找到有关此的更多信息 但陷入困境 krb5 ini 文件中的设置
  • 在Windows Xampp上安装和使用elasticsearch php客户端

    我下载的是elasticsearch 5 1 1 zip来自https www elastic co downloads elasticsearch https www elastic co downloads elasticsearch
  • 如何修改s_client的代码?

    我正在玩apps s client c in the openssl源代码 我想进行一些更改并运行它 但是在保存文件并执行操作后 我的更改没有得到反映make all or a make 例如 我改变了sc usage函数为此 BIO pr
  • Linux shell 命令逐块读取/打印文件

    是否有一个标准的 Linux 命令可以用来逐块读取文件 例如 我有一个大小为 6kB 的文件 我想读取 打印第一个 1kB 然后是第二个 1kB 看来猫 头 尾在这种情况下不起作用 非常感谢 你可以这样做read n在循环中 while r
  • 退出失败设置错误代码

    我有一个 C Windows 程序无法设置退出代码 该程序非常复杂 我目前无法通过简单的测试用例重现该程序 我确实知道该程序调用exit 1 因为我在那一行有一个断点 在我跨过它之后 调试器 VS2010 立即打印The program p
  • 操作系统崩溃的常见原因[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我有兴趣了解 操作系统崩溃 不限于Windows崩溃 最常见的技术原因 从操作系统编程的角度 有哪些 我正在寻找一个不像 打开太多应用
  • 当我通过 shell 脚本创建 .txt 文件时,为什么文件名末尾出现问号? [复制]

    这个问题在这里已经有答案了 我正在编写一个 shell 脚本 我应该在其中创建 1 个文本文件 当我这样做时 文件名末尾出现一个问号 是什么原因 我正在 bash 脚本中尝试以下方法 1 grep ERROR a1 gt text txt
  • Linux下的C#,Process.Start()异常“没有这样的文件或目录”

    我在使用 Process 类调用程序来启动程序时遇到问题 可执行文件的层次结构位于 bin 目录下 而当前工作目录需要位于 lib 目录下 project bin a out this is what I need to call lib
  • Nasm 打印到下一行

    我用 nasm Assembly 编写了以下程序 section text global start start Input variables mov edx inLen mov ecx inMsg mov ebx 1 mov eax 4
  • Windows 中的信号处理

    在Windows中 我试图创建一个等待SIGINT信号的python进程 当它收到SIGINT时 我希望它只打印一条消息并等待SIGINT的另一次出现 所以我使用了信号处理程序 这是我的 signal receiver py 代码 impo
  • 如何使用 Windows 命令行环境查找和替换文件中的文本?

    我正在使用 Windows 命令行环境编写批处理文件脚本 并希望用另一个文件 例如 BAR 更改文件中某些文本 例如 FOO 的每次出现 最简单的方法是什么 有内置函数吗 这里的很多答案都帮助我指明了正确的方向 但是没有一个适合我 所以我发

随机推荐