Linux curl命令详解
关于
【转自:http://aiezu.com/article/linux_curl_command.html】
语法
curl [options] [URL...]
curl命令参数详解
| 参数组 | 参数 | 描述 |
|---|---|---|
| url | url | 需要抓取的一到多个URLs;
支持多个通配符方式:
|
| 请求头 | -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”可以判断客户端使用的浏览器名称和操作系统类型,伪造此参数能导致服务器做出错误判断。
| |
| -e <URL>
--referer <URL> |
(HTTP)设置访问时的来源页面,告诉http服务从哪个页面进入到此页面;
| |
| 响应头 | -I
--head |
(HTTP)只输出HTTP-header,不获取内容(HTTP/FTP/FILE)。
|
| -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";
|
| -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端口。
|
| -p
--proxytunnel |
将“-x”参数的代理,作为通道的方式去代理非HTTP协议,如ftp; | |
| --socks4 <host[:port]>
--socks4a <host[:port]> |
使用 SOCKS4 代理;
使用 SOCKS4A 代理;
| |
| --proxy-anyauth
--proxy-basic |
http代理认证方式,参考:
| |
| -U <user:password>
--proxy-user <user:password> |
设置代理的用户名和密码; | |
| 数据传输 | -G
--get |
如果使用了此参数,“-d/”、“--data”、“--data-binary”参数设置的数据,将附加在 url 上,以 GET 的方式请求; |
| -d @file
-d "string" |
(HTTP)使用HTTP POST方式发送“key/value对”数据,相当于浏览器表单属性(method="POST",enctype="application/x-www-form-urlencoded")
| |
| -F name=@file
-F name=<file |
(HTTP)使用HTTP POST方式发送类似“表单字段”的多类型数据,相当于同时设置浏览器表单属性(method="POST",enctype="multipart/form-data"),可以使用此参数上传二进制文件。
| |
| --form-string <key=value> | (HTTP)类似于“--form”,但是“@”、“<”无特殊含义; | |
| -T file
--upload-file file |
通过“put”的方式将文件传输到远程网址;
| |
| 断点续传 | -C <offset>
--continue-at <offset> |
断点续转,从文件头的指定位置开始继续下载/上传;
|
| -r <range>
--range <range> |
(HTTP/FTP/SFTP/FILE) 只传输内容的指定部分:
| |
| 认证 | --basic | (HTTP)告诉curl使用HTTP Basic authentication(HTTP协议时)。
|
| --ntlm | (HTTP)使用NTLM身份验证方式,用于HTTP协议;
| |
| --digest | (HTTP)使用HTTP Digest authentication加密,用于HTTP协议;
| |
| --negotiate | (HTTP)使用GSS-Negotiate authentication方式,用于HTTP协议;
| |
| --anyauth | (HTTP)告诉curl自动选择合适的身份认证方法,并选用最安全的方式; | |
| -u user:password
--user user:password |
使用用户名、密码认证,此参数会覆盖“-n”、“--netrc”和“--netrc-optional”选项;
| |
| 证书 | -E <证书[:密码]>
--cert <证书[:密码]> |
(SSL)指定“PEM”格式的证书文件和证书密码; |
| --cert-type <type> | (SSL)告诉curl所提供证书的类型:PEM、DER、ENG等;
| |
| --cacert <CA证书> | (SSL)告诉curl所以指定的CA证书文件,必须是“PEM”格式; | |
| --capath <CA证书路径> | (SSL)告诉curl所以指定目录下的CA证书用来验证;
| |
| --crlfile <file> | (HTTPS/FTPS)提供一个PEM格式的文件,用于指定被吊销的证书列表; | |
| -k
--insecure |
(SSL)设置此选项将允许使用无证书的不安全SSL进行连接和传输。 | |
| SSL其他 | --ciphers <list of ciphers> | (SSL)指定SSL要使用的加密方式;如:“aes_256_sha_256”; |
| --engine <name> | 设置一个OpenSSL加密引擎用于加密操作;
| |
| --random-file | (SSL)指定包含随机数据的文件路径名;数据是用来为SSL连接产生随机种子为; | |
| --egd-file <file> | (SSL)为随机种子生成器EGD(Entropy Gathering Daemon socket)指定的路径名; | |
| -1/--tlsv1
--tlsv1.0 |
(SSL)使用 TLS版本2 与远程服务器通讯; (SSL)使用 TLS 1.0 版本与远程服务器通讯; | |
| 私钥公钥 | --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主动模式时,设置一个地址等待服务器的连接,如:
网卡: |
| --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/秒”。
|
| -y
--speed-time <time> |
如果在一个快速时间内,下载速度低于每秒速度限制字节数,则下载将中止。
| |
| -Y
--speed-limit <speed> |
如果下载速度低于此给定速度(以字节/秒为单位)达数秒,则会中止下载。
| |
| 其他选项 | -0/--http1.0 | (HTTP) 强制curl使用HTTP 1.0而不是使用默认的HTTP 1.1; |
| --interface <name> | 使用指定的网卡接口访问;
| |
| -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> |
失败重试次数;
重试间隔时间; | |
| -S
--show-error |
安静模式下显示错误信息; | |
| --stderr <file> | 错误信息保存文件; | |
| 输出 | -o file
--output file |
将返回内容输出到文件。
|
| -O
--remote-name |
将返回内容输出到当前目录下,和url中文件名相同的文件中(不含目录); | |
| --create-dirs | 与“-o”参数配合使用,创建必要的本地目录层次结构 | |
| -w
--write-out format |
操作完成后在返回信息尾部追加指定的内容;
| |
| 调试 | -trace <file> | 转储所有传入和传出的数据到文件,包括描述信息;
|
| --trace-ascii file | 转储所有传入和传出的数据到文件,包括描述信息,只转储ASCII部分,更容易阅读;
| |
| --trace-time | 转储文件中添加时间信息; | |
| -K
--config <config file> |
从配置文件中读取参数。 | |
| -v
--verbose |
显示更详细的信息,调试时使用; | |
| 帮助 | -M
--manual |
显示完整的帮助手册; |
| -h
--help |
linux curl用法帮助; |
Linux curl命令退出码
| 退出码 | 错误描述 |
|---|---|
| 1 | Unsupported protocol.
This build of curl has no support for this protocol. |
| 2 | Failed to initialize. |
| 3 | URL malformed.
The syntax was not correct. |
| 5 | Couldn't resolve proxy.
The given proxy host could not be resolved. |
| 6 | Couldn't resolve host.
The given remote host was not resolved. |
| 7 | Failed to connect to host. |
| 8 | FTP weird server reply.
The server sent data curl couldn't parse. |
| 9 | FTP access denied.
The server denied login or denied access to the particular resource or directory you wanted to reach. |
| 11 | FTP weird PASS reply. Curl couldn't parse the reply sent to the PASS request. |
| 13 | FTP weird PASV reply, Curl couldn't parse the reply sent to the PASV request. |
| 14 | FTP weird 227 format.
Curl couldn't parse the 227-line the server sent. |
| 15 | FTP can't get host.
Couldn't resolve the host IP we got in the 227-line. |
| 17 | FTP couldn't set binary.
Couldn't change transfer method to binary. |
| 18 | Partial file.
Only a part of the file was transferred. |
| 19 | FTP couldn't download/access the given file, the RETR (or similar) command failed. |
| 21 | FTP quote error.
A quote command returned error from the server. |
| 22 | HTTP page not retrieved.
The requested url was not found or returned another error with the HTTP error code being 400 or above. |
| 23 | Write error.
Curl couldn't write data to a local filesystem or similar. |
| 25 | FTP couldn't STOR file.
The server denied the STOR operation, used for FTP uploading. |
| 26 | Read error.
Various reading problems. |
| 27 | Out of memory.
A memory allocation request failed. |
| 28 | Operation timeout.
The specified time-out period was reached according to the conditions. |
| 30 | FTP PORT failed.
The PORT command failed. |
| 31 | FTP couldn't use REST.
The REST command failed. |
| 33 | HTTP range error.
The range "command" didn't work. |
| 34 | HTTP post error.
Internal post-request generation error. |
| 35 | SSL connect error.
The SSL handshaking failed. |
| 36 | FTP bad download resume.
Couldn't continue an earlier aborted download. |
| 37 | FILE couldn't read file.
Failed to open the file. Permissions? |
| 38 | LDAP cannot bind.
LDAP bind operation failed. |
| 39 | LDAP search failed. |
| 41 | Function not found.
A required LDAP function was not found. |
| 42 | Aborted by callback.
An application told curl to abort the operation. |
| 43 | Internal error.
A function was called with a bad parameter. |
| 45 | Interface error.
A specified outgoing interface could not be used. |
| 47 | Too many redirects.
When following redirects, curl hit the maximum amount. |
| 48 | Unknown TELNET option specified. |
| 49 | Malformed telnet option. |
| 51 | The peer's SSL certificate or SSH MD5 fingerprint was not ok. |
| 52 | The server didn't reply anything, which here is considered an error. |
| 53 | SSL crypto engine not found. |
| 54 | Cannot set SSL crypto engine as default. |
| 55 | Failed sending network data. |
| 56 | Failure in receiving network data. |
| 58 | Problem with the local certificate. |
| 59 | Couldn't use specified SSL cipher. |
| 60 | Peer certificate cannot be authenticated with known CA certificates. |
| 61 | Unrecognized transfer encoding. |
| 62 | Invalid LDAP URL. |
| 63 | Maximum file size exceeded. |
| 64 | Requested FTP SSL level failed. |
| 65 | Sending the data requires a rewind that failed. |
| 66 | Failed to initialize SSL Engine. |
| 67 | The user name, password, or similar was not accepted and curl failed to log in. |
| 68 | File not found on TFTP server. |
| 69 | Permission problem on TFTP server. |
| 70 | Out of disk space on TFTP server. |
| 71 | Illegal TFTP operation. |
| 72 | Unknown TFTP transfer ID. |
| 73 | File already exists (TFTP). |
| 74 | No such user (TFTP). |
| 75 | Character conversion failed. |
| 76 | Character conversion functions required. |
| 77 | Problem with reading the SSL CA cert (path? access rights?). |
| 78 | The resource referenced in the URL does not exist. |
| 79 | An unspecified error occurred during the SSH session. |
| 80 | Failed to shut down the SSL connection. |
| 82 | Could not load CRL file, missing or wrong format (added in 7.19.0). |
| 83 | Issuer check failed (added in 7.19.0). |
| XX | More error codes will appear here in future releases.
The existing ones are meant to never change. |