乐闻世界logo
搜索文章和话题

cURL 支持哪些协议?如何使用 FTP/SFTP 传输文件?

3月6日 23:06

cURL 是一个多协议数据传输工具,支持超过 20 种协议。了解这些协议的支持情况对于选择合适的传输方式非常重要。

cURL 支持的协议

协议用途示例 URL
HTTP/HTTPSWeb 请求http://example.com
FTP/FTPS文件传输ftp://ftp.example.com
SFTP安全文件传输sftp://user@host.com
SCP安全复制scp://user@host.com/file
TFTP简单文件传输tftp://host.com/file
LDAP/LDAPS目录服务ldap://ldap.example.com
SMTP/SMTPS邮件发送smtp://mail.example.com
POP3/POP3S邮件接收pop3://mail.example.com
IMAP/IMAPS邮件访问imap://mail.example.com
RTSP/RTMP流媒体rtsp://stream.example.com
FILE本地文件file:///path/to/file
GOPHER早期互联网协议gopher://gopher.example.com
DICT字典协议dict://dict.org/d:word
TELNET远程登录telnet://host.com

FTP 文件传输

FTP 下载

bash
# 匿名 FTP 下载 curl ftp://ftp.example.com/pub/file.txt -o file.txt # 认证 FTP 下载 curl -u "username:password" \ ftp://ftp.example.com/remote/file.txt \ -o file.txt # 下载目录列表 curl ftp://ftp.example.com/pub/ # 被动模式 FTP curl --ftp-skip-pasv-ip \ ftp://ftp.example.com/file.txt

FTP 上传

bash
# 上传文件到 FTP curl -u "username:password" \ -T local-file.txt \ ftp://ftp.example.com/remote/path/ # 上传并指定文件名 curl -u "username:password" \ -T local-file.txt \ ftp://ftp.example.com/remote/renamed-file.txt # 创建目录并上传 curl -u "username:password" \ --ftp-create-dirs \ -T file.txt \ ftp://ftp.example.com/new-folder/file.txt

FTP 高级操作

bash
# 删除远程文件 curl -u "username:password" \ -Q "DELE remote-file.txt" \ ftp://ftp.example.com/ # 重命名文件 curl -u "username:password" \ -Q "RNFR old-name.txt" \ -Q "RNTO new-name.txt" \ ftp://ftp.example.com/ # 列出详细文件信息 curl -u "username:password" \ ftp://ftp.example.com/ -l # 使用 FTPS(FTP over SSL) curl -u "username:password" \ ftps://ftp.example.com/file.txt # 主动模式 FTP curl --ftp-port - \ -u "username:password" \ ftp://ftp.example.com/file.txt

SFTP 文件传输

SFTP 下载

bash
# 基本 SFTP 下载 curl -u "username:password" \ sftp://sftp.example.com/remote/file.txt \ -o file.txt # 使用密钥认证(推荐) curl -u "username:" \ --key ~/.ssh/id_rsa \ --pubkey ~/.ssh/id_rsa.pub \ sftp://sftp.example.com/remote/file.txt \ -o file.txt # 指定端口 curl -u "username:password" \ sftp://sftp.example.com:2222/remote/file.txt # 下载目录(需要配合 tar) ssh user@sftp.example.com "tar czf - /remote/folder" | tar xzf -

SFTP 上传

bash
# 上传文件到 SFTP curl -u "username:password" \ -T local-file.txt \ sftp://sftp.example.com/remote/path/ # 使用密钥上传 curl -u "username:" \ --key ~/.ssh/id_rsa \ -T local-file.txt \ sftp://sftp.example.com/remote/ # 上传多个文件 curl -u "username:password" \ -T "file1.txt" \ -T "file2.txt" \ sftp://sftp.example.com/remote/

SCP 文件传输

bash
# SCP 下载 curl -u "username:password" \ scp://host.example.com/remote/file.txt \ -o file.txt # SCP 上传 curl -u "username:password" \ -T local-file.txt \ scp://host.example.com/remote/ # 使用密钥 curl -u "username:" \ --key ~/.ssh/id_rsa \ scp://host.example.com/remote/file.txt

协议对比

特性FTPFTPSSFTPSCP
加密
端口2121/9902222
认证密码密码/证书密码/密钥密码/密钥
防火墙友好⚠️
推荐场景内网企业环境通用简单传输

邮件协议示例

bash
# SMTP 发送邮件 curl -v smtp://smtp.gmail.com:587 \ --mail-from "sender@example.com" \ --mail-rcpt "recipient@example.com" \ --upload-file email.txt \ --user "username:password" # POP3 读取邮件 curl -u "username:password" \ pop3://pop.gmail.com # IMAP 列出邮箱 curl -u "username:password" \ imap://imap.gmail.com

实用脚本

bash
#!/bin/bash # FTP/SFTP 自动备份脚本 SOURCE="/local/path/to/backup" DEST="sftp://backup.example.com/backups/" USER="backup_user" KEY="~/.ssh/backup_key" DATE=$(date +%Y%m%d) ARCHIVE="backup-${DATE}.tar.gz" # 创建压缩包 tar czf "/tmp/${ARCHIVE}" -C "$(dirname $SOURCE)" "$(basename $SOURCE)" # 上传到 SFTP curl -u "${USER}:" \ --key "$KEY" \ -T "/tmp/${ARCHIVE}" \ "${DEST}${ARCHIVE}" # 清理本地临时文件 rm "/tmp/${ARCHIVE}" echo "Backup completed: ${ARCHIVE}"

常见问题

bash
# 问题 1:FTP 被动模式失败 # 解决:使用 --ftp-skip-pasv-ip curl --ftp-skip-pasv-ip ftp://ftp.example.com/file # 问题 2:SFTP 密钥权限错误 # 解决:确保私钥权限正确 chmod 600 ~/.ssh/id_rsa # 问题 3:SSL 证书验证失败 # 解决:指定 CA 或跳过验证(测试用) curl --cacert /path/to/ca.crt ftps://ftp.example.com/file curl -k ftps://ftp.example.com/file # 不安全 # 问题 4:文件名包含空格 # 解决:使用 URL 编码或引号 curl -u "user:pass" "ftp://ftp.example.com/file%20name.txt"

标签:cURL