Linux操作系统之curl命令详解

前言

①curl 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。

②curl 命令用作网络数据包收发,常应用于非交互式环境中。

  • 交互式模式:在终端上执行命令,shell解释器等待你的输入,并且立即执行你提交的命令(shell与用户进行交互)。这种模式也是大多数用户非常熟悉的:登录、执行一些命令、退出。当你退出后,shell也终止了。
  • 非交互式模式:非交互式模式,以shell script(非交互)方式执行。在这种模式下,shell不与你进行交互,而是读取存放在文件中的命令,并且执行它们。当它读到文件的结尾EOF,shell也就终止了。

③curl是一个命令行访问URL的工具,作用是发出网络请求,然后得到和提取数据,显示在"标准输出"(stdout)上面。可以用它构造http request报文,且可以解析服务器返回的http response,额外还支持cookie特性,可以用curl完成web浏览器的基本功能,curl还支持HTTPS/FTP/FTPS/TELNET/LDAP等协议。

④ 执行curl --help :查看curl工具的参数使用方法

test@test-Vostro-3888-China-HDD-Protection:~$ curl --help
Usage: curl [options...] <url>
     --abstract-unix-socket <path> Connect via abstract Unix domain socket
     --alt-svc <file name> Enable alt-svc with this cache file
     --anyauth       Pick any authentication method
 -a, --append        Append to target file when uploading
     --basic         Use HTTP Basic Authentication
     --cacert <file> CA certificate to verify peer against
     --capath <dir>  CA directory to verify peer against
 -E, --cert <certificate[:password]> Client certificate file and password
     --cert-status   Verify the status of the server certificate
     --cert-type <type> Certificate file type (DER/PEM/ENG)
     --ciphers <list of ciphers> SSL ciphers to use
     --compressed    Request compressed response
     --compressed-ssh Enable SSH compression
 -K, --config <file> Read config from a file
     --connect-timeout <seconds> Maximum time allowed for connection
     --connect-to <HOST1:PORT1:HOST2:PORT2> Connect to host
 -C, --continue-at <offset> Resumed transfer offset
 -b, --cookie <data|filename> Send cookies from string/file
 -c, --cookie-jar <filename> Write cookies to <filename> after operation
     --create-dirs   Create necessary local directory hierarchy
     --crlf          Convert LF to CRLF in upload
     --crlfile <file> Get a CRL list in PEM format from the given file
 -d, --data <data>   HTTP POST data
     --data-ascii <data> HTTP POST ASCII data
     --data-binary <data> HTTP POST binary data
     --data-raw <data> HTTP POST data, '@' allowed
     --data-urlencode <data> HTTP POST data url encoded
     --delegation <LEVEL> GSS-API delegation permission
     --digest        Use HTTP Digest Authentication
 -q, --disable       Disable .curlrc
     --disable-eprt  Inhibit using EPRT or LPRT
     --disable-epsv  Inhibit using EPSV
     --disallow-username-in-url Disallow username in url
     --dns-interface <interface> Interface to use for DNS requests
     --dns-ipv4-addr <address> IPv4 address to use for DNS requests
     --dns-ipv6-addr <address> IPv6 address to use for DNS requests
     --dns-servers <addresses> DNS server addrs to use
     --doh-url <URL> Resolve host names over DOH
 -D, --dump-header <filename> Write the received headers to <filename>
     --egd-file <file> EGD socket path for random data
     --engine <name> Crypto engine to use
     --etag-save <file> Get an ETag from response header and save it to a FILE
     --etag-compare <file> Get an ETag from a file and send a conditional request
     --expect100-timeout <seconds> How long to wait for 100-continue
 -f, --fail          Fail silently (no output at all) on HTTP errors
     --fail-early    Fail on first transfer error, do not continue
     --false-start   Enable TLS False Start
 -F, --form <name=content> Specify multipart MIME data
     --form-string <name=string> Specify multipart MIME data
     --ftp-account <data> Account data string
     --ftp-alternative-to-user <command> String to replace USER [name]
     --ftp-create-dirs Create the remote dirs if not present
     --ftp-method <method> Control CWD usage
     --ftp-pasv      Use PASV/EPSV instead of PORT
 -P, --ftp-port <address> Use PORT instead of PASV
     --ftp-pret      Send PRET before PASV
     --ftp-skip-pasv-ip Skip the IP address for PASV
     --ftp-ssl-ccc   Send CCC after authenticating
     --ftp-ssl-ccc-mode <active/passive> Set CCC mode
     --ftp-ssl-control Require SSL/TLS for FTP login, clear for transfer
 -G, --get           Put the post data in the URL and use GET
 -g, --globoff       Disable URL sequences and ranges using {} and []
     --happy-eyeballs-timeout-ms <milliseconds> How long to wait in milliseconds for IPv6 before trying IPv4
     --haproxy-protocol Send HAProxy PROXY protocol v1 header
 -I, --head          Show document info only
 -H, --header <header/@file> Pass custom header(s) to server
 -h, --help          This help text
     --hostpubmd5 <md5> Acceptable MD5 hash of the host public key
     --http0.9       Allow HTTP 0.9 responses
 -0, --http1.0       Use HTTP 1.0
     --http1.1       Use HTTP 1.1
     --http2         Use HTTP 2
     --http2-prior-knowledge Use HTTP 2 without HTTP/1.1 Upgrade
     --http3         Use HTTP v3
     --ignore-content-length Ignore the size of the remote resource
 -i, --include       Include protocol response headers in the output
 -k, --insecure      Allow insecure server connections when using SSL
     --interface <name> Use network INTERFACE (or address)
 -4, --ipv4          Resolve names to IPv4 addresses
 -6, --ipv6          Resolve names to IPv6 addresses
 -j, --junk-session-cookies Ignore session cookies read from file
     --keepalive-time <seconds> Interval time for keepalive probes
     --key <key>     Private key file name
     --key-type <type> Private key file type (DER/PEM/ENG)
     --krb <level>   Enable Kerberos with security <level>
     --libcurl <file> Dump libcurl equivalent code of this command line
     --limit-rate <speed> Limit transfer speed to RATE
 -l, --list-only     List only mode
     --local-port <num/range> Force use of RANGE for local port numbers
 -L, --location      Follow redirects
     --location-trusted Like --location, and send auth to other hosts
     --login-options <options> Server login options
     --mail-auth <address> Originator address of the original email
     --mail-from <address> Mail from this address
     --mail-rcpt <address> Mail to this address
 -M, --manual        Display the full manual
     --max-filesize <bytes> Maximum file size to download
     --max-redirs <num> Maximum number of redirects allowed
 -m, --max-time <seconds> Maximum time allowed for the transfer
     --metalink      Process given URLs as metalink XML file
     --negotiate     Use HTTP Negotiate (SPNEGO) authentication
 -n, --netrc         Must read .netrc for user name and password
     --netrc-file <filename> Specify FILE for netrc
     --netrc-optional Use either .netrc or URL
 -:, --next          Make next URL use its separate set of options
     --no-alpn       Disable the ALPN TLS extension
 -N, --no-buffer     Disable buffering of the output stream
     --no-keepalive  Disable TCP keepalive on the connection
     --no-npn        Disable the NPN TLS extension
     --no-progress-meter Do not show the progress meter
     --no-sessionid  Disable SSL session-ID reusing
     --noproxy <no-proxy-list> List of hosts which do not use proxy
     --ntlm          Use HTTP NTLM authentication
     --ntlm-wb       Use HTTP NTLM authentication with winbind
     --oauth2-bearer <token> OAuth 2 Bearer Token
 -o, --output <file> Write to file instead of stdout
 -Z, --parallel      Perform transfers in parallel
     --parallel-immediate Do not wait for multiplexing (with --parallel)
     --parallel-max  Maximum concurrency for parallel transfers
     --pass <phrase> Pass phrase for the private key
     --path-as-is    Do not squash .. sequences in URL path
     --pinnedpubkey <hashes> FILE/HASHES Public key to verify peer against
     --post301       Do not switch to GET after following a 301
     --post302       Do not switch to GET after following a 302
     --post303       Do not switch to GET after following a 303
     --preproxy [protocol://]host[:port] Use this proxy first
 -#, --progress-bar  Display transfer progress as a bar
     --proto <protocols> Enable/disable PROTOCOLS
     --proto-default <protocol> Use PROTOCOL for any URL missing a scheme
     --proto-redir <protocols> Enable/disable PROTOCOLS on redirect
 -x, --proxy [protocol://]host[:port] Use this proxy
     --proxy-anyauth Pick any proxy authentication method
     --proxy-basic   Use Basic authentication on the proxy
     --proxy-cacert <file> CA certificate to verify peer against for proxy
     --proxy-capath <dir> CA directory to verify peer against for proxy
     --proxy-cert <cert[:passwd]> Set client certificate for proxy
     --proxy-cert-type <type> Client certificate type for HTTPS proxy
     --proxy-ciphers <list> SSL ciphers to use for proxy
     --proxy-crlfile <file> Set a CRL list for proxy
     --proxy-digest  Use Digest authentication on the proxy
     --proxy-header <header/@file> Pass custom header(s) to proxy
     --proxy-insecure Do HTTPS proxy connections without verifying the proxy
     --proxy-key <key> Private key for HTTPS proxy
     --proxy-key-type <type> Private key file type for proxy
     --proxy-negotiate Use HTTP Negotiate (SPNEGO) authentication on the proxy
     --proxy-ntlm    Use NTLM authentication on the proxy
     --proxy-pass <phrase> Pass phrase for the private key for HTTPS proxy
     --proxy-pinnedpubkey <hashes> FILE/HASHES public key to verify proxy with
     --proxy-service-name <name> SPNEGO proxy service name
     --proxy-ssl-allow-beast Allow security flaw for interop for HTTPS proxy
     --proxy-tls13-ciphers <list> TLS 1.3 ciphersuites for proxy (OpenSSL)
     --proxy-tlsauthtype <type> TLS authentication type for HTTPS proxy
     --proxy-tlspassword <string> TLS password for HTTPS proxy
     --proxy-tlsuser <name> TLS username for HTTPS proxy
     --proxy-tlsv1   Use TLSv1 for HTTPS proxy
 -U, --proxy-user <user:password> Proxy user and password
     --proxy1.0 <host[:port]> Use HTTP/1.0 proxy on given port
 -p, --proxytunnel   Operate through an HTTP proxy tunnel (using CONNECT)
     --pubkey <key>  SSH Public key file name
 -Q, --quote         Send command(s) to server before transfer
     --random-file <file> File for reading random data from
 -r, --range <range> Retrieve only the bytes within RANGE
     --raw           Do HTTP "raw"; no transfer decoding
 -e, --referer <URL> Referrer URL
 -J, --remote-header-name Use the header-provided filename
 -O, --remote-name   Write output to a file named as the remote file
     --remote-name-all Use the remote file name for all URLs
 -R, --remote-time   Set the remote file's time on the local output
 -X, --request <command> Specify request command to use
     --request-target Specify the target for this request
     --resolve <host:port:address[,address]...> Resolve the host+port to this address
     --retry <num>   Retry request if transient problems occur
     --retry-connrefused Retry on connection refused (use with --retry)
     --retry-delay <seconds> Wait time between retries
     --retry-max-time <seconds> Retry only within this period
     --sasl-authzid <identity>  Use this identity to act as during SASL PLAIN authentication
     --sasl-ir       Enable initial response in SASL authentication
     --service-name <name> SPNEGO service name
 -S, --show-error    Show error even when -s is used
 -s, --silent        Silent mode
     --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-gssapi-nec Compatibility with NEC SOCKS5 server
     --socks5-gssapi-service <name> SOCKS5 proxy service name for GSS-API
     --socks5-hostname <host[:port]> SOCKS5 proxy, pass host name to proxy
 -Y, --speed-limit <speed> Stop transfers slower than this
 -y, --speed-time <seconds> Trigger 'speed-limit' abort after this time
     --ssl           Try SSL/TLS
     --ssl-allow-beast Allow security flaw to improve interop
     --ssl-no-revoke Disable cert revocation checks (Schannel)
     --ssl-reqd      Require SSL/TLS
 -2, --sslv2         Use SSLv2
 -3, --sslv3         Use SSLv3
     --stderr        Where to redirect stderr
     --styled-output Enable styled output for HTTP headers
     --suppress-connect-headers Suppress proxy CONNECT response headers
     --tcp-fastopen  Use TCP Fast Open
     --tcp-nodelay   Use the TCP_NODELAY option
 -t, --telnet-option <opt=val> Set telnet option
     --tftp-blksize <value> Set TFTP BLKSIZE option
     --tftp-no-options Do not send any TFTP options
 -z, --time-cond <time> Transfer based on a time condition
     --tls-max <VERSION> Set maximum allowed TLS version
     --tls13-ciphers <list> TLS 1.3 ciphersuites (OpenSSL)
     --tlsauthtype <type> TLS authentication type
     --tlspassword   TLS password
     --tlsuser <name> TLS user name
 -1, --tlsv1         Use TLSv1.0 or greater
     --tlsv1.0       Use TLSv1.0 or greater
     --tlsv1.1       Use TLSv1.1 or greater
     --tlsv1.2       Use TLSv1.2 or greater
     --tlsv1.3       Use TLSv1.3 or greater
     --tr-encoding   Request compressed transfer encoding
     --trace <file>  Write a debug trace to FILE
     --trace-ascii <file> Like --trace, but without hex output
     --trace-time    Add time stamps to trace/verbose output
     --unix-socket <path> Connect through this Unix domain socket
 -T, --upload-file <file> Transfer local FILE to destination
     --url <url>     URL to work with
 -B, --use-ascii     Use ASCII/text transfer
 -u, --user <user:password> Server user and password
 -A, --user-agent <name> Send User-Agent <name> to server
 -v, --verbose       Make the operation more talkative
 -V, --version       Show version number and quit
 -w, --write-out <format> Use output FORMAT after completion
     --xattr         Store metadata in extended file attributes

⑤执行 curl --manual :查看curl工具的详细介绍和用法(此处省略打印);或者查看Curl Cookbook

⑥curl命令是一个运用URL规则在shell终端命令行下工作的文件传输工具;它适用文件的上传和下载,因此是综合传输工具,但按传统,习惯称curl为下载工具。

curl工具的参数

linux操作系统中的curl功能十分强大,所以命令参数十分多,下表只是爱E族(aiezu.com)帅选出来的部分参数,更多参数运行“man curl”命令查看。

参数组参数描述
url url 需要抓取的一到多个URLs;
多个下面通配符的方式:
  1、http://{www,ftp,mail}.aiezu.com;
  2、http://aiezu.com/images/[001-999].jpg;
  3、http://aiezu.com/images/[1-999].html;
  4、ftp://aiezu.com/file[a-z].txt


-H "name: value"
--header "name: value"
(HTTP)添加一个http header(http请求头);
-H "name:"
--header "name:"
(HTTP)移除一个http header(http请求头);
-A "string"
--user-agent "string"
【参考】
(HTTP)设置Http请求头“User-Agent”,服务器通过“User-Agent”可以判断客户端使用的浏览器名称和操作系统类型,伪造此参数能导致服务器做出错误判断。
也可以使用“-H”, “--header option”设置此选项;
-e <URL>
--referer <URL>
【参考】
(HTTP)设置访问时的来源页面,告诉http服务从哪个页面进入到此页面;
-e "aiezu.com"相当于“-H "Referer: www.qq.com"”;


-I
--head
(HTTP)只输出HTTP-header,不获取内容(HTTP/FTP/FILE)。
用于HTTP服务时,获取页面的http头;
  (如:curl -I http://aiezu.com)
用于FTP/FILE时,将会获取文件大小、最后修改时间;
  (如:curl -I file://test.txt)
-i
--include
(HTTP)输出HTTP头和返回内容;
-D <file>
--dump-header <file>
(HTTP)转储http响应头到指定文件;
cookie -b name=data
--cookie name=data
【参考】
(HTTP)发送cookie数据到HTTP服务器,数据格式为:"NAME1=VALUE1; NAME2=VALUE2";

如果行中没有“=”,将把参数值当作cookie文件名;

这个cookie数据可以是由服务器的http响应头“Set-Cookie:”行发送过来的;
-c filename
--cookie-jar file name
【参考】
(HTTP)完成操作后将服务器返回的cookies保存到指定的文件;
指定参数值为“-”将定向到标准输出“如控制台”;
-j
--junk-session-cookies
(HTTP)告诉curl放弃所有的"session cookies";
相当于重启浏览器;
代理 -x host:port
-x [protocol://[user:pwd@]host[:port]
--proxy [protocol://[user:pwd@]host[:port]
【参考】
使用HTTP代理访问;如果未指定端口,默认使用8080端口;
protocol默认为http_proxy,其他可能的值包括:
http_proxy、HTTPS_PROXY、socks4、socks4a、socks5;
如:
--proxy 8.8.8.8:8080;
-x "http_proxy://aiezu:123@aiezu.com:80"
-p
--proxytunnel
将“-x”参数的代理,作为通道的方式去代理非HTTP协议,如ftp;
--socks4 <host[:port]>
--socks4a <host[:port]>
--socks5 <host[:port]>
【参考】
使用SOCKS4代理;
使用SOCKS4A代理;
使用SOCKS5代理;
此参数会覆盖“-x”参数;
--proxy-anyauth
--proxy-basic
--proxy-diges
--proxy-negotiate
--proxy-ntlm
http代理认证方式,参考:
--anyauth
--basic
--diges
--negotiate
--ntlm
-U <user:password>
--proxy-user <user:password>
设置代理的用户名和密码;
数据
传输
-G
--get
【参考】
如果使用了此参数,“-d/”、“--data”、“--data-binary”参数设置的数据,讲附加在url上,以GET的方式请求; 
-d @file
-d "string"
--data "string"
--data-ascii "string"
--data-binary "string"
--data-urlencode "string"
【参考】
(HTTP)使用HTTP POST方式发送“key/value对”数据,相当于浏览器表单属性(method="POST",enctype="application/x-www-form-urlencoded")
  -d,--data:HTTP方式POST数据;
  --data-ascii:HTTP方式POST ascii数据;
  --data-binary:HTTP方式POST二进制数据;
  --data-urlencode:HTTP方式POST数据(进行urlencode);
如果数据以“@”开头,后紧跟一个文件,将post文件内的内容;
-F name=@file
-F name=<file
-F name=content
--form name=content
【参考】
(HTTP)使用HTTP POST方式发送类似“表单字段”的多类型数据,相当于同时设置浏览器表单属性(method="POST",enctype="multipart/form-data"),可以使用此参数上传二进制文件。

如果字段内容以“@”开头,剩下的部分应该是文件名,curl将会上传此文件,如:
curl -F "pic=@pic.jpg" http://aiezu.com;
curl -F "page=@a.html;type=text/html" http://aiezu.com
curl -F "page=@/tmp/a;filename=a.txt" http://aiezu.com

如果字段内容以“<”开头,剩下的部分应该是文件名,curl将从文件中获取作为此字段的值,如:curl -F "text=<text.txt" http://aiezu.com;
--form-string <key=value> (HTTP)类似于“--form”,但是“@”、“<”无特殊含义;
-T file
--upload-file file
通过“put”的方式将文件传输到远程网址;

选项参数只使用字符"-",将通过stdin读入文件内容;
如:
cat test.txt|curl "http://aiezu.com/a.php" -T - 
curl "http://aiezu.com/a.php" -T - <test.txt

此参数也可以使用通配符:
curl -T "{file1,file2}" http://aiezu.com
curl -T "img[1-1000].png" http://aiezu.com
断点
续传
-C <offset>
--continue-at <offset>
断点续转,从文件头的指定位置开始继续下载/上传;
offset续传开始的位置,如果offset值为“-”,curl会自动从文件中识别起始位置开始传输;
-r <range>
--range <range>
(HTTP/FTP/SFTP/FILE) 只传输内容的指定部分:
0-499:最前面500字节;
-500:最后面500字节;
9500-:最前面9500字节;
0-0,-1:最前面和最后面的1字节;
100-199,500-599:两个100字节;



认证
--basic (HTTP)告诉curl使用HTTP Basic authentication(HTTP协议时),这是默认认证方式;
--ntlm (HTTP)使用NTLM身份验证方式,用于HTTP协议;
一般用于IIS使用NTLM的网站;
--digest (HTTP)使用HTTP Digest authentication加密,用于HTTP协议;
配合“-u/--user”选项,防止密码使用明文方式发送;
--negotiate (HTTP)使用GSS-Negotiate authentication方式,用于HTTP协议;
它主要目的是为它的主要目的是为kerberos5认证提供支持支持;
--anyauth (HTTP)告诉curl自动选择合适的身份认证方法,并选用最安全的方式;
-u user:password
--user user:password
使用用户名、密码认证,此参数会覆盖“-n”、“--netrc”和“--netrc-optional”选项;

如果你只提供用户名,curl将要求你输入密码;

如果你使用“SSPI”开启的curl库做“NTLM”认证,可以使用不含用户名密码的“-u:”选项,强制curl使用当前登录的用户名密码进行认证;

此参数相当于设置http头“Authorization:”;
证书 -E <证书[:密码]>
--cert <证书[:密码]>
(SSL)指定“PEM”格式的证书文件和证书密码;
--cert-type <type> (SSL)告诉curl所提供证书的类型:PEM、DER、ENG等;
默认为“PEM”;
--cacert <CA证书> (SSL)告诉curl所以指定的CA证书文件,必须是“PEM”格式;
--capath <CA证书路径> (SSL)告诉curl所以指定目录下的CA证书用来验证;
这些证书必须是“PEM”格式;
--crlfile <file> (HTTPS/FTPS)提供一个PEM格式的文件,用于指定被吊销的证书列表;
-k
--insecure
(SSL)设置此选项将允许使用无证书的不安全SSL进行连接和传输。
SSL
其他
--ciphers <list of ciphers> (SSL)指定SSL要使用的加密方式;如:“aes_256_sha_256”;
--engine <name> 设置一个OpenSSL加密引擎用于加密操作;
使用“curl --engine list”查看支持的加密引擎列表;
--random-file (SSL)指定包含随机数据的文件路径名;数据是用来为SSL连接产生随机种子为;
--egd-file <file> (SSL)为随机种子生成器EGD(Entropy Gathering Daemon socket)指定的路径名;
-1/--tlsv1
--tlsv1.0
--tlsv1.1
--tlsv1.2
-2/--sslv2
-3/--sslv3
(SSL)使用TLS版本2与远程服务器通讯;
(SSL)使用TLS 1.0版本与远程服务器通讯;
(SSL)使用TLS 1.1版本与远程服务器通讯;
(SSL)使用TLS 1.2版本与远程服务器通讯;
(SSL)使用SSL版本2与远程服务器通讯;
(SSL)使用SSL版本3与远程服务器通讯;
私钥
公钥
--key <key> (SSL/SSH)指定一个私钥文件名;为指定时自动尝试使用下面文件:“~/.ssh/id_rsa”、“~/.ssh/id_dsa”、“./id_rsa'”、 “./id_dsa”;
--key-type <type> (SSL)指定私钥文件类型,支持:DER、PEM、ENG,默认是PEM;
--pass <phrase> (SSL/SSH)指定私钥文件的密码;
--pubkey <key> (SSH)使用指定文件提供的您公钥;
FTP -P
--ftp-port <接口>
(FTP)FTP主动模式时,设置一个地址等待服务器的连接,如:
网卡:eth1
IP:8.8.8.8
主机名:aiezu.com
可以加端口号:eth1:20000-21000;
--crlf (FTP)上传时将换行符(LF)转换为回车换行(CRLF);
--ftp-account [data] (FTP)ftp帐号信息;
--ftp-method [method] (FTP)可选值:multicwd/nocwd/singlecwd;
--ftp-pasv (FTP)使用使用PASV(被动)/EPSV模式;
--ftp-skip-pasv-ip (FTP)使用PASV的时,跳过指定IP;
--ftp-create-dirs (FTP)上传时自动创建远程目录;
-l
--list-only
(FTP)列出ftp文件列表;
-B
--use-ascii
(FTP/LDAP)使用Ascii传输模式,用于FTP、LDAP;在ftp中相当与使用了“type=A;”模式。
--disable-epsv (FTP)告诉curl在PASV(被动模式)时不要使用EPSV;
--disable-eprt (FTP)告诉curl在主动模式时禁用EPRT和LPRT;
限速 --limit-rate <speed> 限制curl使用的最大带宽;如果未指定单位,默认单位为“bytes/秒”,你也可以指定单位为“K”、“M”、“G”等单位,如:“--limit-rate 1m”为限制最大使用带宽为“1m字节/秒”;
-y
--speed-time <time>
If a download is slower than speed-limit bytes per second during a speed-time period, the download gets aborted. If speed-time is used, the default speed-limit will be 1 unless set with -Y.
This option controls transfers and thus will not affect slow connects etc. If this is a concern for you, try the --connect-timeout option.
-Y
--speed-limit <speed>
If a download is slower than this given speed (in bytes per second) for speed-time seconds it gets aborted. speed-time is set with -y and is 30 if not set.
其他
选项
-0/--http1.0 (HTTP) 强制curl使用HTTP 1.0而不是使用默认的HTTP 1.1;
--interface <name> 使用指定的网卡接口访问;
curl --interface eth0 http://aiezu.com
curl --interface 10.0.0.101 http://aiezu.com
-X <command>
--request <command>
(HTTP)指定与服务器通信使用的请求方法,如:GET、PUT、POST、DELETE等,默认GET;
--keepalive-time <seconds> 设置keepalive时间
--no-keepalive 关闭keepalive功能;
--no-buffer 禁用对输出流缓冲;
--buffer 启用输出流缓冲;
-L
--location
(HTTP/HTTPS)追随http响应头“Location:”定向到跳转后的页面;
(在http响应码为3XX时使用,如301跳转、302跳转)
--location-trusted (HTTP/HTTPS)同“--location”,但跳转后会发送跳转前的用户名和密码;
--compressed (HTTP)请求对返回内容使用压缩算法进行压缩;curl支持对gzip压缩进行解压;
--connect-timeout <seconds> 指定最大连接超时,单位“秒”;
-m seconds
--max-time seconds
限制整个curl操作的最长时间,单位为秒;
-s
--silent
安静模式。不要显示进度表或错误消息;
-#
--progress-bar
显示进度条;
错误
选项
-f
--fail
(HTTP)连接失败时(400以上错误)不返回默认错误页面,而是返回一个curl错误码“22”;
--retry <num>
--retry-delay <seconds>
--retry-max-time <seconds>
失败重试次数;
重试间隔时间;
最大重试时间;
-S
--show-error
安静模式下显示错误信息;
--stderr <file> 错误信息保存文件;
输出 -o file
--output file
将返回内容输出到文件。
如果是用过通配符获取多个url,可以使用“#”后跟“数字序号”,curl会自动将它替换对应的关键词,如:
  curl "http://aiezu.com/{a,b}.txt" -o "#1.txt";
  将保存为:“a.txt”,“b.txt”;

  curl "http://aiezu.com/{a,b}_[1-3].txt" -o "#1#2.txt";
  将保存为:a1.txt、a2.txt、a3.txt、b1.txt、b2.txt、b3.txt

  如果要根据规则创建保存目录,参考:“--create-dirs”

指定“-”将定向到标准输出“如控制台”; 
-O
--remote-name
将返回内容输出到当前目录下,和url中文件名相同的文件中(不含目录);
--create-dirs 与“-o”参数配合使用,创建必要的本地目录层次结构
-w
--write-out format
操作完成后在返回信息尾部追加指定的内容;要追加的内容可以是一个字符串“string”、从文件中获取“@filename”、从标准输入中获取“@-”

格式参数中可以用%{variable_name} 方式使用响应信息的相关变量,如:%{content_type}、%{http_code}、%{local_ip}...,更多变量参考“man curl”获取;

格式参数可以使用“\n”、“\r”、“\t”等转义字符;
调试 --trace <file> 转储所有传入和传出的数据到文件,包括描述信息;
使用“-”作为文件名将输出发送到标准输出。
--trace-ascii file 转储所有传入和传出的数据到文件,包括描述信息,只转储ASCII部分,更容易阅读;
使用“-”作为文件名将输出发送到标准输出。
这个选项会覆盖之前使用的-v、 --verbose、 --trace-ascii选项;
--trace-time 转储文件中添加时间信息;
-K
--config <config file>
从配置文件中读取参数,参考:http://curl.haxx.se/docs/
-v
--verbose
显示更详细的信息,调试时使用;
帮助 -M
--manual
显示完整的帮助手册;
-h
--help
linux curl用法帮助;

Linux操作系统curl工具命令退出代码

    1    curl 不支持该协议 
    2    curl 初始化失败 
    3    URL 格式错误 
    5    解析代理服务器失败 
    6    解析主机失败 
    7    建立与主机的连接失败 
    8    无法解析 FTP 服务器返回的消息 
    9    FTP 服务器拒接访问。可能是拒绝登录或拒绝访问特定目录,但很多情况下是访问了一个不存在的位置导致的 
    11    无法解析 FTP 服务器的 PASS 回复消息 
    13    无法解析 FTP 服务器的 PASV 回复消息 
    14    无法解析 FTP 服务器的 227-line 回复消息 
    15    无法解析 FTP 主机 
    17    无法与 FTP 服务器建立二进制传输模式 
    18    文件传输不完整。只有文件的一部分被传送了。 
    19    FTP 下载/访问指定的文件失败 
    21    FTP 引用错误 
    22    HTTP 页面获取失败。将返回 400 及其以上的错误码。 
    23    写入数据到本地文件系统发生错误 
    25    FTP 服务器无法存储被上传的文件 
    26    读取数据出错 
    27    内存分配失败 
    28    操作超时 
    30    FTP 服务器运行 PORT 命令失败 
    31    FTP 服务器运行 REST 命令失败 
    33    HTTP 服务器执行 range 命令失败 
    34    HTTP 服务器 post 方法错误 
    35    SSL 连接出错。通常是 SSL 握手失败。 
    36    FTP 断点续传出错,无法继续前一次下载任务。 
    37    FILE 协议无法打开文件。可能是没有权限导致的 
    38    LDAP 协议无法执行 bind 操作 
    39    LDAP 协议执行搜索功能失败 
    41    没有找到匹配的 LDAP 功能 
    42    curl 被其它应用程序中止运行 
    43    发生内部错误。通常是因为调用函数时传递了错误的参数 
    45    接口调用错误 
    47    跳转链接数量达到上限 
    48    为 telnet 协议指定了一个未知的参数 
    49    telnet 协议的参数格式有误 
    51    SSL 认证或 SSH 的 MD5 指纹不正确 
    52    服务器无应答 
    53    未找到 SSL 加密引擎 
    54    无法将 SSL 加密引擎设为默认引擎 
    55    发送网络数据失败 
    56    接收网络数据失败 
    58    本地证书有问题 
    59    无法使用指定的 SSL 密码 
    60    已知的 CA证书无法用于验证 
    61    无法识别传输编码 
    62    无效的 LDAP URL 
    63    文件大小超过限制 
    64    请求的 FTP SSL 级别获取失败 
    65    发送需要进行 rewind 的数据失败 
    66    解析 SSL 引擎失败 
    67    用户名/密码验证失败,curl 无法登录 
    68    TFTP 服务器上没有找到指定的文件 
    69    对 TFTP 服务器的访问出现权限问题 
    70    TFTP 服务器已耗尽磁盘空间 
    71    非法的 TFTP 操作 
    72    未知的 TFTP 传输 ID 
    73    TFTP 企图传输一个已经存在的文件 
    74    用户不存在导致 TFTP 操作失败 
    75    字符转换失败 
    76    需要使用字符转换函数 
    77    读取 SSL CA 证书出错。可能是证书路径不正确,也可能是没有权限 
    78    URL 中指定的资源不存在 
    79    SSH 会话中发生了未知错误 
    80    中断 SSH 连接失败 
    82    无法加载 CRL 文件。可能是没有指定正确的格式 
    83    发布者身份验证失败

curl工具的常用命令

1、curl工具不带有任何参数时,curl 默认发出 GET 请求。

curl https://www.baidu.com

运行结果:

curl工具发送带有参数的GET请求

curl URL?a=1&b=nihao

2、curl工具发送POST请求

curl -X POST -d 'a=1&b=nihao' URL

curl工具发送json格式请求

curl -H "Content-Type: application/json" -X POST -d '{"abc":123,"bcd":"nihao"}' URL

其中:

①-H:代表request header头

②-X:代表指定request method(POST/GET/HEAD/DELETE/PUT/PATCH)

③-d:代表指定request data(json格式/form表单) 

curl工具的常见用法(结合参数)

-A:-A参数指定客户端的用户代理标头,即User-Agent。curl 的默认用户代理字符串是curl/[version]

例1:将User-Agent改成 Chrome 浏览器。

curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://google.com

例2:移除User-Agent标头。

curl -A '' https://google.com

例3:通过-H参数直接指定标头,更改User-Agent

curl -H 'User-Agent: php/1.0' https://google.com

-b:-b参数用来向服务器发送 Cookie。

例1:生成一个标头Cookie: foo=bar,向服务器发送一个名为foo、值为bar的 Cookie。

curl -b 'foo1=bar;foo2=bar2' https://google.com

例2:发送两个 Cookie。

curl -b 'foo1=bar;foo2=bar2' https://google.com

例3:读取本地文件cookies.txt,里面是服务器设置的 Cookie(参见-c参数),将其发送到服务器。

curl -b cookies.txt https://www.google.com

-c:-c参数将服务器设置的 Cookie 写入一个文件。

例1:将服务器的 HTTP 回应所设置 Cookie 写入文本文件cookies.txt

curl -c cookies.txt https://www.google.com

-d:-d参数用于发送 POST 请求的数据体

例1:使用-d参数以后,HTTP 请求会自动加上标头Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略-X POST

curl -d'login=emma&password=123'-X POST https://google.com/login
# 或者
curl -d 'login=emma' -d 'password=123' -X POST  https://google.com/login

例2:-d参数可以读取本地文本文件的数据,向服务器发送。(取data.txt文件的内容,作为数据体向服务器发送。)

curl -d '@data.txt' https://google.com/login

--data-urlencode:--data-urlencode参数等同于-d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。

例1:发送的数据hello world之间有一个空格,需要进行 URL 编码。

curl --data-urlencode 'comment=hello world' https://google.com/login

-e:-e参数用来设置 HTTP 的标头Referer,表示请求的来源。

例1:将Referer标头设为https://google.com?q=example

curl -e 'https://google.com?q=example' https://www.example.com

例2:-H参数可以通过直接添加标头Referer,达到同样效果

curl -H 'Referer: https://google.com?q=example' https://www.example.com

-F:-F参数用来向服务器上传二进制文件。

例1:给 HTTP 请求加上标头Content-Type: multipart/form-data,然后将文件photo.png作为file字段上传。

curl -F 'file=@photo.png' https://google.com/profile

例2:-F参数可以指定 MIME 类型。(指定 MIME 类型为image/png,否则 curl 会把 MIME 类型设为application/octet-stream。)

curl -F 'file=@photo.png;type=image/png' https://google.com/profile

例3:-F参数也可以指定文件名。(原始文件名为photo.png,但是服务器接收到的文件名为me.png。)

curl -F 'file=@photo.png;filename=me.png' https://google.com/profile

-G:-G参数用来构造 URL 的查询字符串。

例1:发出一个 GET 请求,实际请求的 URL 为https://google.com/search?q=kitties&count=20。如果省略-G,会发出一个 POST 请求。

curl -G -d 'q=kitties' -d 'count=20' https://google.com/search

例2:如果数据需要 URL 编码,可以结合--data--urlencode参数。

curl -G --data-urlencode 'comment=hello world' https://www.example.com

-H:-H参数添加 HTTP 请求的标头。

例1:添加 HTTP 标头Accept-Language: en-US

curl -H 'Accept-Language: en-US' https://google.com

例2:添加两个 HTTP 标头。

curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com

例3:添加 HTTP 请求的标头Content-Type: application/json,然后用-d参数发送 JSON 数据。

curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login

-i:-i参数打印出服务器回应的 HTTP 标头。

例1:收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页的源码。

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

-I:-I参数向服务器发出 HEAD 请求,然会将服务器返回的 HTTP 标头打印出来。

例1:输出服务器对 HEAD 请求的回应。

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

例2:--head参数等同于-I

curl --head https://www.example.com

-k:-k参数指定跳过 SSL 检测。

例1:不会检查服务器的 SSL 证书是否正确。

curl -k https://www.example.com

-L:-L参数会让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。

例1:

curl -L -d 'tweet=hi' https://api.twitter.com/tweet

--limit-rate:--limit-rate用来限制 HTTP 请求和回应的带宽,模拟慢网速的环境。

例1:将带宽限制在每秒 200K 字节。

curl --limit-rate 200k https://google.com

-o:-o参数将服务器的回应保存成文件,等同于wget命令

例1:将www.example.com的响应内容保存成example.html

curl -o example.html https://www.example.com

-O:-O参数将服务器回应保存成文件,并将 URL 的最后部分当作文件名。

例1:将服务器回应保存成文件,文件名为bar.html

curl -O https://www.example.com/foo/bar.html

-s:-s参数将不输出错误和进度信息

例1:一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果。

curl -s https://www.example.com

例2: curl 不产生任何输出

curl -s -o /dev/null https://google.com

-S:-S参数指定只输出错误信息,通常与-s一起使用。

例1:没有任何输出,除非发生错误。

curl -s -o /dev/null https://google.com

-u:-u参数用来设置服务器认证的用户名和密码

例1:设置用户名为bob,密码为12345,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1

curl -u 'bob:12345' https://google.com/login

-v:-v参数输出通信的整个过程,用于调试

例1:

curl -v https://www.example.com

-x:-x参数指定 HTTP 请求的代理

例1:指定 HTTP 请求通过myproxy.com:8080的 socks5 代理发出。

curl -x socks5://james:cats@myproxy.com:8080 https://www.example.com

例2:如果没有指定代理协议,默认为 HTTP。(下面命令中,请求的代理使用 HTTP 协议)

curl -x james:cats@myproxy.com:8080 https://www.example.com

-X:-X参数指定 HTTP 请求的方法。

例1:下面命令对https://www.example.com发出 POST 请求。

curl -X POST https://www.example.com

--interface: 本机存在多网卡的情况下,使用curl工具指定网卡发出请求

例1:curl - -interface enp2s0 www.baidu.com  指定网卡发curl请求

 

参考链接:

https://blog.csdn.net/angle_chen123/article/details/120675472

https://blog.csdn.net/weixin_34417272/article/details/116748274

https://www.cnblogs.com/leoyang63/articles/13902937.html

posted @ 2022-11-10 17:27  习久性成  阅读(12981)  评论(0编辑  收藏  举报