使用curl 命令上传下载FTP
CURL命令
1、列出ftp服务器上的目录列表:
curl ftp://www.xxx.com/ --user name:passwd
curl ftp://www.xxx.com/ –u name:passwd #简洁写法
curl ftp://name:passwd@www.xxx.com #简洁写法22、只列出目录,不显示进度条
curl ftp://www.xxx.com –u name:passwd -s3、下载一个文件:
curl ftp://www.xxx.com/size.zip –u name:passwd -o size.zip4、上载一个文件:
curl –u name:passwd -T size.mp3 ftp://www.xxx.com/mp3/5、从服务器上删除文件(使用curl传递ftp协议的DELE命令):
curl –u name:passwd ftp://www.xxx.com/ -X 'DELE mp3/size.mp3'6、另外curl不支持递归下载,不过可以用数组方式下载文件,比如我们要下载1-10.gif连续命名的文件:
curl –u name:passwd ftp://www.xxx.com/img/[1-10].gif –O #O字母大写7、要连续下载多个文件:
curl –u name:passwd ftp://www.xxx.com/img/[one,two,three].jpg –O #O字母大写
# 上传 aa.txt 文件到 FTP 指定目录下(目录必须以"/"结尾), 并以 原文件名 命名保存
curl ftp://192.168.0.100/aaDir/ -u "user:passwd" -T "aa.txt"
# 上传 aa.txt 文件到 FTP 指定目录下, 并以 bb.txt 命名保存
curl ftp://192.168.0.100/aaDir/bb.txt -u "user:passwd" -T "aa.txt"
# 同时上传多个文件
curl ftp://192.168.0.100/aaDir/ -u "user:passwd" -T "{aa.txt,bb.txt}"
# 下载 FTP 指定文件 /aaDir/aa.txt, 以原文件名命名保存到当前目录
curl ftp://192.168.0.100/aaDir/aa.txt -u "user:passwd" -O
# 下载 FTP 指定文件 /aaDir/aa.txt, 以 bb.txt 命名保存
curl ftp://192.168.0.100/aaDir/aa.txt -u "user:passwd" -o bb.txt
- 单条命令:
curl [-options] <ftpUrl> -X "FTP命令"
- 多条命令:
curl [-options] <ftpUrl> -Q "FTP命令" -Q "FTP命令"
#
# 创建文件夹, 在 /aaDir/ 目录(目录必须以"/"结尾)下创建 bbDir 文件夹
#curl -u "user:passwd" ftp://192.168.0.100/aaDir/ -X "MKD bbDir"
# 删除文件夹, 删除 /aaDir/ 目录下的 bbDir 文件夹(文件夹必须为空)
#curl -u "user:passwd" ftp://192.168.0.100/aaDir/ -X "RMD bbDir"
# 删除文件, 删除 /aaDir/ 目录下的 aa.txt 文件
#curl -u "user:passwd" ftp://192.168.0.100/aaDir/ -X "DELE aa.txt"
# 重命名, 重命名需要连续执行两条命令, 使用两个 -Q 参数连续执行两条命令(必须先 RNFR, 后 RNTO)
#curl -u "user:passwd" ftp://192.168.0.100/ -Q "RNFR OldPath" -Q "RNTO NewPath"
利用libcurl编写程序与ftp服务器通信,进行文件上传或下载。其实在命令行下直接使用curl命令就可以完成这些功能,但是需要获取上传下载行为的状态以便更好地控制.
//ftp-manager.h
#ifndef _FTP_MANAGER_H_
#define _FTP_MANAGER_H_/*FTP OPERATION CODE*/
typedef enum FTP_STATE
{FTP_UPLOAD_SUCCESS,FTP_UPLOAD_FAILED,FTP_DOWNLOAD_SUCCESS,FTP_DOWNLOAD_FAILED
}FTP_STATE;/*FTP OPERATIONS OPTIONS*/
typedef struct FTP_OPT
{char *url; /*url of ftp*/char *user_key; /*username:password*/char *file; /*filepath*/
}FTP_OPT;#ifdef __cplusplusextern "C" {
#endif/*upload file to ftp server*/
FTP_STATE ftp_upload(const FTP_OPT ftp_option);/*download file from ftp server*/
FTP_STATE ftp_download(const FTP_OPT ftp_option);#ifdef __cplusplus}
#endif#endif
//ftp-manager.c
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>#include "ftp-manager.h"/*****************util api******************/
int get_file_size(FILE *file)
{int size = 0;fseek(file, 0L, SEEK_END);size = ftell(file);fseek(file, 0L, SEEK_SET);return size;
}/******************curl api****************/
CURL *curl_init()
{curl_global_init(CURL_GLOBAL_DEFAULT); CURL *curl = curl_easy_init();if(NULL == curl){fprintf(stderr, "Init curl failed.\n");exit(1);}return curl;
}void curl_set_upload_opt(CURL *curl, const char *url, const char *user_key, FILE *file)
{curl_easy_setopt(curl, CURLOPT_URL, url);curl_easy_setopt(curl, CURLOPT_USERPWD, user_key);curl_easy_setopt(curl, CURLOPT_READDATA, file); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);curl_easy_setopt(curl, CURLOPT_INFILESIZE, get_file_size(file));curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1);
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
}void curl_set_download_opt(CURL *curl, const char *url, const char *user_key, FILE *file)
{curl_easy_setopt(curl, CURLOPT_URL, url);curl_easy_setopt(curl, CURLOPT_USERPWD, user_key);curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
}void curl_exit(CURL *curl)
{curl_easy_cleanup(curl);curl_global_cleanup();
}CURLcode curl_perform(CURL *curl)
{CURLcode ret = curl_easy_perform(curl);if(ret != 0){fprintf(stderr, "Perform curl failed.\n");curl_exit(curl);exit(1);}return ret;
}/****************ftp upload & download api******************/
FTP_STATE ftp_upload(const FTP_OPT ftp_option)
{FTP_STATE state;CURL *curl;;FILE *fp = fopen(ftp_option.file, "r");if(NULL == fp){fprintf(stderr, "Open file failed at %s:%d\n", __FILE__, __LINE__);return FTP_UPLOAD_FAILED;}curl = curl_init();curl_set_upload_opt(curl, ftp_option.url, ftp_option.user_key, fp);if(CURLE_OK == curl_perform(curl))state = FTP_UPLOAD_SUCCESS;elsestate = FTP_UPLOAD_FAILED;curl_exit(curl);fclose(fp);return state;
}FTP_STATE ftp_download(const FTP_OPT ftp_option)
{FTP_STATE state;CURL *curl;FILE *fp = fopen(ftp_option.file, "w");if(NULL == fp){fprintf(stderr, "Open file failed at %s:%d\n", __FILE__, __LINE__);return FTP_UPLOAD_FAILED;}curl = curl_init();curl_set_download_opt(curl, ftp_option.url, ftp_option.user_key, fp);if(CURLE_OK == curl_perform(curl))state = FTP_DOWNLOAD_SUCCESS;elsestate = FTP_DOWNLOAD_FAILED;curl_exit(curl);fclose(fp);return state;
}
//test.c
#include <stdio.h>#include "ftp-manager.h"int main()
{FTP_OPT ftp_opt;ftp_opt.url = "ftp://127.0.0.1//var/ftp/upload.txt";ftp_opt.user_key = "xxx:xxx";ftp_opt.file = "/home/xxx/project/ftpManager/upload.txt";if(FTP_UPLOAD_SUCCESS == ftp_upload(ftp_opt))printf("Upload success.\n");elseprintf("Upload failed.\n");ftp_opt.url = "ftp://127.0.0.1//var/ftp/download.txt";ftp_opt.file = "/home/xxx/project/ftpManager/download.txt";if(FTP_DOWNLOAD_SUCCESS == ftp_download(ftp_opt))printf("Download success.\n");elseprintf("Download failed.\n");return 0;
}
使用curl 命令上传下载FTP相关推荐
- linux上传文件权限命令,Linux 普通用户拿到root权限及使用szrz命令上传下载文件...
1.如何拿到root权限 在shell里如果看到你的命令输入行最前面显示的是 $符号,说明目前账号只有系统的普通权限. 输入:sudo su 这时能看到shell的输入行最前面已经显示的是#号,说明已 ...
- 支持国产ARM64架构部署,支持使用rz、sz命令上传下载文件,JumpServer堡垒机v2.12.0发布
支持国产ARM64架构部署,JumpServer堡垒机v2.12.0发布 7月19日,JumpServer开源堡垒机正式发布v2.12.0版本.在这一版本中,JumpServer新增支持ARM64架构 ...
- Linux使用SSH上传下载文件,Linux下通过ssh scp命令上传下载文件
Linux下通过ssh scp命令上传下载文件. 1.上传本地文件到服务器 scp /path/filename username@servername:/path/ 例如 scp /var/tale ...
- Linux安装FTP及使用python上传下载ftp
参考 https://www.cnblogs.com/mingforyou/p/4103022.html 一.安装及配置 1.直接使用yum安装 yum -y install vsftpd 2.配置文 ...
- 怎样在SecureCRT的shell里用命令上传下载文件(转)
SecureCRT记住密码的功能容易设置,于是偶这懒人,后来习惯了用SecureCRT,但其上传文件功能偶一直没弄明白过.之前一直用的是pshell,因为pshell有个功能强大的secureftp功 ...
- Linux 普通用户拿到root权限及使用szrz命令上传下载文件
1.如何拿到root权限 在shell里如果看到你的命令输入行最前面显示的是 $符号,说明目前账号只有系统的普通权限. 输入:sudo su 这时能看到shell的输入行最前面已经显示的是#号,说明已 ...
- scp命令Linux文件上传与下载,Linux利用scp命令上传下载文件
标签:上传下载 目次 file nbsp bsp test www 文件下载 上传 1.从效劳器下载文件 scp username@servername:/path/f ...
- linux ftp上传下载文件,Linux下ftp命令上传下载文件
命令行下连接ftp服务器 方式一: 默认端口 ftp hostname 方式二: 指定端口 [ec2-user@ip-99-240-80-144 ~]$ ftp ftp> open 99.240 ...
- linux shell 上传,下载ftp文件
#####从ftp服务器上的/home/data 到 本地的/home/databackup#### #!/bin/bash ftp -n<<! open 192.168.1.171 us ...
最新文章
- debian10 简单搭建squid
- UTF-8 BOM(EF BB BF)
- 基于 linux 平台的 libpcap 源代码分析
- 使用Jest测试JavaScript (入门篇)
- boost::gil::color_spaces_are_compatible用法的测试程序
- linux tar压缩排除某个文件夹或者文件
- differential forms
- 浅谈算法(简单算法)
- db2 linux 64位下载,Redhat6.2 64位 安装DB2V10.5
- Hive采集数据框架flume
- 搭建结构光三维扫描仪(基于TI资料,PointGrey相机+DLP投影仪搭建成的)
- 值钱的木头——前缀和思想
- 小米平板2刷哪个系统更流畅_Windows 10版小米平板2简测:流畅度不及自家MIUI版...
- python 因果推断_微软因果推理框架DoWhy入门
- 03虚幻4 场景中的基础光源和视觉效果
- 展厅万能播控软件,中控视频播放器!展馆专用!
- Win10 中文输入法关闭 Ctrl+Shift+B 快捷键
- java 微信公众号之授权登录
- 碳排放权、碳交易日度数据分部门、能源碳排放面板数据
- 【科学的尽头是神学】祖师爷坐镇