@ -11,220 +11,190 @@ curl为下载工具。作为一款强力工具, curl支持包括HTTP、HTTPS、
## 选项
## 选项
```markdown
```markdown
-a/--append 上传文件时,附加到目标文件
(H) means HTTP/HTTPS only, (F) means FTP only
-A/--user-agent 设置用户代理发送给服务器
-anyauth 可以使用“任何”身份验证方法
--anyauth Pick "any" authentication method (H)
-b/--cookie cookie字符串或文件读取位置
-a, --append Append to target file when uploading (F/SFTP)
--basic 使用HTTP基本验证
--basic Use HTTP Basic Authentication (H)
-B/--use-ascii 使用ASCII /文本传输
--cacert FILE CA certificate to verify peer against (SSL)
-c/--cookie-jar 操作结束后把cookie写入到这个文件中
--capath DIR CA directory to verify peer against (SSL)
-C/--continue-at 断点续传
-E, --cert CERT[:PASSWD] Client certificate file and password (SSL)
-d/--data HTTP POST方式传送数据
--cert-type TYPE Certificate file type (DER/PEM/ENG) (SSL)
--data-ascii 以ascii的方式post数据
--ciphers LIST SSL ciphers to use (SSL)
--data-binary 以二进制的方式post数据
--compressed Request compressed response (using deflate or gzip)
--negotiate 使用HTTP身份验证
-K, --config FILE Specify which config file to read
--digest 使用数字身份验证
--connect-timeout SECONDS Maximum time allowed for connection
--disable-eprt 禁止使用EPRT或LPRT
-C, --continue-at OFFSET Resumed transfer offset
--disable-epsv 禁止使用EPSV
-b, --cookie STRING/FILE String or file to read cookies from (H)
-D/--dump-header 把header信息写入到该文件中
-c, --cookie-jar FILE Write cookies to this file after operation (H)
--egd-file 为随机数据(SSL)设置EGD socket路径
--create-dirs Create necessary local directory hierarchy
--tcp-nodelay 使用TCP_NODELAY选项
--crlf Convert LF to CRLF in upload
-e/--referer 来源网址
--crlfile FILE Get a CRL list in PEM format from the given file
-E/--cert 客户端证书文件和密码 (SSL)
-d, --data DATA HTTP POST data (H)
--cert-type 证书文件类型 (DER/PEM/ENG) (SSL)
--data-ascii DATA HTTP POST ASCII data (H)
--key 私钥文件名 (SSL)
--data-binary DATA HTTP POST binary data (H)
--key-type 私钥文件类型 (DER/PEM/ENG) (SSL)
--data-urlencode DATA HTTP POST data url encoded (H)
--pass 私钥密码 (SSL)
--delegation STRING GSS-API delegation permission
--engine 加密引擎使用 (SSL). "--engine list" for list
--digest Use HTTP Digest Authentication (H)
--cacert CA证书 (SSL)
--disable-eprt Inhibit using EPRT or LPRT (F)
--capath CA目录 (made using c_rehash) to verify peer against (SSL)
--disable-epsv Inhibit using EPSV (F)
--ciphers SSL密码
-D, --dump-header FILE Write the headers to this file
--compressed 要求返回是压缩的形势 (using deflate or gzip)
--egd-file FILE EGD socket path for random data (SSL)
--connect-timeout 设置最大请求时间
--engine ENGINGE Crypto engine (SSL). "--engine list" for list
--create-dirs 建立本地目录的目录层次结构
-f, --fail Fail silently (no output at all) on HTTP errors (H)
--crlf 上传是把LF转变成CRLF
-F, --form CONTENT Specify HTTP multipart POST data (H)
-f/--fail 连接失败时不显示http错误
--form-string STRING Specify HTTP multipart POST data (H)
--ftp-create-dirs 如果远程目录不存在,创建远程目录
--ftp-account DATA Account data string (F)
--ftp-method [multicwd/nocwd/singlecwd] 控制CWD的使用
--ftp-alternative-to-user COMMAND String to replace "USER [name]" (F)
--ftp-pasv 使用 PASV/EPSV 代替端口
--ftp-create-dirs Create the remote dirs if not present (F)
--ftp-skip-pasv-ip 使用PASV的时候,忽略该IP地址
--ftp-method [MULTICWD/NOCWD/SINGLECWD] Control CWD usage (F)
--ftp-ssl 尝试用 SSL/TLS 来进行ftp数据传输
--ftp-pasv Use PASV/EPSV instead of PORT (F)
--ftp-ssl-reqd 要求用 SSL/TLS 来进行ftp数据传输
-P, --ftp-port ADR Use PORT with given address instead of PASV (F)
-F/--form 模拟http表单提交数据
--ftp-skip-pasv-ip Skip the IP address for PASV (F)
--form-string 模拟http表单提交数据
--ftp-pret Send PRET before PASV (for drftpd) (F)
-g/--globoff 禁用网址序列和范围使用{}和[]
--ftp-ssl-ccc Send CCC after authenticating (F)
-G/--get 以get的方式来发送数据
--ftp-ssl-ccc-mode ACTIVE/PASSIVE Set CCC mode (F)
-H/--header 自定义头信息传递给服务器
--ftp-ssl-control Require SSL/TLS for ftp login, clear for transfer (F)
--ignore-content-length 忽略的HTTP头信息的长度
-G, --get Send the -d data with a HTTP GET (H)
-i/--include 输出时包括protocol头信息
-g, --globoff Disable URL sequences and ranges using {} and []
-I/--head 只显示请求头信息
-H, --header LINE Custom header to pass to server (H)
-j/--junk-session-cookies 读取文件进忽略session cookie
-I, --head Show document info only
--interface 使用指定网络接口/地址
-h, --help This help text
--krb4 使用指定安全级别的krb4
--hostpubmd5 MD5 Hex encoded MD5 string of the host public key. (SSH)
-k/--insecure 允许不使用证书到SSL站点
-0, --http1.0 Use HTTP 1.0 (H)
-K/--config 指定的配置文件读取
--ignore-content-length Ignore the HTTP Content-Length header
-l/--list-only 列出ftp目录下的文件名称
-i, --include Include protocol headers in the output (H/F)
--limit-rate 设置传输速度
-k, --insecure Allow connections to SSL sites without certs (H)
--local-port 强制使用本地端口号
--interface INTERFACE Specify network interface/address to use
-m/--max-time 设置最大传输时间
-4, --ipv4 Resolve name to IPv4 address
--max-redirs 设置最大读取的目录数
-6, --ipv6 Resolve name to IPv6 address
--max-filesize 设置最大下载的文件总量
-j, --junk-session-cookies Ignore session cookies read from file (H)
-M/--manual 显示全手动
--keepalive-time SECONDS Interval between keepalive probes
-n/--netrc 从netrc文件中读取用户名和密码
--key KEY Private key file name (SSL/SSH)
--netrc-optional 使用 .netrc 或者 URL来覆盖-n
--key-type TYPE Private key file type (DER/PEM/ENG) (SSL)
--ntlm 使用 HTTP NTLM 身份验证
--krb LEVEL Enable Kerberos with specified security level (F)
-N/--no-buffer 禁用缓冲输出
--libcurl FILE Dump libcurl equivalent code of this command line
-o/--output 把输出写到该文件中
--limit-rate RATE Limit transfer speed to this rate
-O/--remote-name 把输出写到该文件中,保留远程文件的文件名
-l, --list-only List only names of an FTP directory (F)
-p/--proxytunnel 使用HTTP代理
--local-port RANGE Force use of these local port numbers
--proxy-anyauth 选择任一代理身份验证方法
-L, --location Follow redirects (H)
--proxy-basic 在代理上使用基本身份验证
--location-trusted like --location and send auth to other hosts (H)
--proxy-digest 在代理上使用数字身份验证
-M, --manual Display the full manual
--proxy-ntlm 在代理上使用ntlm身份验证
--mail-from FROM Mail from this address
-P/--ftp-port 使用端口地址, 而不是使用PASV
--mail-rcpt TO Mail to this receiver(s)
-q 作为第一个参数,关闭 .curlrc
--mail-auth AUTH Originator address of the original email
-Q/--quote 文件传输前,发送命令到服务器
--max-filesize BYTES Maximum file size to download (H/F)
-r/--range 检索来自HTTP/1.1或FTP服务器字节范围
--max-redirs NUM Maximum number of redirects allowed (H)
--range-file 读取( SSL) 的随机文件
-m, --max-time SECONDS Maximum time allowed for the transfer
-R/--remote-time 在本地生成文件时,保留远程文件时间
--metalink Process given URLs as metalink XML file
--retry 传输出现问题时,重试的次数
--negotiate Use HTTP Negotiate Authentication (H)
--retry-delay 传输出现问题时,设置重试间隔时间
-n, --netrc Must read .netrc for user name and password
--retry-max-time 传输出现问题时,设置最大重试时间
--netrc-optional Use either .netrc or URL; overrides -n
-s/--silent 静默模式。不输出任何东西
--netrc-file FILE Set up the netrc filename to use
-S/--show-error 显示错误
-N, --no-buffer Disable buffering of the output stream
--socks4 用socks4代理给定主机和端口
--no-keepalive Disable keepalive use on the connection
--socks5 用socks5代理给定主机和端口
--no-sessionid Disable SSL session-ID reusing (SSL)
--stderr
--noproxy List of hosts which do not use proxy
-t/--telnet-option Telnet选项设置
--ntlm Use HTTP NTLM authentication (H)
--trace 对指定文件进行debug
-o, --output FILE Write output to < file > instead of stdout
--trace-ascii Like --跟踪但没有hex输出
--pass PASS Pass phrase for the private key (SSL/SSH)
--trace-time 跟踪/详细输出时,添加时间戳
--post301 Do not switch to GET after following a 301 redirect (H)
-T/--upload-file 上传文件
--post302 Do not switch to GET after following a 302 redirect (H)
--url Spet URL to work with
--post303 Do not switch to GET after following a 303 redirect (H)
-u/--user 设置服务器的用户和密码
-#, --progress-bar Display transfer progress as a progress bar
-U/--proxy-user 设置代理用户名和密码
--proto PROTOCOLS Enable/disable specified protocols
-w/--write-out [format] 什么输出完成后
--proto-redir PROTOCOLS Enable/disable specified protocols on redirect
-x/--proxy 在给定的端口上使用HTTP代理
-x, --proxy [PROTOCOL://]HOST[:PORT] Use proxy on given port
-X/--request 指定什么命令
--proxy-anyauth Pick "any" proxy authentication method (H)
-y/--speed-time 放弃限速所要的时间, 默认为30
--proxy-basic Use Basic authentication on the proxy (H)
-Y/--speed-limit 停止传输速度的限制,速度时间
--proxy-digest Use Digest authentication on the proxy (H)
--proxy-negotiate Use Negotiate authentication on the proxy (H)
--proxy-ntlm Use NTLM authentication on the proxy (H)
-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 a HTTP proxy tunnel (using CONNECT)
--pubkey KEY Public key file name (SSH)
-Q, --quote CMD Send command(s) to server before transfer (F/SFTP)
--random-file FILE File for reading random data from (SSL)
-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, --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
--resolve HOST:PORT:ADDRESS Force resolve of HOST:PORT to ADDRESS
--retry NUM Retry request NUM times if transient problems occur
--retry-delay SECONDS When retrying, wait this many seconds between each
--retry-max-time SECONDS Retry only within this period
-S, --show-error Show error. With -s, make curl show errors when they occur
-s, --silent Silent mode. Don't output anything
--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
--ssl Try SSL/TLS (FTP, IMAP, POP3, SMTP)
--ssl-reqd Require SSL/TLS (FTP, IMAP, POP3, SMTP)
-2, --sslv2 Use SSLv2 (SSL)
-3, --sslv3 Use SSLv3 (SSL)
--ssl-allow-beast Allow security flaw to improve interop (SSL)
--stderr FILE Where to redirect stderr. - 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
-1, --tlsv1 Use => TLSv1 (SSL)
--tlsv1.0 Use TLSv1.0 (SSL)
--tlsv1.1 Use TLSv1.1 (SSL)
--tlsv1.2 Use TLSv1.2 (SSL)
--trace FILE Write a debug trace to the given 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 Transfer FILE to destination
--url URL URL to work with
-B, --use-ascii Use ASCII/text transfer
-u, --user USER[:PASSWORD] Server user and password
--tlsuser USER TLS username
--tlspassword STRING TLS password
--tlsauthtype STRING TLS authentication type (default SRP)
--unix-socket FILE Connect through this UNIX domain socket
-A, --user-agent STRING User-Agent to send to server (H)
-v, --verbose Make the operation more talkative
-V, --version Show version number and quit
-w, --write-out FORMAT What to output after completion
--xattr Store metadata in extended file attributes
-q If used as the first parameter disables .curlrc
```
```
## 实例
## 实例
```bash
```bash
** 文件下载**
curl -I http://man.linuxde.net # 通过-I或者--head可以只打印出HTTP头部信息
curl -H "Host:man.linuxde.net" -H "accept-language:zh-cn" URL # 使用-H "头部信息" 传递多个头部信息
curl命令可以用来执行下载、发送各种HTTP请求, 指定HTTP头部等操作。如果系统没有curl可以使用`yum install curl`安装, 也可以下载安装。curl是将下载文件输出到stdout, 将进度信息输出到stderr, 不显示进度信息使用`--silent`选项。
curl URL --silent # curl默认输出到stdout, 将进度信息输出到stderr, 不显示进度信息使用--silent选项
curl http://man.linuxde.net/text.iso --silent -O # 所有下载的数据都被写入到stdout,将下载的数据写入到文件,必须使用绝对路径
curl URL --silent
curl http://man.linuxde.net/test.iso -o filename.iso --progress # 选项-o将下载数据写入到指定文件中, --progress显示进度
```
curl URL/File -C 偏移量 # curl能从特定的文件偏移处继续下载, 它可以通过指定一个偏移量来下载部分文件,偏移量是以字节为单位的整数
curl http://man.linuxde.net --cookie "user=root;pass=123456" # 指定cookie, 多个cookie使用分号分隔
这条命令是将下载文件输出到终端, 所有下载的数据都被写入到stdout。
curl URL --cookie-jar cookie_file # 将cookie另存为一个文件, 使用--cookie-jar选项
curl URL --user-agent "Mozilla/5.0" # 使用user-agent访问或下载
使用选项`-O`将下载的数据写入到文件,必须使用文件的绝对地址:
curl URL --limit-rate 50k # --limit-rate限制curl的下载速度: k,m,g或K,M,G
curl URL --max-filesize bytes # 使用--max-filesize指定可下载的最大文件大小
```
curl -u user:pwd http://man.linuxde.net # 使用curl选项-u可以完成HTTP或者FTP的认证, 也可以不指定密码在后续操作中输入密码
curl http://man.linuxde.net/text.iso --silent -O
```
: < < comment
参照页是位于HTTP头部中的一个字符串, 用来表示用户是从哪个页面到达当前页面的, 如果用户点击网页A中的某个连接, 那么用户就会跳转到B网页
选项`-o`将下载数据写入到指定名称的文件中,并使用`--progress`显示进度条:
网页B头部的参照页字符串就包含网页A的URL,使用--referer选项指定参照页字符串:
comment
```
curl http://man.linuxde.net/test.iso -o filename.iso --progress
######################################### 100.0%
```
** 断点续传**
curl能够从特定的文件偏移处继续下载, 它可以通过指定一个便宜量来下载部分文件:
```
curl URL/File -C 偏移量
#偏移量是以字节为单位的整数, 如果让curl自动推断出正确的续传位置使用-C -:
curl -C -URL
```
** 使用curl设置参照页字符串**
参照页是位于HTTP头部中的一个字符串, 用来表示用户是从哪个页面到达当前页面的, 如果用户点击网页A中的某个连接, 那么用户就会跳转到B网页, 网页B头部的参照页字符串就包含网页A的URL。
使用`--referer`选项指定参照页字符串:
```
curl --referer http://www.google.com http://man.linuxde.net
curl --referer http://www.google.com http://man.linuxde.net
```
** 用curl设置cookies**
使用`--cookie "COKKIES"`选项来指定cookie, 多个cookie使用分号分隔:
```
curl http://man.linuxde.net --cookie "user=root;pass=123456"
```
将cookie另存为一个文件, 使用`--cookie-jar`选项:
```
curl URL --cookie-jar cookie_file
```
** 用curl设置用户代理字符串**
有些网站访问会提示只能使用IE浏览器来访问, 这是因为这些网站设置了检查用户代理, 可以使用curl把用户代理设置为IE, 这样就可以访问了。使用`--user-agent`或者`-A`选项:
```
curl URL --user-agent "Mozilla/5.0"
curl URL -A "Mozilla/5.0"
```
其他HTTP头部信息也可以使用curl来发送, 使用`-H`"头部信息" 传递多个头部信息,例如:
```
curl -H "Host:man.linuxde.net" -H "accept-language:zh-cn" URL
```
**curl的带宽控制和下载配额**
使用`--limit-rate`限制curl的下载速度:
```
curl URL --limit-rate 50k
```
命令中用k( 千字节) 和m( 兆字节) 指定下载速度限制。
使用`--max-filesize`指定可下载的最大文件大小:
```
curl URL --max-filesize bytes
```
如果文件大小超出限制, 命令则返回一个非0退出码, 如果命令正常则返回0。
** 用curl进行认证**
使用curl选项 -u 可以完成HTTP或者FTP的认证, 可以指定密码, 也可以不指定密码在后续操作中输入密码:
```
```
curl -u user:pwd http://man.linuxde.net
curl -u user http://man.linuxde.net
```
** 只打印响应头部信息**
通过`-I`或者`-head`可以只打印出HTTP头部信息:
```
[root@localhost text]# curl -I http://man.linuxde.net
HTTP/1.1 200 OK
Server: nginx/1.2.5
date: Mon, 10 Dec 2012 09:24:34 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Pingback: http://man.linuxde.net/xmlrpc.php
```