cURL 是一个多协议数据传输工具,支持超过 20 种协议。了解这些协议的支持情况对于选择合适的传输方式非常重要。
cURL 支持的协议
| 协议 | 用途 | 示例 URL |
|---|---|---|
| HTTP/HTTPS | Web 请求 | 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
协议对比
| 特性 | FTP | FTPS | SFTP | SCP |
|---|---|---|---|---|
| 加密 | ❌ | ✅ | ✅ | ✅ |
| 端口 | 21 | 21/990 | 22 | 22 |
| 认证 | 密码 | 密码/证书 | 密码/密钥 | 密码/密钥 |
| 防火墙友好 | ❌ | ⚠️ | ✅ | ✅ |
| 推荐场景 | 内网 | 企业环境 | 通用 | 简单传输 |
邮件协议示例
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"