目录
1 curl命令
1.1 简单说明
curl命令 是一个用于从服务器传输数据到服务器或从服务器接收数据的工具。curl以下协议:DICT、FILE、FTP、FTPS、GOPHER、GOPHERS、HTTP、HTTPS、IMAP、IMAPS、LDAP、LDAPS、MQTT、POP3、POP3S、RTMP、RTMPS、RTSP、SCP、SFTP、SMB、SMBS、SMTP、SMTPS、TELNET 或 TFTP,还支持代理支持、用户身份验证、FTP 上传、HTTP POST、SSL 连接、cookie、文件传输恢复等。
1.2 说明
使用:curl [options / URLs]
支持的URL写法:
- 可以通过在花括号中编写部分集合并引用URL来指定多个URL或URL的部分,例如:http://site.{one,two,three}.com
- 可以使用[],写一个连续数字或字母的URL,如:“ftp://ftp.example.com/file[1-100].txt”,
- “ftp://ftp.example.com/file[001-100].txt”,“ftp://ftp.example.com/file[a-z].txt”
- 不支持{}和[]嵌套,但是可以将几个放在一起使用:如:“http://example.com/archive[1996-1999]/vol[1-4]/part{a,b,c}.html”
- 可以指定一个步长计数器,以获取每N个数字或字母:如:”http://example.com/file[1-100:10].txt”,“http://example.com/file[a-z:2].txt“
- 如果使用的URL中包含特殊字符,需要使用双引号引起来:如:“http://[fe80::3%25eth0]/”
选项:
- -a, --append:对于FTP和SFTP,当用于上传时,curl会将数据追加到目标文件,而不是覆盖它。如果远程文件不存在,则会创建。但是有些SFTP服务器(包括OpenSSH)不支持。 如:curl --upload-file local --append ftp://example.com/
- –basic:在与远程主机通信时使用HTTP基础认证,这是默认设置,不需要使用。如:curl -u name:password --basic https://example.com
- -E, --cert certificate[:password]:对于HTTPS、FTPS或其他基于SSL的协议获取文件时,使用指定的客户端证书文件。如果使用Secure Transport,证书必须是PKCS#12格式;如果使用其他引擎,则必须是PEM格式。如果没有指定可选的密码,它将在终端上查询,例如:curl --cert certfile --key keyfile
- -K, --config file:指定一个配置文件。例如:
# --- Example file ---
# this is a comment
url = "example.com"
output = "curlhere.html"
user-agent = "superagent/1.0"
# and fetch another URL too
url = "example.com/docs/manpage.html"
-O
referer = "http://nowhereatall.example.com/"
# --- End of example file ---
curl --config file.txt https://example.com
-
–connect-timeout:fractional seconds:允许curl的连接所花费的最大时间(以秒为单位)。
-
–connect-to HOST1:PORT1:HOST2:PORT2:HOST1:PORT1发出请求,使用HOST2:PORT2进行连接,用于将请求定向到特定的服务器,例如:curl --connect-to example.com:443:example.net:8443 https://example.com
-
-C, --continue-at offset:从给定的偏移量处继续/恢复之前的文件传输。给定的偏移量是从源文件开始要跳过的确切字节数,然后将其传输到目标位置。
-
–create-dirs,当与 -o 或 --output 选项一起使用时,会在需要时创建必要的本地目录结构。如果 --output 文件名没有使用目录,或者它提到的目录已经存在,那么将不会创建任何目录。如:curl --create-dirs --output local/dir/file https://example.com
-
–create-file-mode mode:设置创建目录的权限,默认是0644,如:curl --create-file-mode 0777 -T localfile sftp://example.com/new
-
–dns-servers addresses:使用参数中的dns,而不是系统配置的,如:curl --dns-servers 192.168.0.1,192.168.0.2 https://example.com
-
-D, --dump-header filename:(HTTP FTP)将接收到的协议头部写入指定的文件。如果没有接收到任何头部,使用这个选项将会创建一个空文件。在 FTP 中使用时,FTP 服务器的响应行被视为“头部”,因此也会保存在那里。如:curl --dump-header store.txt https://example.com
-
-P, --ftp-port address:如何通过主动模式连接到ftp,可以通过网卡、IP和主机名。如:curl -P - ftp:/example.com,curl -P eth0 ftp:/example.com,curl -P 192.168.0.2 ftp:/example.com
-
-I, --head:仅获取头部信息,如:curl -I https://example.com
-
–ignore-content-length:http忽略头部
-
-i:在输出中包含HTTP响应头部。HTTP响应头部可以包含诸如服务器名称、cookies、文档日期、HTTP版本等信息……
-
–interface:使用指定的接口执行操作。可以是接口名称、IP地址或主机名,如:curl --interface eth0 https://example.com
-
–keepalive-time seconds:设置连接在发送保持活动探测前需要保持空闲的时间,以及单个保持活动探测之间的时间间隔
-
–limit-rate speed:指定curl在下载和上传时要使用的最大传输速率,默认是字节/秒,如:curl --limit-rate 100K https://example.com,curl --limit-rate 1000 https://example.com,curl --limit-rate 10M https://example.com
-
–local-port num/range:设置用于连接的本地端口范围,如:curl --local-port 1000-3000 https://example.com
-
-L, --location:如果服务器报告请求的页面已移动到另一个位置(通过Location:头部和3XX响应代码指示),可以向新位置重新发起请求
-
-M:显示帮助文档
-
–max-redirs num:设置最大重定向次数,一般和-L选项一起使用。默认情况下,限制设置为50次重定向。设置为-1时,可以不限制。curl --max-redirs 3 --location https://example.com
-
-m, --max-time fractional seconds :设置连接的最大时间,如:curl --max-time 10 https://example.com
-
-N, --no-buffer:禁止输出缓冲流
-
–output-dir dir:输出到指定目录,如果目录不存在则会报错,如:curl --output-dir “tmp” -O https://example.com
-
-o, --output file:将输出写入file文件,如果使用{}或[]获取了多个文档,应该引用URL,并可以在 file 指定符中使用’#'后跟一个数字。该变量将被替换为当前正在获取的URL的字符串,如:curl “http://{one,two}.example.com” -o “file_#1.txt”,curl “http://{site,host}.host[1-5].com” -o “#1_#2”,curl -o aa example.com -o bb example.net,curl example.com example.net -o aa -o bb; 也可以将输出到回收站,如:curl example.com -o /dev/null,curl example.com -o nul
-
-Z, --parallel:并行方式执行,如:curl --parallel https://example.com -o file1 https://example.com -o file2
-
-Q, --quote command:向ftp和sftp服务器发送执行命令, 如:curl --quote “DELE file” ftp://example.com/foo,可以执行的命令有如下几种:
- atime date file:设置文件的最后访问时间。
- chgrp group file:设置文件的组id
- chmod mode file:修改文件权限
- chown user file:修改文件所有者
- ln source_file target_file:设置链接
- mkdir directory_name:创建目录
- pwd:显示当前目录
- rename source target:修改文件或目录名
- rm file:删除文件
- rmdir directory:删除目录
-
-O, --remote-name:将输出写入一个本地文件,本地文件名就是远程文件名,如:curl -O https://example.com/filename
-
-R, --remote-time:如果远程文件的时间戳可用,则使本地文件具有相同的的时间戳。
-
-S,–show-error:当与 -s, --silent 一起使用时,如果curl失败,它会显示一个错误消息,如:curl --show-error --silent https://example.com
-
-s, --silent:静默或安静模式。不显示进度仪表盘或错误消息,如:curl -s https://example.com
-
-Y, --speed-limit speed:如果下载速度低于给定的速度(每秒字节数)达到速度时间秒,则下载会被取消。速度时间是通过 -y, --speed-time 设置的,如果没有设置,则默认为30。
-
-y, --speed-time seconds:如果在速度时间期内下载速度低于速度限制(每秒字节数),则下载将被取消。如果使用了–speed-time,则默认的速度限制将是1,除非通过-Y, --speed-limit设置了其他值。如:curl --speed-limit 300 --speed-time 10 https://example.com
-
–stderr file:会将错误信息输出到指定文件,如果没问题,则文件没有任务内容。如:curl --stderr output.txt https://example.com
-
-z, --time-cond time:(HTTP FTP) 请求一个在给定的时间和日期之后修改过的文件,或者一个在那时之前修改过的文件。日期表达式 可以是各种日期字符串,如果它不匹配任何内部格式,它将被视为文件名,并尝试从 文件 获取修改日期(mtime)。如:curl -z “Wed 01 Sep 2021 12:18:00” https://example.com,curl -z “-Wed 01 Sep 2021 12:18:00” https://example.com,curl -z file https://example.com
-
–trace-ascii file:将完整跟踪转储,包括描述性信息,输出到给定的文件中。如:curl --trace-ascii log.txt http://www.baidu.com
-
–trace-time:会在curl显示的每个跟踪或详细信息行前添加一个时间戳,如:curl --trace-time --trace-ascii log.txt http://www.baidu.com。默认是没有。可以和上面进行对比。
-
–trace file:启用对所有传入和 outgoing数据的完整跟踪转储,包括描述性信息,输出到给定的文件中。如:curl --trace log.txt https://example.com
-
-T, --upload-file file:将指定的本地文件传输到远程 URL。可以上传单个文件,也可以是多个文件。curl -T file https://example.com,curl -T “img[1-1000].png” ftp://ftp.example.com/,curl --upload-file “{file1,file2}” https://example.com
-
-u, --user user:password:指定用于服务器身份验证的用户名和密码。curl -u user:secret https://example.com
-
-v, --verbose:显示详细信息,curl --verbose https://example.com
-
-w, --write-out format:在传输完成后在标准输出上显示信息。如:curl -w ‘%{http_code}\n’ https://example.com。常用的信息有:
- content_type:请求文档的内容类型
- errormsg:错误信息
- exitcode:退出码
- filename_effective:最终写入的文件名
- ftp_entry_path:登录到ftp时的最初路径
- http_code
- http_connect
- http_version:HTTP版本
- json:包含所有可用键的JSON对象
- local_ip:结束最近一次连接时的本地IP
- local_port:结束最近一次连接时的端口
- method:最近一次http 请求时使用的http method
- num_connects:最近传输过程中的连接数
- num_headers:最近一次请求中的响应头数量(每次重定向时重新开始计数)
- num_redirects:请求过程中跟随的重定向次数
- onerror:如果传输返回了非零错误,则只显示输出的其余部分
- proxy_ssl_verify_result:请求的HTTPS代理的SSL对等证书验证的结果
- remote_ip:最近一次连接中远程端的ip地址,可以是ipv4也可以是ipv6
- remote_port:最近连接中远程端的端口号
- response_code:最近一次传输中找到的数值响应码
- scheme:实际使用的URL方案(有时称为协议)
- size_download:下载的总字节数。这是传输的主体/数据的大小,不包括头部。
- size_header:下载的头部总字节数。
- size_request:HTTP请求中发送的总字节数。
- size_upload:上传的总字节数。这是传输的主体/数据的大小,不包括头部
- speed_download:平均下载速度,以每秒字节数表示
- speed_upload:平均上传速度,以每秒字节数表示
- ssl_verify_result:请求的SSL对等证书验证的结果。0表示验证成功
- time_appconnect:从开始到与远程主机完成SSL/SSH/等连接/握手所花费的时间(以秒为单位)
- time_connect:从开始到与远程主机(或代理)完成TCP连接所花费的时间(以秒为单位)。
- time_namelookup:从开始到名称解析完成所花费的时间(以秒为单位)
- time_pretransfer:从开始到文件传输即将开始所花费的时间(以秒为单位)
- time_redirect:所有重定向步骤(包括名称查找、连接、预传输和传输)在最终事务开始之前所花费的时间(以秒为单位)。time_redirect显示了多个重定向的完整执行时间。
- time_starttransfer:从开始到即将传输第一个字节所花费的时间(以秒为单位)
- time_total:整个操作持续的总时间(以秒为单位)
- url:获取的URL
- urlnum:此次传输的URL索引号,从0开始。解扩展的URL与原始通配符URL共享相同的索引号。
- url_effective:最后获取的URL
1.3 案例
1、获取头部信息
# 仅获取头部信息
curl -I http://www.baidu.com
##### 输出:
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Mon, 11 Mar 2024 08:44:36 GMT
Etag: "575e1f60-115"
Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
# 获取包括头部信息在内的所有信息
curl -i http://www.baidu.com
2、将输出写入到文件中
### 1、
curl -L "http://www.{bilibili,baidu}.com" -o "#1.txt"
### 输出
[1/2]: http://www.bilibili.com --> bilibili.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 239 100 239 0 0 5431 0 --:--:-- --:--:-- --:--:-- 5558
100 1334 0 1334 0 0 6335 0 --:--:-- --:--:-- --:--:-- 24254
[2/2]: http://www.baidu.com --> baidu.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2381 100 2381 0 0 97414 0 --:--:-- --:--:-- --:--:-- 99208
# 查看
ls
baidu.txt bilibili.txt
##### 2、
root@con01:~/test# curl -L -o aa www.baidu.com -o bb www.bilibili.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2381 100 2381 0 0 56432 0 --:--:-- --:--:-- --:--:-- 58073
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 239 100 239 0 0 2507 0 --:--:-- --:--:-- --:--:-- 2515
100 1334 0 1334 0 0 1434 0 --:--:-- --:--:-- --:--:-- 0
root@con01:~/test# ls
aa bb
##### 3、输出到回收站
root@con01:~/test# curl www.baidu.com -o /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2381 100 2381 0 0 94487 0 --:--:-- --:--:-- --:--:-- 95240
root@con01:~/test# ls
aa bb
# -O(大o):链接对象不能是一个文件
root@con01:~/test# curl -O https://nginx.org
curl: Remote file name has no length!
curl: (23) Failed writing received data to disk/application
root@con01:~/test# curl -O https://nginx.org/download/nginx-1.22.1.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1048k 100 1048k 0 0 176k 0 0:00:05 0:00:05 --:--:-- 213k
root@con01:~/test# ls
aa bb nginx-1.22.1.tar.gz
# -O(大o):指定文件名时报错
root@con01:~/test# curl -O a https://nginx.org/download/nginx-1.22.1.tar.gz
curl: Remote file name has no length!
curl: (23) Failed writing received data to disk/application
root@con01:~/test#
root@con01:~/test# curl -o a https://nginx.org/download/nginx-1.22.1.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1048k 100 1048k 0 0 506k 0 0:00:02 0:00:02 --:--:-- 506k
root@con01:~/test# ls
a aa bb nginx-1.22.1.tar.gz
root@con01:~/test#
输出参数解释:
- % Total: 已经下载的总数据的百分比。
- % Received: 最近接收到的数据的百分比。
- % Xferd: 已经传输的数据的百分比。
- Dload: 下载速度(以字节/秒为单位)。
- Upload: 上传速度(以字节/秒为单位)。这个在你只是下载数据时通常是0。
- Total: 总的传输时间。
- Spent: 已经花费的时间。
- Left: 预计剩余时间。
- Current: 当前的速度。
- Average Speed: 平均速度。
总结:-O和-o的区别
- -O(大O) :会使用远程文件的名称来命名本地文件。
- -o :允许指定一个本地文件名来保存下载的数据。
- -O(大O):只能是一个文件
- -o:对象可以是一个文件,也可以是一个URL
3、并行执行
root@con01:~/test# curl -Z -L -o aa www.baidu.com -o bb www.bilibili.com
DL% UL% Dled Uled Xfers Live Qd Total Current Left Speed
156 -- 3715 0 2 0 0 --:--:-- --:--:-- --:--:-- 5145
root@con01:~/test# ls
aa bb
root@con01:~/test#
### 或者
curl --parallel -L -o aa www.baidu.com -o bb www.bilibili.com
以下是一些参数解释:
- DL%: 下载进度的百分比。
- UL%: 上传进度的百分比(通常下载时这个值为0,除非你同时也在上传数据)。
- Dled: 已经下载的数据量(以字节为单位)。
- Uled: 已经上传的数据量(以字节为单位)。
- Xfers: 当前正在进行的传输数量。
- Live: 当前活动的连接数。
- Qd: 在队列中等待的连接数(这个参数在 curl 的标准输出中并不常见,可能是某个特定版本或配置的输出)。
- Total: 总共需要传输的数据量(以字节为单位)。
- Current: 当前的速度(以字节/秒为单位)。
- Left: 预计剩余时间。
- Speed: 平均速度(以字节/秒为单位)。
4、使用远程文件的时间戳
root@con01:~/test# curl -o abc http://www.baidu.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2381 100 2381 0 0 94237 0 --:--:-- --:--:-- --:--:-- 95240
root@con01:~/test# curl --remote-time -o abcd http://www.baidu.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2381 100 2381 0 0 70926 0 --:--:-- --:--:-- --:--:-- 72151
root@con01:~/test# ll abc abcd
-rw-r--r-- 1 root root 2381 Mar 11 10:14 abc
-rw-r--r-- 1 root root 2381 Jan 23 2017 abcd
5、不显示进度仪表盘
root@con01:~/test# curl -s -o a1 http://www.baidu.com
root@con01:~/test# ls
a a1 aa abc abcd bb nginx-1.22.1.tar.gz
root@con01:~/test#
6、将错误输出到文件中
root@con01:~/test# curl --stderr output.txt http://www.baidu.com
# 以上执行没有报错,则会正常输出,同时文件中没有内容
root@con01:~/test# cat output.txt
root@con01:~/test# curl --stderr output.txt -O a http://www.baidu.com
root@con01:~/test# cat output.txt
curl: Remote file name has no length!
curl: (23) Failed writing received data to disk/application
root@con01:~/test#
7、显示传输中的部分信息
# 如显示http code
curl -w '%{http_code}\n' http://www.baidu.com
# 其他信息可以参考上面的说明