Linux curl 命令详解

 

命令概要

该命令设计用于在没有用户交互的情况下工作。

curl 是一个工具,用于传输来自服务器或者到服务器的数据。「向服务器传输数据或者获取来自服务器的数据」

可支持的协议有(DICT、FILE、FTP、FTPS、GOPHER、HTTP、HTTPS、IMAP、IMAPS、LDAP、LDAPS、POP3、POP3S、RTMP、RTSP、SCP、SFTP、SMTP、SMTPS、TELNET和TFTP)。

curl提供了大量有用的技巧,比如代理支持、用户身份验证、FTP上传、HTTP post、SSL连接、cookie、文件断点续传、Metalink等等。正如你将在下面看到的,这些特性的数量会让您头晕目眩!

 

访问的URL

你可以在命令行上指定任意数量的url。它们将按指定的顺序依次获取。

你可以指定多个url,或url的部分通过在花括号内编写部分集,如:

1 http://site.{one,two,three}.com
2 # 参见
3 curl http://www.zhangblog.com/2019/06/16/hexo{04,05,06}/ -I  # 查看信息

 

或者可以使用[]得到字母数字序列的序列,如:

1 ftp://ftp.numericals.com/file[1-100].txt
2 ftp://ftp.numericals.com/file[001-100].txt   # 前导用零
3 ftp://ftp.letters.com/file[a-z].txt 
4 # 参见
5 curl http://www.zhangblog.com/2019/06/16/hexo[04-06]/ -I     # 查看信息

 

不支持嵌套序列,但可以使用几个相邻的序列:

http://any.org/archive[1996-1999]/vol[1-4]/part{a,b,c}.html

 

你可以指定一个步长计数器的范围,以获得每第n个数字或字母:

http://www.numericals.com/file[1-100:10].txt 
http://www.letters.com/file[a-z:2].txt

 

如果指定URL而没有protocol:// prefix,默认为HTTP。

 

常用选项一

curl通常在操作过程中显示一个进度表,显示传输的数据量、传输速度和估计的剩余时间等。

-#, --progress-bar

将curl进度显示为一个简单的进度条;而不是标准的、具有更多信息的进度表。

1 [root@iZ28xbsfvc4Z 20190702]# curl -O http://www.zhangblog.com/2019/06/16/hexo04/index.html  # 默认的进度表
2   %  Total    %  Received  % Xferd  Average  Speed   Time    Time     Time  Current
3                                     Dload   Upload   Total   Spent    Left  Speed
4 100  97299  100  97299     0     0   186k       0 --:--:-- --:--:-- --:--:--  186k
5 [root@iZ28xbsfvc4Z 20190702]# 
6 [root@iZ28xbsfvc4Z 20190702]# curl -# -O http://www.zhangblog.com/2019/06/16/hexo04/index.html  #简单的进度条
7 ######################################################################## 100.0%

 

-0, --http1.0

(HTTP)强制curl使用HTTP 1.0发出请求,而不是使用其内部首选的HTTP 1.1。

-1, --tlsv1

(SSL)强制curl使用TLS 1.x 版本,当与远程TLS服务进行协商时。
可以使用选项 --tlsv1.0、--tlsv1.1和 --tlsv1.2来更精确地控制TLS版本(如果使用的SSL后端支持这种级别的控制)。

-2, --sslv2

(SSL)强制curl使用TLS 2 版本,当与远程TLS服务进行协商时。

-3, --sslv3

(SSL)强制curl使用TLS 3 版本,当与远程TLS服务进行协商时。

-4, --ipv4

如果curl能够将一个地址解析为多个IP版本(比如它支持ipv4和ipv6),那么这个选项告诉curl只将名称解析为IPv4地址。

-6, --ipv6

如果curl能够将一个地址解析为多个IP版本(比如它支持ipv4和ipv6),那么这个选项告诉curl只将名称解析为IPv6地址。

-a, --append

(FTP/SFTP)当在上传中使用时,这将告诉curl追加到目标文件而不是覆盖它。如果文件不存在,将创建它。注意,一些SSH服务器(包括OpenSSH)会忽略此标志。

-A, --user-agent <agent string>

(HTTP)指定要发送到HTTP服务端的User-Agent字符串。当然也可以使用 -H, --header 选项来设置。
用于模拟客户端,如:谷歌浏览器、火狐浏览器、IE 浏览器等等。

如果多次使用此选项,则将使用最后一个选项。

模仿浏览器访问

curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/75.0.3770.999" http://www.zhangblog.com/2019/06/24/domainexpire/

 

--basic

(HTTP)告诉curl使用HTTP基本身份验证。这是默认的。

 

常用选项二

(HTTP)将数据作为cookie传递给HTTP服务器。它应该是之前从服务端接收到的“Set-Cookie:”行中的数据。数据格式为“NAME1=VALUE1;NAME2 = VALUE2”。

如果行中没有使用 ‘=’ 符号,则将其视为一个文件名,用于读取先前存储的cookie行,如果它们匹配,则应在此会话中使用。
要读取cookie文件的文件格式应该是纯HTTP头文件或Netscape/Mozilla cookie文件格式。

注意:使用 -b, --cookie 指定的文件仅用作输入。文件中不会存储cookies。要存储cookies,可以使用 -c, --cookie-jar 选项,或者您甚至可以使用 -D, --dump-header 将HTTP头保存到文件中。

(HTTP)指定希望curl在完成操作后将所有cookie写入哪个文件。
Curl写之前从指定文件读取的所有cookie,以及从远程服务端接收的所有cookie。
如果没有已知的cookie,则不会写入任何文件。该文件将使用Netscape cookie文件格式编写。如果你将文件名设置为单个破折号 “-” ,cookie将被标准输出。

该命令行选项将激活cookie引擎,使curl记录并使用cookies。激活它的另一种方法是使用 -b, --cookie 选项。

如果不能创建或写入cookie jar,那么整个curl操作就不会失败,甚至不能清楚地报告错误。使用 -v 会得到一个警告,但这是你得到的关于这种可能致命的情况的唯一可见反馈。

如果多次使用此选项,将使用最后指定的文件名。

--connect-timeout <seconds>

连接服务端的超时时间。这只限制了连接阶段,一旦curl连接了此选项就不再使用了。

也可参见:-m, --max-time 选项。

1 # 当前 https://www.zhangXX.com 是国外服务器,访问受限
2 [root@iZ28xbsfvc4Z ~]# curl --connect-timeout 10 https://www.zhangXX.com | head
3   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
4                                  Dload  Upload   Total   Spent    Left  Speed
5   0     0    0     0    0     0      0      0 --:--:--  0:00:10 --:--:--     0
6 curl: (28) Connection timed out after 10001 milliseconds

 

--create-dirs

当与 -o 选项一起使用时,curl将根据需要创建必要的本地目录层次结构。

这个选项只创建与 -o 选项相关的dirs,没有其他内容。如果 -o 文件名没有使用dir,或者其中提到的dir已经存在,则不会创建dir。

示例

curl -o ./hexo04/index.html --create-dirs http://www.zhangblog.com/2019/06/16/hexo04

 

-C, --continue-at <offset>

按给定偏移量继续/恢复以前的文件传输。给定的偏移量是将被跳过的确切字节数,从源文件的开头开始计算,然后再将其传输到目标文件。

使用 “-C -“「注意有空格和无空格的情况」,告诉curl自动找出在哪里/如何恢复传输。然后,它使用给定的输出/输入文件来解决这个问题。

1 # 下载一个 2G 的文件,可以反复测试,查看结果
2 curl -C - -o tmp.data http://www.zhangblog.com/uploads/tmp/tmp.data

 

-d, --data <data>

使用该选项,那么默认请求方式为 POST。
(HTTP)在POST请求中向HTTP服务器发送指定的数据,与浏览器在用户填写HTML表单并按下submit按钮时所做的相同。这将导致curl使用content-type application/x-www-form-urlencoded将数据传递给服务器。也可参见:-F,-form 。

如果这些命令在同一个命令行使用多次,这些数据片段将使用指定的分隔符 & 合并。
因此,使用 ‘-d name=daniel -d skill=lousy’ 将生成一个类似 ‘name=daniel&skill=lousy’ 的post块,也可以直接这样合并使用。

-d, --data 与 --data-ascii 相同。post数据为纯粹的二进制数据时,那么使用 --data-binary 选项。要对表单字段的值进行url编码,可以使用 --data-urlencode。

如果您以字母@开始数据,那么其余的应该是一个文件名,以便从其中读取数据。或者 - 如果您希望curl从stdin【标准输入】读取数据。文件的内容必须已经是url编码的。还可以指定多个文件。因此,Posting数据名为 “foobar” 的文件将使用 --data @foobar 完成。

示例

请求信息:

 1 [root@iZ28xbsfvc4Z 20190712]# curl -sv --local-port 9000 -d 'user=zhang&pwd=123456' http://www.zhangblog.com/2019/06/24/domainexpire/ | head -n1 
 2 * About to connect() to www.zhangblog.com port 80 (#0)
 3 *   Trying 120.27.48.179...
 4 * Local port: 9000
 5 * Connected to www.zhangblog.com (120.27.48.179) port 80 (#0)
 6 > POST /2019/06/24/domainexpire/ HTTP/1.1   # 可见请求方式为POST
 7 > User-Agent: curl/7.29.0
 8 > Host: www.zhangblog.com
 9 > Accept: */*
10 > Content-Length: 21
11 > Content-Type: application/x-www-form-urlencoded
12 > 
13 } [data not shown]
14 * upload completely sent off: 21 out of 21 bytes
15 < HTTP/1.1 405 Not Allowed
16 < Server: nginx/1.14.2
17 < Date: Fri, 12 Jul 2019 13:34:20 GMT
18 < Content-Type: text/html
19 < Content-Length: 173
20 < Connection: keep-alive
21 < 
22 { [data not shown]
23 * Connection #0 to host www.zhangblog.com left intact
24 <html>

 

抓包信息

[root@iZ28xbsfvc4Z tcpdump]# tcpdump -i any port 9000 -A -s 0

 

--data-ascii <data>

参见 -d, --data

--data-binary <data>

(HTTP) POST数据完全按照指定的方式,没有任何额外的处理。

如果您以字母@开始数据,其余的应该是文件名。
数据是以类似于 --data-ascii 的方式发布的,只不过保留了换行,而且永远不会进行转换【数据不转换】。

如果多次使用此选项,第一个选项后面的选项将按照 -d, --data 中的描述追加数据。

--data-urlencode <data>

(HTTP)这个Post 数据,与另一个 --data 选项类似,除执行url编码以外。

-D, --dump-header <file>

将响应协议头写入指定的文件。

如果多次使用此选项,则将使用最后一个选项。

当你想要存储HTTP站点发送给你的头文件时,使用此选项非常方便。

1 [root@iZ28xbsfvc4Z 20190703]# curl -D baidu_header.info www.baidu.com 
2 ………………
3 [root@iZ28xbsfvc4Z 20190703]# ll
4 total 4
5 -rw-r--r-- 1 root root 400 Jul  3 10:11 baidu_header.info  # 生成的头文件

之后第二次curl调用通过 -b, --cookie 选项,可以从头部读取 cookies 。然而 -c, --cookie-jar 选项是存储 cookies 更好的方法。

常用选项三

--digest

(HTTP)启用HTTP摘要身份验证。这是一种身份验证方案,可以防止密码以明文通过网络发送。将此选项与普通的 -u, --user 选项组合使用,以设置用户名和密码。
相关选项请参见 --ntlm, --negotiate 和 --anyauth。

如果多次使用此选项,则只使用第一个选项。

-e, --referer <URL>

(HTTP)将 “Referer Page” 【从哪个页面跳转过来的】信息发送到HTTP服务器。当然也可以使用 -H, --header 标志来设置。

如果多次使用此选项,则将使用最后一个选项。

curl -e 'https:www.baidu.com' http://www.zhangblog.com/2019/06/24/domainexpire/

-f, --fail

(HTTP)在服务器错误上静默失败(完全没有输出)。这主要是为了使脚本等更好地处理失败的尝试。

在通常情况下,当HTTP服务器无法交付文档时,它会返回一个HTML文档,说明原因(通常还会描述原因)。此标志将阻止curl输出该值并返回错误22。

 1 [root@iZ28xbsfvc4Z 20190713]# curl http://www.zhangblog.com/201912312
 2 <html>
 3 <head><title>404 Not Found</title></head>
 4 <body bgcolor="white">
 5 <center><h1>404 Not Found</h1></center>
 6 <hr><center>nginx/1.14.2</center>
 7 </body>
 8 </html>
 9 [root@iZ28xbsfvc4Z 20190713]# curl -f http://www.zhangblog.com/201912312
10 curl: (22) The requested URL returned error: 404 Not Found

-F, --form <name=content>

(HTTP)这允许curl模拟用户按下submit按钮后填充的表单。

该情况让curl 可使用Content-Type multipart/form-data POST数据。也可以上传二进制文件等。

@文件名:使一个文件作为文件上传附加在post中。
<文件名:从文件中获取该文本字段的内容。

例如,要将密码文件发送到服务器,其中“password”是表单字段的名称,/etc/passwd将作为输入:

curl -F password=@/etc/passwd www.mypasswords.com

 

您还可以使用 ‘type=’ 告诉curl使用什么 Content-Type ,方法类似于:

curl -F "web=@index.html;type=text/html" url.com
或
curl -F "name=daniel;type=text/foo" url.com

 

可以通过设置 filename= 更改本地上传的文件名,如下:

curl -F "file=@localfile;filename=nameinpost" url.com

上传的文件名从改为了 nameinpost

如果文件名/路径包括 ‘,’ 或 ‘;’ ,必须用双引号括起来:

curl -F "file=@\"localfile\";filename=\"nameinpost\"" url.com
或
curl -F 'file=@"localfile";filename="nameinpost"' url.com

最外层可用单引号或双引号。

这个选项可以多次使用。

 

请勿如下使用

curl -F 'user=zhang&password=pwd' url.com   # 这种用法是错误的

 

--form-string <name=string>

(HTTP)类似于 --form,只是命名参数的value字符串按字面意思使用。
在值中以 ‘@’ 和 ‘<’ 开头的字符,以及 ‘;type=’ 字符串没有特殊的含义。

如果字符串值有可能意外触发 --form 的 “@” 或 “<” 特性,请优先使用此选项。

-g, --globoff

这个选项关闭了“URL全局解析器”。当您设置这个选项时,您可以指定包含字母 {}[] 的url,而不需要curl本身来解释它们。

注意,这些字母不是正常的合法URL内容,但是它们应该按照URI标准进行编码。

-G, --get

使用此选项时,将使所有使用 -d, --data 或 --data-binary 指定的数据在HTTP GET请求中使用,而不是在POST请求中使用。
数据将被追加到URL的一个 ‘?’ 的分隔符后。

如果与 -I 结合使用,POST数据将被替换追加到带有HEAD请求的URL中。

如果多次使用此选项,则只使用第一个选项。

示例

 1 [root@iZ28xbsfvc4Z 20190712]# curl -sv -G --local-port 9000 -d 'user=zhang&pwd=123456' http://www.zhangblog.com/2019/06/24/domainexpire/ | head -n1 
 2 或则
 3 [root@iZ28xbsfvc4Z 20190713]# curl -sv --local-port 9000 "http://www.zhangblog.com/2019/06/24/domainexpire/?user=zhang&pwd=123456" | head -n1
 4 * About to connect() to www.zhangblog.com port 80 (#0)
 5 *   Trying 120.27.48.179...
 6 * Local port: 9000
 7 * Connected to www.zhangblog.com (120.27.48.179) port 80 (#0)
 8 > GET /2019/06/24/domainexpire/?user=zhang&pwd=123456 HTTP/1.1  # 可见请求方式为 GET,且参数追加到了URI后
 9 > User-Agent: curl/7.29.0
10 > Host: www.zhangblog.com
11 > Accept: */*
12 > 
13 < HTTP/1.1 200 OK
14 < Server: nginx/1.14.2
15 < Date: Fri, 12 Jul 2019 14:04:19 GMT
16 < Content-Type: text/html
17 < Content-Length: 51385
18 < Last-Modified: Tue, 09 Jul 2019 13:55:19 GMT
19 < Connection: keep-alive
20 < ETag: "5d249cc7-c8b9"
21 < Accept-Ranges: bytes
22 < 
23 { [data not shown]
24 * Connection #0 to host www.zhangblog.com left intact
25 <!DOCTYPE html>

 

抓包信息

[root@iZ28xbsfvc4Z tcpdump]# tcpdump -i any port 9000 -A -s 0

 

-H, --header <header>

(HTTP) 要发送到服务端的自定义请求头。

此选项可多次用于添加/替换/删除多个headers。

1 curl -H 'Connection: keep-alive' -H 'Referer: https://sina.com.cn' -H 'User-Agent: Mozilla/1.0' http://www.zhangblog.com/2019/06/24/domainexpire/

 

--ignore-content-length

(HTTP)忽略Content-Length 头信息。

-i, --include

(HTTP)在输出的内容中包含HTTP 头信息。

curl -i https://www.baidu.com

 

-I, --head

(HTTP/FTP/FILE)只获取HTTP头文件。
在FTP或FILE 文件上使用时,curl只显示文件大小和最后修改时间。

curl -I https://www.baidu.com

 

-k, --insecure

(SSL)允许curl执行不安全的SSL连接和传输。
所有SSL连接都尝试使用默认安装的CA证书包来确保安全。

示例

 1 [root@iZ28xbsfvc4Z ~]# curl https://140.205.16.113/  # 被拒绝
 2 curl: (51) Unable to communicate securely with peer: requested domain name does not match the server's certificate.
 3 [root@iZ28xbsfvc4Z ~]# 
 4 [root@iZ28xbsfvc4Z ~]# curl -k https://140.205.16.113/  # 允许执行不安全的证书连接
 5 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
 6 <html>
 7 <head><title>403 Forbidden</title></head>
 8 <body bgcolor="white">
 9 <h1>403 Forbidden</h1>
10 <p>You don't have permission to access the URL on this server.<hr/>Powered by Tengine</body>
11 </html>

 

常用选项四

--keepalive-time <seconds>

keepalive 时长。如果使用no-keepalive,则此选项无效。

如果多次使用此选项,则将使用最后一个选项。如果未指定,该选项默认为60秒。

--key <key>

(SSL/SSH)私钥文件名。允许你在这个单独的文件中提供你的私钥。

对于SSH,如果没有指定,curl尝试如下顺序:’~/.ssh/id_rsa’,’~/.ssh/id_dsa’,’./id_rsa’,’./id_dsa’。

如果多次使用此选项,则将使用最后一个选项。

--key-type <type>

(SSL)私钥文件类型。指定 --key 提供的私钥的类型。支持DER、PEM和ENG。如果没有指定,则定为PEM。

如果多次使用此选项,则将使用最后一个选项。

-L, --location

(HTTP/HTTPS) 跟踪重定向
如果服务器报告请求页面已移动到另一个位置(用location: header和3XX响应代码表示),此选项将使curl在新位置上重做请求。

如果与 -i, --include 或 -I, --head 一起使用,将显示所有请求页面的标题。

 1 [root@iZ28xbsfvc4Z ~]# curl -I -L https://baidu.com/ 
 2 HTTP/1.1 302 Moved Temporarily  # 302 重定向
 3 Server: bfe/1.0.8.18
 4 Date: Thu, 04 Jul 2019 03:07:15 GMT
 5 Content-Type: text/html
 6 Content-Length: 161
 7 Connection: keep-alive
 8 Location: http://www.baidu.com/
 9 
10 HTTP/1.1 200 OK
11 Accept-Ranges: bytes
12 Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
13 Connection: Keep-Alive
14 Content-Length: 277
15 Content-Type: text/html
16 Date: Thu, 04 Jul 2019 03:07:15 GMT
17 Etag: "575e1f60-115"
18 Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
19 Pragma: no-cache
20 Server: bfe/1.0.8.18

 

--limit-rate <speed>

指定要使用curl的最大传输速率。

如果有一个有限的管道,并且希望传输不要使用您的全部带宽,那么这个特性是非常有用的。

curl --limit-rate 500 http://www.baidu.com/
curl --limit-rate 2k http://www.baidu.com/

单位:默认字节,除非添加后缀。附加 “k” 或 “K” 表示千字节, “m” 或 “M” 表示兆字节,而 “g” 或 “G” 表示千兆字节。例如:200K, 3m和1G。

给定的速率是整个传输过程中计算的平均速度。这意味着curl可能在短时间内使用更高的传输速度,但是随着时间的推移,它只使用给定的速率。

如果多次使用此选项,则将使用最后一个选项。

--local-port <num>[-num]

指定本地的一个端口或端口范围去连接。

请注意,端口号本质上是一种稀缺资源,有时会很忙,因此将此范围设置为太窄可能会导致不必要的连接失败。

curl --local-port 9000 http://www.baidu.com/
curl --local-port 9000-9999 http://www.baidu.com/

 

-m, --max-time <seconds>

允许整个操作花费的最大时间(以秒为单位)。

这对于防止由于网络或链接变慢而导致批处理作业挂起数小时非常有用。

也可参见:--connect-timeout 选项

 1 [root@iZ28xbsfvc4Z ~]# curl -m 10 --limit-rate 5 http://www.baidu.com/ | head  # 超过10秒后,断开连接
 2   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 3                                  Dload  Upload   Total   Spent    Left  Speed
 4   2  2381    2    50    0     0      4      0  0:09:55  0:00:10  0:09:45     4
 5 curl: (28) Operation timed out after 10103 milliseconds with 50 out of 2381 bytes received
 6 <!DOCTYPE html>
 7 <!--STATUS OK--><html> <head><met
 8 ### 或
 9 [root@iZ28xbsfvc4Z ~]# curl -m 10 https://www.zhangXX.com | head   # 超过10秒后,断开连接
10   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
11                                  Dload  Upload   Total   Spent    Left  Speed
12   0     0    0     0    0     0      0      0 --:--:--  0:00:10 --:--:--     0
13 curl: (28) Connection timed out after 10001 milliseconds

 

--max-filesize <bytes>

指定要下载的文件的最大大小(以字节为单位)。
如果请求的文件大于这个值,那么传输将不会启动,curl将返回退出代码63。

示例

 1 [root@iZ28xbsfvc4Z ~]# curl -I http://www.zhangblog.com/uploads/hexo/00.jpg # 正常
 2 HTTP/1.1 200 OK
 3 Server: nginx/1.14.2
 4 Date: Thu, 04 Jul 2019 07:24:24 GMT
 5 Content-Type: image/jpeg
 6 Content-Length: 18196
 7 Last-Modified: Mon, 24 Jun 2019 01:43:02 GMT
 8 Connection: keep-alive
 9 ETag: "5d102aa6-4714"
10 Accept-Ranges: bytes
11 
12 [root@iZ28xbsfvc4Z ~]# echo $?
13 0
14 [root@iZ28xbsfvc4Z ~]# 
15 [root@iZ28xbsfvc4Z ~]# 
16 [root@iZ28xbsfvc4Z ~]# curl --max-filesize 1000 -I http://www.zhangblog.com/uploads/hexo/00.jpg # 受限异常
17 HTTP/1.1 200 OK
18 Server: nginx/1.14.2
19 Date: Thu, 04 Jul 2019 07:24:54 GMT
20 Content-Type: image/jpeg
21 curl: (63) Maximum file size exceeded
22 [root@iZ28xbsfvc4Z ~]# 
23 [root@iZ28xbsfvc4Z ~]# echo $?
24 63

 

--max-redirs <num>

设置允许的最大重定向跟踪数。

如果也使用了 -L, --location,则此选项可用于防止curl在悖论中无限重定向。
默认情况下,限制为50重定向。将此选项设置为-1,使其无限。

--no-keepalive

禁用在TCP连接上使用keepalive消息,因为默认情况下curl启用了它们。

注意,这是文档中已否定的选项名。因此,您可以使用 --keepalive 来强制keepalive。

常用选项五

-o, --output <file>

输出到一个文件,而不是标准输出。

如果使用 {} 或 [] 来获取多个documents。可以使用 ‘#’ 后跟说明符中的一个数字。该变量将替换为正在获取URL的当前字符串。就像:

curl http://{one,two}.site.com -o "file_#1.txt"
curl http://{site,host}.host[1-5].com -o "#1_#2"

 

示例1

 1 [root@iZ28xbsfvc4Z 20190703]# curl "http://www.zhangblog.com/2019/06/16/hexo{04,05,06}/" -o "file_#1.info"   # 注意curl 的地址需要用引号括起来
 2  3 [root@iZ28xbsfvc4Z 20190703]# curl "http://www.zhangblog.com/2019/06/16/hexo[04-06]/" -o "file_#1.info"   # 注意curl 的地址需要用引号括起来
 4 [1/3]: http://www.zhangblog.com/2019/06/16/hexo04/ --> file_04.info
 5   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 6                                  Dload  Upload   Total   Spent    Left  Speed
 7 100 97299  100 97299    0     0  1551k      0 --:--:-- --:--:-- --:--:-- 1557k
 8 
 9 [2/3]: http://www.zhangblog.com/2019/06/16/hexo05/ --> file_05.info
10 100 54409  100 54409    0     0   172M      0 --:--:-- --:--:-- --:--:--  172M
11 
12 [3/3]: http://www.zhangblog.com/2019/06/16/hexo06/ --> file_06.info
13 100 56608  100 56608    0     0   230M      0 --:--:-- --:--:-- --:--:--  230M
14 [root@iZ28xbsfvc4Z 20190703]# 
15 [root@iZ28xbsfvc4Z 20190703]# ll
16 total 212
17 -rw-r--r-- 1 root root 97299 Jul  4 16:51 file_04.info
18 -rw-r--r-- 1 root root 54409 Jul  4 16:51 file_05.info
19 -rw-r--r-- 1 root root 56608 Jul  4 16:51 file_06.info

 

示例2

 1 [root@iZ28xbsfvc4Z 20190703]# curl "http://www.{baidu,douban}.com" -o "site_#1.txt"  # 注意curl 的地址需要用引号括起来
 2 [1/2]: http://www.baidu.com --> site_baidu.txt
 3   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 4                                  Dload  Upload   Total   Spent    Left  Speed
 5 100  2381  100  2381    0     0  46045      0 --:--:-- --:--:-- --:--:-- 46686
 6 
 7 [2/2]: http://www.douban.com --> site_douban.txt
 8 100   162  100   162    0     0   3173      0 --:--:-- --:--:-- --:--:--  3173
 9 [root@iZ28xbsfvc4Z 20190703]# 
10 [root@iZ28xbsfvc4Z 20190703]# ll
11 total 220
12 -rw-r--r-- 1 root root  2381 Jul  4 16:53 site_baidu.txt
13 -rw-r--r-- 1 root root   162 Jul  4 16:53 site_douban.txt

 

-O, --remote-name

写入到本地文件,名称与远程文件的名称相同。(只使用远程文件的文件部分,路径被切断。)

用于保存的远程文件名是从给定的URL中提取的,没有其他内容。
因此,文件将保存在当前工作目录中。如果希望将文件保存在另一个目录中,请确保在curl调用 -O, --remote-name之前更改当前工作目录!

1 [root@iZ28xbsfvc4Z 20190712]# curl -O https://www.baidu.com   # 使用了 -O 选项,必须指定到具体的文件  错误使用
2 curl: Remote file name has no length!
3 curl: try 'curl --help' or 'curl --manual' for more information
4 [root@iZ28xbsfvc4Z 20190712]# curl -O https://www.baidu.com/index.html   # 使用了 -O 选项,必须指定到具体的文件  正确使用
5   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
6                                  Dload  Upload   Total   Spent    Left  Speed
7 100  2443  100  2443    0     0  13289      0 --:--:-- --:--:-- --:--:-- 13349

 

--pass <phrase>

(SSL/SSH)私钥密码

如果多次使用此选项,则将使用最后一个选项。

--post301

告诉curl当301重定向时,不要将POST请求转换为GET请求。

非rfc行为在web浏览器中无处不在,因此curl在缺省情况下进行转换以保持一致性。但是,服务器可能需要在重定向之后将POST保留为POST。

这个选项只有在使用 -L, --location 时才有意义

--post302

告诉curl当302重定向时,不要将POST请求转换为GET请求。

非rfc行为在web浏览器中无处不在,因此curl在缺省情况下进行转换以保持一致性。但是,服务器可能需要在重定向之后将POST保留为POST。

这个选项只有在使用 -L, --location 时才有意义

--post303

告诉curl当303重定向时,不要将POST请求转换为GET请求。

非rfc行为在web浏览器中无处不在,因此curl在缺省情况下进行转换以保持一致性。但是,服务器可能需要在重定向之后将POST保留为POST。

这个选项只有在使用 -L, --location 时才有意义

说明:
上述三个选项都是为了防止在重定向过程中,原来的 POST 请求,变为 GET请求。为了防止该情况,有两种处理方式。
1、使用上述选项可避免;
2、使用 -X POST 选项和命令。

示例

[root@iZ28xbsfvc4Z ~]# curl -Lsv -d 'user=zhang' https://baidu.com | head -n1

 

开始是POST请求,302 重定向后变为了 GET请求。

 

[root@iZ28xbsfvc4Z ~]# curl -Lsv -d 'user=zhang' --post301 --post302 --post303 https://baidu.com | head -n1

前后都是 POST 请求。但是选项较多。

 

[root@iZ28xbsfvc4Z ~]# curl -Lsv -d 'user=zhang' -X POST https://baidu.com | head -n1

前后都是 POST 请求。推荐使用此命令。

 

--pubkey <key>

(SSH)公钥文件名。允许在这个单独的文件中提供公钥。

如果多次使用此选项,则将使用最后一个选项。

-r, --range <range>

(HTTP/FTP/SFTP/FILE)从HTTP/1.1、FTP或SFTP服务器或本地文件检索字节范围。范围可以通过多种方式指定。用于分段下载。

有时文件比较大,或者难以迅速传输,而利用分段传输,可以实现稳定、高效并且有保障的传输,更具有实用性,同时容易对差错文件进行更正。

0-499:指定前500个字节
500-999:指定第二个500字节
-500:指定最后500个字节
9500-:指定9500字节及之后的字节
0-0,-1:指定第一个和最后一个字节
500-700,600-799:从偏移量500开始指定300字节
100-199,500-599:指定两个单独100字节的范围

分段下载

 1 [root@iZ28xbsfvc4Z 20190715]# curl -I http://www.zhangblog.com/uploads/hexo/00.jpg   # 查看文件大小
 2 HTTP/1.1 200 OK
 3 Server: nginx/1.14.2
 4 Date: Mon, 15 Jul 2019 03:23:44 GMT
 5 Content-Type: image/jpeg
 6 Content-Length: 18196   # 文件大小
 7 Last-Modified: Fri, 05 Jul 2019 08:04:58 GMT
 8 Connection: keep-alive
 9 ETag: "5d1f04aa-4714"
10 Accept-Ranges: bytes
11 [root@iZ28xbsfvc4Z 20190715]# curl -r 0-499   -o 00-jpg.part1 http://www.zhangblog.com/uploads/hexo/00.jpg
12 [root@iZ28xbsfvc4Z 20190715]# curl -r 500-999 -o 00-jpg.part2 http://www.zhangblog.com/uploads/hexo/00.jpg
13 [root@iZ28xbsfvc4Z 20190715]# curl -r 1000-   -o 00-jpg.part3 http://www.zhangblog.com/uploads/hexo/00.jpg

 

查看下载文件

1 [root@iZ28xbsfvc4Z 20190715]# ll
2 total 36
3 -rw-r--r-- 1 root root   500 Jul 15 11:25 00-jpg.part1
4 -rw-r--r-- 1 root root   500 Jul 15 11:25 00-jpg.part2
5 -rw-r--r-- 1 root root 17196 Jul 15 11:26 00-jpg.part3

 

文件合并

1 [root@iZ28xbsfvc4Z 20190715]# cat 00-jpg.part1 00-jpg.part2 00-jpg.part3 > 00.jpg
2 [root@iZ28xbsfvc4Z 20190715]# ll
3 total 56
4 -rw-r--r-- 1 root root 18196 Jul 15 11:29 00.jpg

 

-R, --remote-time

使curl尝试获取远程文件的时间戳,如果可用,则使本地文件获得相同的时间戳【针对修改时间戳Modify】。

curl -o nfs1.info -R http://www.zhangblog.com/2019/07/05/nfs1/

 

--retry <num>

传输出现问题时,重试的次数。数字设置为0将使curl不重试(这是缺省值)。

出现的瞬时错误如:timeout、FTP 4xx响应状代码或HTTP 5xx响应状代码。

当curl准备重试传输时,它将首先等待一秒钟,之后对于所有即将到来的重试,它将把等待时间延长一倍,直到达到10分钟,这将是其余重试之间的延迟。

--retry-delay <seconds>

传输出现问题时,设置重试间隔时间。将此延迟设置为零将使curl使用默认的延迟时间。

--retry-max-time <seconds>

传输出现问题时,设置最大重试时间。将此选项设置为0则不超时重试。

 

常用选项六

-s, --silent

静默或静音模式。不显示进度表/条或错误消息。

示例

1 [root@iZ28xbsfvc4Z 20190713]# curl https://www.baidu.com | head -n1  # 默认有进度表
2   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
3                                  Dload  Upload   Total   Spent    Left  Speed
4 100  2443  100  2443    0     0  13346      0 --:--:-- --:--:-- --:--:-- 13349
5 <!DOCTYPE html>
6 [root@iZ28xbsfvc4Z 20190713]# curl -s https://www.baidu.com | head -n1
7 <!DOCTYPE html>

 

-S, --show-error

当与 -s 一起使用时,如果curl失败,curl将显示一条错误消息。

1 [root@iZ28xbsfvc4Z 20190713]# curl -s https://140.205.16.113/ 
2 [root@iZ28xbsfvc4Z 20190713]# 
3 [root@iZ28xbsfvc4Z 20190713]# curl -sS https://140.205.16.113/ 
4 curl: (51) Unable to communicate securely with peer: requested domain name does not match the server's certificate.

 

--stderr <file>

将错误信息重定向到一个文件。如果文件名是普通的 ‘-‘,则将其写入stdout。

如果多次使用此选项,则将使用最后一个选项。

1 [root@iZ28xbsfvc4Z 20190713]# curl --stderr err.info https://140.205.16.113/ 
2 [root@iZ28xbsfvc4Z 20190713]# ll
3 total 92
4 -rw-r--r-- 1 root root   116 Jul 13 10:19 err.info
5 [root@iZ28xbsfvc4Z 20190713]# cat err.info 
6 curl: (51) Unable to communicate securely with peer: requested domain name does not match the server's certificate.

 

-T, --upload-file <file>

这将指定的本地文件传输到远程URL。如果指定的URL中没有文件部分,Curl将附加本地文件名。

注意:必须在最后一个目录上使用尾随 / 来真正证明Curl没有文件名,否则Curl会认为您的最后一个目录名是要使用的远程文件名。这很可能导致上传操作失败。
如果在HTTP(S)服务器上使用此命令,则将使用PUT命令。

同时也支持多个文件上传,如下:

curl -T "{file1,file2}" http://www.uploadtothissite.com
或则
curl -T "img[1-1000].png" ftp://ftp.picturemania.com/upload/

 

--trace <file>

对指定文件进行debug。包括所有传入和传出数据。

此选项会覆盖之前使用的 -v、--verbose或 --trace-ascii。

如果多次使用此选项,则将使用最后一个选项。

curl --trace trace.info https://www.baidu.com

 

--trace-ascii <file>

对指定文件进行debug。包括所有传入和传出数据。

这非常类似于 --trace,但是省略了十六进制部分,只显示转储的ASCII部分。使它输出更小,对于我们来说可能更容易阅读。

此选项会覆盖之前使用的 -v、--verbose或 --trace。

如果多次使用此选项,则将使用最后一个选项。

curl --trace-ascii trace2.info https://www.baidu.com

 

--trace-time

为curl显示的每个跟踪或冗长的行添加时间戳。

curl --trace-ascii trace3.info --trace-time https://www.baidu.com

 

-v, --verbose

显示详细操作信息。主要用于调试。

以 > 开头的行表示curl发送的”header data”;< 表示curl接收到的通常情况下隐藏的”header data”;而以 * 开头的行表示curl提供的附加信息。

 1 [root@iZ28xbsfvc4Z 20190712]# curl -v https://www.baidu.com
 2 * About to connect() to www.baidu.com port 443 (#0)
 3 *   Trying 180.101.49.12...
 4 * Connected to www.baidu.com (180.101.49.12) port 443 (#0)
 5 * Initializing NSS with certpath: sql:/etc/pki/nssdb
 6 *   CAfile: /etc/pki/tls/certs/ca-bundle.crt
 7   CApath: none
 8 * SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
 9 * Server certificate:
10 *     subject: CN=baidu.com,O="Beijing Baidu Netcom Science Technology Co., Ltd",OU=service operation department,L=beijing,ST=beijing,C=CN
11 *     start date: May 09 01:22:02 2019 GMT
12 *     expire date: Jun 25 05:31:02 2020 GMT
13 *     common name: baidu.com
14 *     issuer: CN=GlobalSign Organization Validation CA - SHA256 - G2,O=GlobalSign nv-sa,C=BE
15 > GET / HTTP/1.1
16 > User-Agent: curl/7.29.0
17 > Host: www.baidu.com
18 > Accept: */*
19 > 
20 < HTTP/1.1 200 OK
21 < Accept-Ranges: bytes
22 < Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
23 < Connection: Keep-Alive
24 < Content-Length: 2443
25 < Content-Type: text/html
26 < Date: Fri, 12 Jul 2019 08:26:23 GMT
27 < Etag: "588603eb-98b"
28 < Last-Modified: Mon, 23 Jan 2017 13:23:55 GMT
29 < Pragma: no-cache
30 < Server: bfe/1.0.8.18
31 < Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
32 < 
33 <!DOCTYPE html>
34 ………………  # curl 网页的具体信息

 

-w, --write-out <format>

在完成和成功操作后要在stdout上显示什么。

支持如下变量,具体含义请自行参见curl文档。

content_type
filename_effective
ftp_entry_path
http_code
http_connect
local_ip
local_port
num_connects
num_redirects
redirect_url
remote_ip
remote_port
size_download
size_header
size_request
size_upload
speed_download
speed_upload
ssl_verify_result
time_appconnect
time_connect
time_namelookup
time_pretransfer
time_redirect
time_starttransfer
time_total
url_effective

  

 示例

1 [root@iZ28xbsfvc4Z 20190713]# curl -o /dev/null -s -w %{content_type} www.baidu.com  # 输出结果没有换行
2 text/html[root@iZ28xbsfvc4Z 20190713]# 
3 [root@iZ28xbsfvc4Z 20190713]# curl -o /dev/null -s -w %{http_code} www.baidu.com  # 输出结果没有换行
4 200[root@iZ28xbsfvc4Z 20190713]# 
5 [root@iZ28xbsfvc4Z 20190713]# curl -o /dev/null -s -w %{local_port} www.baidu.com  # 输出结果没有换行
6 37346[root@iZ28xbsfvc4Z 20190713]# 
7 [root@iZ28xbsfvc4Z 20190713]#

 

-x, --proxy <[protocol://][user:password@]proxyhost[:port]>

使用指定的HTTP代理。如果没有指定端口号,则假定它位于端口1080。

-X, --request <command>

(HTTP)指定与HTTP服务器通信时的请求方式。默认GET

curl -vs -X POST https://www.baidu.com | head -n1

 

curl -vs -X PUT https://www.baidu.com | head -n1

 

推荐阅读

Linux curl 命令详解

Linux curl 常用示例

Linux curl 表单登录或提交与cookie使用

 


 

如果觉得不错就点个赞呗 (-^O^-) !

———END———-

 

posted on 2019-08-09 13:06  踏歌行666  阅读(12065)  评论(0编辑  收藏  举报

导航