Linux常用命令-curl

命令

curl

描述

transfer a URL
请求URL工具

用法

1
curl [options] [URL...]

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
Options:
(H) means HTTP/HTTPS only, (F) means FTP only
--compressed 请求压缩响应 (using deflate or gzip)
-K, --config FILE 指定配置文件
--connect-timeout SECONDS 允许连接的最长时间
-C, --continue-at OFFSET 断点续传,如果指定为'-C -',则自动从上次中止的位置续传
-b, --cookie STRING/FILE 指定cookie (H)
-c, --cookie-jar FILE 操作结束后将Cookie写入文件 (H)
--create-dirs 创建必要的本地目录层次结构
--crlf 上传时将LF转换为CRLF
--crlfile FILE 从给定文件中获取PEM格式的CRL列表
-d, --data DATA 提交数据 (H)
--data-ascii DATA HTTP POST ASCII data (H)
--data-binary DATA HTTP POST binary data (H)
--data-urlencode DATA HTTP POST data url encoded (H)
--delegation STRING GSS-API 授权许可
-D, --dump-header FILE 将header写入文件
-f, --fail 不显示错误输出 (H)
-F, --form CONTENT 提交表单数据 (H)
--form-string STRING 提交表单数据 (H)
-G, --get 使用HTTP GET方式发送-d数据 (H)
-g, --globoff 使用{}和[]禁用URL序列和范围
-H, --header LINE 指定header信息 (H)
-I, --head 只显示响应头信息
-0, --http1.0 使用HTTP 1.0 (H)
--ignore-content-length Ignore the HTTP Content-Length header
-i, --include 在输出中包括响应头信息 (H/F)
-k, --insecure 忽略证书访问SSL站点 (H)
--interface INTERFACE 指定要使用的网络接口/地址
-4, --ipv4 使用IPv4地址
-6, --ipv6 使用IPv6地址
-j, --junk-session-cookies 忽略从文件中读取的会话cookie (H)
--keepalive-time SECONDS keepalive探测的时间间隔
--limit-rate RATE 传输速度
--local-port RANGE 强制使用这些本地端口号
-L, --location 跟随重定向 (H)
--location-trusted 类似-L,并发送认证到其他主机 (H)
-M, --manual 显示详细的帮助
--mail-from FROM Mail from this address
--mail-rcpt TO Mail to this receiver(s)
--mail-auth AUTH Originator address of the original email
--max-filesize BYTES 下载的最大文件大小 (H/F)
--max-redirs NUM 允许的最大重定向数 (H)
-m, --max-time SECONDS 允许传输的最长时间
--metalink 将给定URL作为metalink XML文件处理
-n, --netrc 读取.netrc以获取用户名和密码
--netrc-optional 使用.netrc或URL;覆盖-n
--netrc-file FILE 指定netrc文件
-N, --no-buffer 禁用缓存
--no-keepalive 禁用keepalive
--no-sessionid 禁用SSL session-ID (SSL)
--noproxy 不使用代理服务器
--post301 在执行301重定向后不要切换到GET (H)
--post302 在执行302重定向后不要切换到GET (H)
--post303 在执行303重定向后不要切换到GET (H)
-#, --progress-bar 显示进度条
--proto PROTOCOLS 启用/禁用指定协议
--proto-redir PROTOCOLS 在重定向时启用/禁用指定协议
-x, --proxy [PROTOCOL://]HOST[:PORT] 使用代理
--proxy-anyauth 选择任意代理身份验证 (H)
--proxy-basic 在代理上使用基本身份验证 (H)
--proxy-digest Use Digest authentication on the proxy (H)
--proxy-negotiate 在代理上使用摘要身份验证 (H)
--proxy-ntlm 在代理上使用NTLM身份验证 (H)
-U, --proxy-user USER[:PASSWORD] 设置代理的用户名和密码
--proxy1.0 HOST[:PORT] 使用HTTP/1.0代理
-p, --proxytunnel 通过HTTP代理隧道进行操作 (using CONNECT)
-r, --range RANGE Retrieve only the bytes within a range
--raw Do HTTP "raw", without any transfer decoding (H)
-e, --referer 指定Referer URL (H)
-J, --remote-header-name Use the header-provided filename (H)
-o, --output FILE 保存输出到指定文件
-O, --remote-name 保存输出到文件,使用远程文件名
--remote-name-all Use the remote file name for all URLs
-R, --remote-time 在本地生成文件时,保留远程文件时间
-X, --request COMMAND 指定要使用的请求方法,如GET,POST,HEAD等
--resolve HOST:PORT:ADDRESS Force resolve of HOST:PORT to ADDRESS
--retry NUM 重试次数
--retry-delay SECONDS 重试间隔时间
--retry-max-time SECONDS 最大重试时间
-S, --show-error 显示错误,配合-s可以在出错时显示错误信息
-s, --silent 静默模式,不显示任何输出
--socks4 HOST[:PORT] SOCKS4 proxy on given host + port
--socks4a HOST[:PORT] SOCKS4a proxy on given host + port
--socks5 HOST[:PORT] SOCKS5 proxy on given host + port
--socks5-basic Enable username/password auth for SOCKS5 proxies
--socks5-gssapi Enable GSS-API auth for SOCKS5 proxies
--socks5-hostname HOST[:PORT] SOCKS5 proxy, pass host name to proxy
--socks5-gssapi-service NAME SOCKS5 proxy service name for gssapi
--socks5-gssapi-nec Compatibility with NEC SOCKS5 server
-Y, --speed-limit RATE Stop transfers below speed-limit for 'speed-time' secs
-y, --speed-time SECONDS Time for trig speed-limit abort. Defaults to 30
--stderr FILE 将标准错误输出到文件. - means stdout
--tcp-nodelay Use the TCP_NODELAY option
-t, --telnet-option OPT=VAL Set telnet option
--tftp-blksize VALUE Set TFTP BLKSIZE option (must be >512)
-z, --time-cond TIME Transfer based on a time condition
--trace FILE 将调试跟踪保存到文件
--trace-ascii FILE Like --trace but without the hex output
--trace-time Add time stamps to trace/verbose output
--tr-encoding Request compressed transfer encoding (H)
-T, --upload-file FILE 上传文件
--url URL URL to work with
-B, --use-ascii Use ASCII/text transfer
-u, --user USER[:PASSWORD] 指定用户名和密码
--unix-socket FILE Connect through this UNIX domain socket
-A, --user-agent STRING 指定User-Agent (H)
-v, --verbose 显示详情
-w, --write-out FORMAT 数据传输完成后显示的信息,FORMAT格式为%{xxx},如%{http_code},%{time_connect}等,具体变量清参考man curl帮助
--xattr Store metadata in extended file attributes
-q If used as the first parameter disables .curlrc
--anyauth 使用任意身份验证 (H)
--basic 使用HTTP基本身份验证 (H)
--digest 使用HTTP摘要身份验证 (H)
--negotiate 使用HTTP协商身份验证 (H)
--ntlm 使用HTTP NTLM身份验证 (H)

# SSL相关选项
--cacert FILE CA证书路径 (SSL)
--capath DIR CA证书目录 (SSL)
-E, --cert CERT[:PASSWD] 客户端证书文件和密码 (SSL)
--cert-type TYPE 证书文件类型,可选DER/PEM/ENG (SSL)
--key KEY 私钥文件 (SSL/SSH)
--key-type TYPE 私钥文件类型,可选DER/PEM/ENG (SSL)
--ciphers LIST 使用的SSL密码 (SSL)
--egd-file FILE EGD socket path for random data (SSL)
--engine ENGINGE 指定加密引擎 (SSL). "--engine list" for list
--pass PASS 私钥密码 (SSL/SSH)
--pubkey KEY 公钥key文件 (SSH)
--hostpubmd5 MD5 主机公钥的十六进制编码MD5字符串 (SSH)
--ssl Try SSL/TLS (FTP, IMAP, POP3, SMTP)
--ssl-reqd Require SSL/TLS (FTP, IMAP, POP3, SMTP)
-2, --sslv2 使用SSLv2 (SSL)
-3, --sslv3 使用SSLv3 (SSL)
--ssl-allow-beast Allow security flaw to improve interop (SSL)
-1, --tlsv1 使用TLSv1 (SSL)
--tlsv1.0 Use TLSv1.0 (SSL)
--tlsv1.1 Use TLSv1.1 (SSL)
--tlsv1.2 Use TLSv1.2 (SSL)
--tlsv1.3 Use TLSv1.3 (SSL)
--tls-max VERSION Use TLS up to VERSION (SSL)
--tlsuser USER TLS username
--tlspassword STRING TLS password
--tlsauthtype STRING TLS authentication type (default SRP)
--random-file FILE File for reading random data from (SSL)

# FTP相关选项
-a, --append Append to target file when uploading (F/SFTP)
--disable-eprt Inhibit using EPRT or LPRT (F)
--disable-epsv Inhibit using EPSV (F)
--ftp-account DATA Account data string (F)
--ftp-alternative-to-user COMMAND String to replace "USER [name]" (F)
--ftp-create-dirs Create the remote dirs if not present (F)
--ftp-method [MULTICWD/NOCWD/SINGLECWD] Control CWD usage (F)
--ftp-pasv Use PASV/EPSV instead of PORT (F)
-P, --ftp-port ADR Use PORT with given address instead of PASV (F)
--ftp-skip-pasv-ip Skip the IP address for PASV (F)
--ftp-pret Send PRET before PASV (for drftpd) (F)
--ftp-ssl-ccc Send CCC after authenticating (F)
--ftp-ssl-ccc-mode ACTIVE/PASSIVE Set CCC mode (F)
--ftp-ssl-control Require SSL/TLS for ftp login, clear for transfer (F)
-l, --list-only List only names of an FTP directory (F)
-Q, --quote CMD Send command(s) to server before transfer (F/SFTP)

注意

官网 https://curl.se/ https://github.com/curl/curl
官方文档
https://curl.se/docs/manpage.html
https://curl.se/docs/httpscripting.html
https://curl.se/docs/manual.html

参考
https://www.cnblogs.com/doseoer/p/7044344.html
http://www.ruanyifeng.com/blog/2019/09/curl-reference.html

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
$ curl -V
curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.53.1 zlib/1.2.7 libidn/1.28 libssh2/1.8.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp
Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz unix-sockets

# URL匹配
字符串匹配 http://{www,ftp,mail}.a.com/
字符匹配 http://www.a.com/file[a-z].html
数字匹配 http://www.a.com/file[1-10].html

# 默认显示html源码
$ curl http://www.a.com
<!DOCTYPE html>
<!--STATUS OK--><html> <title>hello,world
...
# -I 显示响应头
$ curl -I http://www.a.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: Sat, 23 Oct 2021 14:34:11 GMT
Etag: "575e1f59-115"
Last-Modified: Mon, 13 Jun 2016 02:50:01 GMT
Pragma: no-cache
Server: nginx
# --compressed 压缩请求
$ curl -I --compressed http://www.a.com
HTTP/1.1 200 OK
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Encoding: gzip
...
# -o 保存文件,指定文件名
$ curl -o a.html https://www.a.com/index.html
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2443 100 2443 0 0 13313 0 --:--:-- --:--:-- --:--:-- 13349
$ ll a.html
-rw-r--r-- 1 root root 2443 Oct 24 13:35 a.html
# -O 保存文件,使用远程文件名
$ curl -O https://www.a.com/index.html
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2443 100 2443 0 0 14981 0 --:--:-- --:--:-- --:--:-- 15080
$ ll index.html
-rw-r--r-- 1 root root 2443 Oct 24 13:37 index.html
# 批量下载test1-test10
$ curl -O https://www.a.com/test[1-10].html
# -C - 自动识别上次传输中止的位置进行续传,最后的-不能少
$ curl -O -C - https://www.a.com/index.html
# -A 指定客户端UA
$ curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36" https://www.a.com/index.html
# -c 保存cookie
$ curl -c cookie.txt https://www.a.com/index.html
# -b 指定cookie
$ curl -b "user=admin,userid=1001" https://www.a.com/index.html
$ curl -b cookie.txt https://www.a.com/index.html
# -d 提交数据
$ curl -d "user=admin,password=admin" https://www.a.com/index.html
# -F 提交表单数据
$ curl -c cookie.txt -F login=admin -F password=admin https://www.a.com/index.html
# -D 保存header
$ curl -D header.txt https://www.a.com/index.html
# -e 指定referer
$ curl -e https://www.b.com/ https://www.a.com/index.html
# -f 显示curl错误信息而不显示错误输出
$ curl -f https://www.a.com/a.html
curl: (22) The requested URL returned error: 404 Not Found
# -H 指定header
$ curl -H "Content-Type: text/xml" https://www.a.com/index.html
# -i 同时显示header和响应信息
$ curl -i https://www.a.com/index.html
# -L 跟随重定向
$ curl -L http://www.a.com/
# -k 忽略ssl检查
$ curl -k https://www.a.com/
# -N 禁用缓存
$ curl -N https://www.a.com/index.html
# -x 使用代理
$ curl -x 123.45.67.89:1080 https://www.a.com/
# -X 指定请求方法
$ curl -X DELETE https://www.a.com/index.html
# -s 静默模式,配合-S可以显示错误信息
$ curl -s https://www.a.com/index.html
$ curl -sS https://www.a.com/index.html
# -w 显示指定信息,如http_code
$ curl -o /dev/null -s -w %{http_code} https://www.a.com/index.html
200
# -v 显示通信的具体过程
$ curl -v https://www.a.com/
# --trace 用于debug
$ curl --trace debug.txt https://www.a.com/index.html
$ more debug.txt
== Info: About to connect() to www.a.com port 443 (#0)
== Info: Trying 110.120.130.1...
== Info: Connected to www.a.com (110.120.130.1) port 443 (#0)
== Info: Initializing NSS with certpath: sql:/etc/pki/nssdb
== Info: CAfile: /etc/pki/tls/certs/ca-bundle.crt
CApath: none
== Info: SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
== Info: Server certificate:
== Info: subject: CN=a.com,O="a Technology Co., Ltd",O
U=IT,L=beijing,ST=beijing,C=CN
== Info: start date: Jul 01 01:16:03 2021 GMT
== Info: expire date: Aug 02 01:16:03 2022 GMT
== Info: common name: a.com
== Info: issuer: CN=GlobalSign Organization Validation CA - SHA256 - G2,O=GlobalSign
nv-sa,C=BE
=> Send header, 83 bytes (0x53)
...

# 获取域名ssl证书的过期时间,注意如果是自签名证书,需要将对应的CA证书添加到当前系统中,否则会报错 unknown CA (560),添加CA证书参考openssl命令
$ curl -Ivs https://www.a.com 2>&1|grep "expire date" |awk '{$1=$2=$3="";print}'
May 22 07:39:19 2023 GMT