curl实现SFTP上传下载文件

摘要:
=curl_code){printf("performfailed");curl_easy_cleanup(curl);fclose(fp);remove(filePath);return-1;}curl_easy_cleanup(curl);fclose(fp);return0;}intmain(intargc,char*argv[]){gloale_init();char*serverip="172.17.6.157";char*port="22";char*serverpath="/root/2.xml.bak";char*user="root";char*passwd="root@1234";char*savep

摘自:https://blog.csdn.net/swj9099/article/details/85292444

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <pthread.h>#include <time.h>#include <sys/stat.h>#include <signal.h>#include <curl/curl.h>#include <curl/easy.h>#include <string.h>

/*gcc curl_test.cpp -I /usr/local/curl/include/ -I /usr/local/libssh2/include/ -I /usr/local/openssl/include/  -L /usr/local/curl/lib/ -L /usr/local/libssh2/lib/ -L /usr/local/openssl/lib/ -lrt -lcurl -lssh2 -lssl -lcrypto -ldl -lz
*/

static void gloale_init(void)
{
    curl_global_init(CURL_GLOBAL_DEFAULT);
    return;
}

static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream) //回调函数
{
    curl_off_t nread;
    size_t retcode = fread(ptr, size, nmemb, (FILE*)(stream));
    nread =(curl_off_t)retcode;
    returnretcode;
}

static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
    int written = fwrite(ptr, size, nmemb, (FILE *)stream);
    returnwritten;
}

static size_t upload(const char *user, const char *passwd, const char *url, const char *path)
{
    CURL *curl =NULL;
    CURLcode res;
    //char *s3 = NULL;
    //asprintf(&s3, "%s:%s", user, passwd);
    //free(s3);

    //system("ls write_file");
    FILE *pSendFile = fopen(path, "r");
    if (pSendFile ==NULL)
    {
        printf("open failed
");
        return 1;
    }

    fseek(pSendFile, 0L, SEEK_END);

    size_t iFileSize =ftell(pSendFile);

    fseek(pSendFile, 0L, SEEK_SET);
    printf("begin easy_init
");

    curl =curl_easy_init();
    printf("curl_easy_init success
");
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL,url);
        //curl_easy_setopt(curl, CURLOPT_USERPWD, s3.c_str());
curl_easy_setopt(curl, CURLOPT_USERNAME, user);
        curl_easy_setopt(curl, CURLOPT_PASSWORD, passwd);
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
        curl_easy_setopt(curl, CURLOPT_READDATA, pSendFile);
        curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 0);
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
        curl_easy_setopt(curl, CURLOPT_INFILESIZE, iFileSize);

        printf("curl_easy_setopt success
");
        res =curl_easy_perform(curl);

        curl_easy_cleanup(curl);

        if (CURLE_OK !=res)
        {

            fprintf(stdout, "curl told us %d
", res);
        }
    }
    fclose(pSendFile);
    curl_global_cleanup();
    return 0;
}

static int download(const char *user, const char *passwd, const char *url, const char *filePath)
{
    CURL *curl =NULL;
    CURLcode curl_code;
    //char *s3 = NULL;
    //asprintf(&s3, "%s:%s", user, passwd);
    //free(s3);
curl =curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, url);
//curl_easy_setopt(curl, CURLOPT_USERPWD, s3.c_str());
curl_easy_setopt(curl, CURLOPT_USERNAME, user);
    curl_easy_setopt(curl, CURLOPT_PASSWORD, passwd);

    FILE *fp = fopen(filePath, "wb+");
    if (NULL ==fp)
    {
        curl_easy_cleanup(curl);
        printf("fopen failed
");
        return -1;
    }

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
    curl_code =curl_easy_perform(curl);
    printf("curl_code = %d
",curl_code);
    if (CURLE_OK !=curl_code)
    {
        printf("perform failed
");
        curl_easy_cleanup(curl);
        fclose(fp);
        remove(filePath);
        return -1;
    }
    curl_easy_cleanup(curl);

    fclose(fp);

    return 0;
}

int main(int argc, char *argv[])
{
    gloale_init();
    char *serverip = "172.17.6.157";
    char *port = "22";
    char *serverpath = "/root/2.xml.bak";
    char *user = "root";
    char *passwd = "root@1234";
    char *savepath = "/root/2.xml";
    char url[125] = {0};

    sprintf(url,"sftp://%s:%s/%s",serverip,port,serverpath);
    printf("url: %s
", url);
    //download(user,passwd,url,savepath);
upload(user,passwd,url,savepath);

    return 0;
}

免责声明:文章转载自《curl实现SFTP上传下载文件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇JAVA读取yml配置文件指定key下的所有内容Prometheus+grafana监控SpringBoot2应用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

sql server 数据类型

摘自:http://www.cnblogs.com/zhangwei595806165/archive/2012/02/23/2364746.html 1. 字符数据类型 字符数据类型包括varchar、char、nvarchar、nchar、text以及ntext。这些数据类型用于 存储字符数据。varchar和char类型的主要区别是数据填充。如果有一...

linux kernel的cmdline參数解析原理分析

利用工作之便,今天研究了kernel下cmdline參数解析过程。记录在此。与大家共享。转载请注明出处。谢谢。 Kernel 版本:3.4.55 Kernel启动时会解析cmdline,然后依据这些參数如console root来进行配置执行。 Cmdline是由bootloader传给kernel。如uboot。将须要传给kernel的參数做成一个...

Linux 命令详解(十一)Shell 解析 json命令jq详解

前言 在自动化部署中涉及到shell脚本需要动态读取很多配置文件,最好是json格式。 更多jq信息:http://stedolan.github.io/jq/manual/ 一、根据key获取value 语法:jq '.key' 1、单个值获取 www@TinywanAliYun:~$ cat d25341478381063d1c76e81b3a52e0...

ios7 以后准确获取iphone设备的MAC(物理地址)

通过参考 钉钉 项目,知道是通过wifi拿到路由的MAC地址。那么可不可以拿到iphone 设备的MAC 地址呢? 经过一番搜索,发现所有文章都是针对 ios 7 以前 可以拿到。 而且方法也都是同一篇文章上面的,对于ios7 以后完全没提示。 而使用网络上的方法,在 大于 ios 7 的环境下, 永远返回的MAC 为02:00:00:00:00:00 下...

cmd 下登陆ftp及相关操作

cmd 下登陆ftp及相关操作   2011-08-09 20:34:28|  分类: 小技巧|字号 订阅 一、举例 假设FTP地址为“ 61.129.83.39”(大家试验的时候不要以这个FTP去试,应该可能密码要改掉。)       1:“开始”-“运行”-输入“FTP”进去cmd界面       2.open 61.129.83.39       如...

Windows Server 2008服务器配置FTP站点的方法教程

下面我们参考一下Windows Server 2008服务器配置FTP站点的方法 1.首先,安装FTP服务 打开服务器管理器,点击角色,添加角色,如果安装过iis,角色摘要里面会有个Web服务器(IIS),点击后面的添加角色,滚动条拉到最后勾选FTP服务器,根据步骤安装。 2.添加FTP站点 打开IIS管理器,依图操作: 点击完成,至此创建站...