欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Linux curl命令详解

Linux curl命令详解

  curl是一个非常实用的、用来与服务器之间传输数据的工具;支持的协议包括 (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP),curl设计为无用户交互下完成工作;

  curl提供了一大堆非常有用的功能,包括代理访问、用户认证、ftp上传下载、HTTP POST、SSL连接、cookie支持、断点续传...。
 
一、curl命令语法:
curl [options] [URL...]
 
二、curl命令参数详解:
  由于linux curl功能十分强大,所以命令参数十分多,下表只是爱E族(aiezu.com)帅选出来的部分参数,更多参数请运行“man curl”命令查看。
参数组参数描述
urlurl需要抓取的一到多个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命令退出码:
下面是linux curl命令的错误代码和她们的相应的错误消息,可能会出现在恶劣的环境。
退出码错误描述
1Unsupported protocol. This build of curl has no support for this protocol.
2Failed to initialize.
3URL malformed. The syntax was not correct.
5Couldn't resolve proxy. The given proxy host could not be resolved.
6Couldn't resolve host. The given remote host was not resolved.
7Failed to connect to host.
8FTP weird server reply. The server sent data curl couldn't parse.
9FTP access denied. The server denied login or denied access to the particular resource or directory you wanted to reach. Most often you tried to change to a directory that doesn't exist on the server.
11FTP weird PASS reply. Curl couldn't parse the reply sent to the PASS request.
13FTP weird PASV reply, Curl couldn't parse the reply sent to the PASV request.
14FTP weird 227 format. Curl couldn't parse the 227-line the server sent.
15FTP can't get host. Couldn't resolve the host IP we got in the 227-line.
17FTP couldn't set binary. Couldn't change transfer method to binary.
18Partial file. Only a part of the file was transferred.
19FTP couldn't download/access the given file, the RETR (or similar) command failed.
21FTP quote error. A quote command returned error from the server.
22HTTP page not retrieved. The requested url was not found or returned another error with the HTTP error code being 400 or above. This return code only appears if -f/--fail is used.
23Write error. Curl couldn't write data to a local filesystem or similar.
25FTP couldn't STOR file. The server denied the STOR operation, used for FTP uploading.
26Read error. Various reading problems.
27Out of memory. A memory allocation request failed.
28Operation timeout. The specified time-out period was reached according to the conditions.
30FTP PORT failed. The PORT command failed. Not all FTP servers support the PORT command, try doing a transfer using PASV instead!
31FTP couldn't use REST. The REST command failed. This command is used for resumed FTP transfers.
33HTTP range error. The range "command" didn't work.
34HTTP post error. Internal post-request generation error.
35SSL connect error. The SSL handshaking failed.
36FTP bad download resume. Couldn't continue an earlier aborted download.
37FILE couldn't read file. Failed to open the file. Permissions?
38LDAP cannot bind. LDAP bind operation failed.
39LDAP search failed.
41Function not found. A required LDAP function was not found.
42Aborted by callback. An application told curl to abort the operation.
43Internal error. A function was called with a bad parameter.
45Interface error. A specified outgoing interface could not be used.
47Too many redirects. When following redirects, curl hit the maximum amount.
48Unknown TELNET option specified.
49Malformed telnet option.
51The peer's SSL certificate or SSH MD5 fingerprint was not ok.
52The server didn't reply anything, which here is considered an error.
53SSL crypto engine not found.
54Cannot set SSL crypto engine as default.
55Failed sending network data.
56Failure in receiving network data.
58Problem with the local certificate.
59Couldn't use specified SSL cipher.
60Peer certificate cannot be authenticated with known CA certificates.
61Unrecognized transfer encoding.
62Invalid LDAP URL.
63Maximum file size exceeded.
64Requested FTP SSL level failed.
65Sending the data requires a rewind that failed.
66Failed to initialize SSL Engine.
67The user name, password, or similar was not accepted and curl failed to log in.
68File not found on TFTP server.
69Permission problem on TFTP server.
70Out of disk space on TFTP server.
71Illegal TFTP operation.
72Unknown TFTP transfer ID.
73File already exists (TFTP).
74No such user (TFTP).
75Character conversion failed.
76Character conversion functions required.
77Problem with reading the SSL CA cert (path? access rights?).
78The resource referenced in the URL does not exist.
79An unspecified error occurred during the SSH session.
80Failed to shut down the SSL connection.
82Could not load CRL file, missing or wrong format (added in 7.19.0).
83Issuer check failed (added in 7.19.0).
XXMore error codes will appear here in future releases. The existing ones are meant to never change.

四、用法演示:
1、下载页面:
1
curl -o index.html http://aiezu.com
 
2、下载文件并显示简单进度条:
1
curl -# -o centos6.8.iso http://mirrors.aliyun.com/centos/6.8/isos/x86_64/CentOS-6.8-x86_64-minimal.iso
 
3、断点续传:
1
2
#继续完成上次终止的未完成的下载
curl -# -o centos6.8.iso -C - http://mirrors.aliyun.com/centos/6.8/isos/x86_64/CentOS-6.8-x86_64-minimal.iso

4、伪造来源页面:
1
2
#告诉爱E族,我是从百度来的
curl -e http://baidu.com http://aiezu.com

 5、伪造代理设备:
1
2
3
4
5
#告诉爱E族,我是GOOGLE爬虫蜘蛛(其实我是curl命令)
curl -A " Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" http://aiezu.com
 
#告诉爱E族,我用的是微信内置浏览器
curl -A "Mozilla/5.0 AppleWebKit/600 Mobile MicroMessenger/6.0" http://aiezu.com
 
6、http头:
1
2
# 看看本站的http头是怎么样的
curl -I  http://aiezu.com
输出:
1
2
3
4
5
6
7
8
9
HTTP/1.1 200 OK
Date: Fri, 25 Nov 2016 16:45:49 GMT
Server: Apache
Set-Cookie: rox__Session=abdrt8vesprhnpc3f63p1df7j4; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Vary: Accept-Encoding
Content-Type: text/html; charset=utf-8
 
6、设置http请求头:
1
curl -H "Cache-Control:no-cache"  http://aiezu.com

7、发送表单数据:
1
curl -F "pic=@logo.png" -F "site=aiezu"  http://aiezu.com/

8、发送cookie:
1
curl -b "domain=aiezu.com"  http://aiezu.com
 1
 分享 2016-11-23

Linux curl命令参数详解

 
复制代码
linux curl是通过url语法在命令行下上传或下载文件的工具软件,它支持http,https,ftp,ftps,telnet等多种协议,常被用来抓取网页和监控Web服务器状态。

一、Linux curl用法举例:

1. linux curl抓取网页:

抓取百度:
curl http://www.baidu.com
 
如发现乱码,可以使用iconv转码
curl http://iframe.ip138.com/ic.asp|iconv -fgb2312

iconv的用法请参阅:在Linux/Unix系统下用iconv命令处理文本文件中文乱码问题

2. Linux curl使用代理:

linux curl使用http代理抓取页面:
curl -x 111.95.243.36:80 http://iframe.ip138.com/ic.asp|iconv -fgb2312
curl -x 111.95.243.36:80 -U aiezu:password http://www.baidu.com
 
使用socks代理抓取页面:
curl --socks4 202.113.65.229:443 http://iframe.ip138.com/ic.asp|iconv -fgb2312
curl --socks5 202.113.65.229:443 http://iframe.ip138.com/ic.asp|iconv -fgb2312 

代理服务器地址可以从爬虫代理上获取。

3. linux curl处理cookies

接收cookies:
curl -c /tmp/cookies http://www.baidu.com #cookies保存到/tmp/cookies文件
 
发送cookies:
curl -b "key1=val1;key2=val2;" http://www.baidu.com #发送cookies文本
curl -b /tmp/cookies http://www.baidu.com #从文件中读取cookies

4. linux curl发送数据:

linux curl get方式提交数据:
curl -G -d "name=value&name2=value2" http://www.baidu.com
 
linux curl post方式提交数据:
curl -d "name=value&name2=value2" http://www.baidu.com #post数据
curl -d a=b&c=d&txt@/tmp/txt http://www.baidu.com  #post文件
 
以表单的方式上传文件:
curl -F file=@/tmp/me.txt http://www.aiezu.com
 
相当于设置form表单的method="POST"和enctype='multipart/form-data'两个属性。

5. linux curl http header处理:

设置http请求头信息:
curl -A "Mozilla/5.0 Firefox/21.0" http://www.baidu.com #设置http请求头User-Agent
curl -e "http://pachong.org/" http://www.baidu.com #设置http请求头Referer
curl -H "Connection:keep-alive \n User-Agent: Mozilla/5.0" http://www.aiezu.com

设置http响应头处理:
curl -I http://www.aiezu.com #仅仅返回header
curl -D /tmp/header http://www.aiezu.com #将http header保存到/tmp/header文件
 
6. linux curl认证:
curl -u aiezu:password http://www.aiezu.com #用户名密码认证
curl -E mycert.pem https://www.baidu.com #采用证书认证
 
6. 其他:

curl -# http://www.baidu.com #以“#”号输出进度条
curl -o /tmp/aiezu http://www.baidu.com #保存http响应到/tmp/aiezu

linux 使用curl小经验教训:
http请求地址的url要使用""括起来。当有存在多个参数使用&连接时可能会出错。

来自  http://www.cnblogs.com/intval/p/5764616.html


linux curl是通过url语法在命令行下上传或下载文件的工具软件,它支持http,https,ftp,ftps,telnet等多种协议,常被用来抓取网页和监控Web服务器状态。

  curl命令参数介绍请参考:http://aiezu.com/article/linux_curl_command.html

一、Linux curl用法举例:
1. linux curl抓取网页:

抓取百度:

 

 

如发现乱码,可以使用iconv转码:

 

iconv的用法请参阅:在Linux/Unix系统下用iconv命令处理文本文件中文乱码问题

 

2. Linux curl使用代理

linux curl使用http代理抓取页面:

 

使用socks代理抓取页面:

 

代理服务器地址可以从爬虫代理上获取。

 

3. linux curl处理cookies

接收cookies:

 

发送cookies:

 

 

4. linux curl发送数据:

linux curl get方式提交数据:

 

linux curl post方式提交数据:

 

以表单的方式上传文件:

 

相当于设置form表单的method="POST"和enctype='multipart/form-data'两个属性。

 

5. linux curl http header处理:

设置http请求头信息:

设置http响应头处理:

 

 

6. linux curl认证:

 

 

 

6. 其他:

 

 

 
分类:Linux标签:
来自  http://www.aiezu.com/system/linux/linux_curl_syntax.html


 对于windows用户如果用Cygwin模拟unix环境的话,里面没有带curl命令,要自己装,所以建议用Gow来模拟,它已经自带了curl工具,安装后直接在cmd环境中用curl命令就可,因为路径已经自动给你配置好了。

    Linux curl是一个利用URL规则在命令行下工作的文件传输工具。它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称url为下载工具。

  一,curl命令参数,有好多我没有用过,也不知道翻译的对不对,如果有误的地方,还请指正。

  -a/--append 上传文件时,附加到目标文件

  -A/--user-agent <string>  设置用户代理发送给服务器

  - anyauth   可以使用“任何”身份验证方法

  -b/--cookie <name=string/file> cookie字符串或文件读取位置

  - basic 使用HTTP基本验证

  -B/--use-ascii 使用ASCII /文本传输

  -c/--cookie-jar <file> 操作结束后把cookie写入到这个文件中

  -C/--continue-at <offset>  断点续转

  -d/--data <data>   HTTP POST方式传送数据

  --data-ascii <data>  以ascii的方式post数据

  --data-binary <data> 以二进制的方式post数据

  --negotiate     使用HTTP身份验证

  --digest        使用数字身份验证

  --disable-eprt  禁止使用EPRT或LPRT

  --disable-epsv  禁止使用EPSV

  -D/--dump-header <file> 把header信息写入到该文件中

  --egd-file <file> 为随机数据(SSL)设置EGD socket路径

  --tcp-nodelay   使用TCP_NODELAY选项

  -e/--referer 来源网址

  -E/--cert <cert[:passwd]> 客户端证书文件和密码 (SSL)

  --cert-type <type> 证书文件类型 (DER/PEM/ENG) (SSL)

  --key <key>     私钥文件名 (SSL)

  --key-type <type> 私钥文件类型 (DER/PEM/ENG) (SSL)

  --pass  <pass>  私钥密码 (SSL)

  --engine <eng>  加密引擎使用 (SSL). "--engine list" for list

  --cacert <file> CA证书 (SSL)

  --capath <directory> CA目录 (made using c_rehash) to verify peer against (SSL)

  --ciphers <list>  SSL密码

  --compressed    要求返回是压缩的形势 (using deflate or gzip)

  --connect-timeout <seconds> 设置最大请求时间

  --create-dirs   建立本地目录的目录层次结构

  --crlf          上传是把LF转变成CRLF

  -f/--fail          连接失败时不显示http错误

  --ftp-create-dirs 如果远程目录不存在,创建远程目录

  --ftp-method [multicwd/nocwd/singlecwd] 控制CWD的使用

  --ftp-pasv      使用 PASV/EPSV 代替端口

  --ftp-skip-pasv-ip 使用PASV的时候,忽略该IP地址

  --ftp-ssl       尝试用 SSL/TLS 来进行ftp数据传输

  --ftp-ssl-reqd  要求用 SSL/TLS 来进行ftp数据传输

  -F/--form <name=content> 模拟http表单提交数据

  -form-string <name=string> 模拟http表单提交数据

  -g/--globoff 禁用网址序列和范围使用{}和[]

  -G/--get 以get的方式来发送数据

  -h/--help 帮助

  -H/--header <line>自定义头信息传递给服务器

  --ignore-content-length  忽略的HTTP头信息的长度

  -i/--include 输出时包括protocol头信息

  -I/--head  只显示文档信息

  从文件中读取-j/--junk-session-cookies忽略会话Cookie

  - 界面<interface>指定网络接口/地址使用

  - krb4 <级别>启用与指定的安全级别krb4

  -j/--junk-session-cookies 读取文件进忽略session cookie

  --interface <interface> 使用指定网络接口/地址

  --krb4 <level>  使用指定安全级别的krb4

  -k/--insecure 允许不使用证书到SSL站点

  -K/--config  指定的配置文件读取

  -l/--list-only 列出ftp目录下的文件名称

  --limit-rate <rate> 设置传输速度

  --local-port<NUM> 强制使用本地端口号

  -m/--max-time <seconds> 设置最大传输时间

  --max-redirs <num> 设置最大读取的目录数

  --max-filesize <bytes> 设置最大下载的文件总量

  -M/--manual  显示全手动

  -n/--netrc 从netrc文件中读取用户名和密码

  --netrc-optional 使用 .netrc 或者 URL来覆盖-n

  --ntlm          使用 HTTP NTLM 身份验证

  -N/--no-buffer 禁用缓冲输出

  -o/--output 把输出写到该文件中

  -O/--remote-name 把输出写到该文件中,保留远程文件的文件名

  -p/--proxytunnel   使用HTTP代理

  --proxy-anyauth 选择任一代理身份验证方法

  --proxy-basic   在代理上使用基本身份验证

  --proxy-digest  在代理上使用数字身份验证

  --proxy-ntlm    在代理上使用ntlm身份验证

  -P/--ftp-port <address> 使用端口地址,而不是使用PASV

  -Q/--quote <cmd>文件传输前,发送命令到服务器

  -r/--range <range>检索来自HTTP/1.1或FTP服务器字节范围

  --range-file 读取(SSL)的随机文件

  -R/--remote-time   在本地生成文件时,保留远程文件时间

  --retry <num>   传输出现问题时,重试的次数

  --retry-delay <seconds>  传输出现问题时,设置重试间隔时间

  --retry-max-time <seconds> 传输出现问题时,设置最大重试时间

  -s/--silent静音模式。不输出任何东西

  -S/--show-error   显示错误

  --socks4 <host[:port]> 用socks4代理给定主机和端口

  --socks5 <host[:port]> 用socks5代理给定主机和端口

  --stderr <file>
-t/--telnet-option <OPT=val> Telnet选项设置

  --trace <file>  对指定文件进行debug

  --trace-ascii <file> Like --跟踪但没有hex输出

  --trace-time    跟踪/详细输出时,添加时间戳

  -T/--upload-file <file> 上传文件

  --url <URL>     Spet URL to work with

  -u/--user <user[:password]>设置服务器的用户和密码

  -U/--proxy-user <user[:password]>设置代理用户名和密码

  -v/--verbose

  -V/--version 显示版本信息

  -w/--write-out [format]什么输出完成后

  -x/--proxy <host[:port]>在给定的端口上使用HTTP代理

  -X/--request <command>指定什么命令

  -y/--speed-time 放弃限速所要的时间。默认为30

  -Y/--speed-limit 停止传输速度的限制,速度时间'秒

  -z/--time-cond  传送时间设置

  -0/--http1.0  使用HTTP 1.0

  -1/--tlsv1  使用TLSv1(SSL)

  -2/--sslv2 使用SSLv2的(SSL)

  -3/--sslv3         使用的SSLv3(SSL)

  --3p-quote      like -Q for the source URL for 3rd party transfer

  --3p-url        使用url,进行第三方传送

  --3p-user       使用用户名和密码,进行第三方传送

  -4/--ipv4   使用IP4

  -6/--ipv6   使用IP6

  -#/--progress-bar 用进度条显示当前的传送状态

  -a/--append 上传文件时,附加到目标文件

  -A/--user-agent <string>  设置用户代理发送给服务器

  - anyauth   可以使用“任何”身份验证方法

  -b/--cookie <name=string/file> cookie字符串或文件读取位置

  - basic 使用HTTP基本验证

  -B/--use-ascii 使用ASCII /文本传输

  -c/--cookie-jar <file> 操作结束后把cookie写入到这个文件中

  -C/--continue-at <offset>  断点续转

  -d/--data <data>   HTTP POST方式传送数据

  --data-ascii <data>  以ascii的方式post数据

  --data-binary <data> 以二进制的方式post数据

  --negotiate     使用HTTP身份验证

  --digest        使用数字身份验证

  --disable-eprt  禁止使用EPRT或LPRT

  --disable-epsv  禁止使用EPSV

  -D/--dump-header <file> 把header信息写入到该文件中

  --egd-file <file> 为随机数据(SSL)设置EGD socket路径

  --tcp-nodelay   使用TCP_NODELAY选项

  -e/--referer 来源网址

  -E/--cert <cert[:passwd]> 客户端证书文件和密码 (SSL)

  --cert-type <type> 证书文件类型 (DER/PEM/ENG) (SSL)

  --key <key>     私钥文件名 (SSL)

  --key-type <type> 私钥文件类型 (DER/PEM/ENG) (SSL)

  --pass  <pass>  私钥密码 (SSL)

  --engine <eng>  加密引擎使用 (SSL). "--engine list" for list

  --cacert <file> CA证书 (SSL)

  --capath <directory> CA目录 (made using c_rehash) to verify peer against (SSL)

  --ciphers <list>  SSL密码

  --compressed    要求返回是压缩的形势 (using deflate or gzip)

  --connect-timeout <seconds> 设置最大请求时间

  --create-dirs   建立本地目录的目录层次结构

  --crlf          上传是把LF转变成CRLF

  -f/--fail          连接失败时不显示http错误

  --ftp-create-dirs 如果远程目录不存在,创建远程目录

  --ftp-method [multicwd/nocwd/singlecwd] 控制CWD的使用

  --ftp-pasv      使用 PASV/EPSV 代替端口

  --ftp-skip-pasv-ip 使用PASV的时候,忽略该IP地址

  --ftp-ssl       尝试用 SSL/TLS 来进行ftp数据传输

  --ftp-ssl-reqd  要求用 SSL/TLS 来进行ftp数据传输

  -F/--form <name=content> 模拟http表单提交数据

  -form-string <name=string> 模拟http表单提交数据

  -g/--globoff 禁用网址序列和范围使用{}和[]

  -G/--get 以get的方式来发送数据

  -h/--help 帮助

  -H/--header <line>自定义头信息传递给服务器

  --ignore-content-length  忽略的HTTP头信息的长度

  -i/--include 输出时包括protocol头信息

  -I/--head  只显示文档信息

  从文件中读取-j/--junk-session-cookies忽略会话Cookie

  - 界面<interface>指定网络接口/地址使用

  - krb4 <级别>启用与指定的安全级别krb4

  -j/--junk-session-cookies 读取文件进忽略session cookie

  --interface <interface> 使用指定网络接口/地址

  --krb4 <level>  使用指定安全级别的krb4

  -k/--insecure 允许不使用证书到SSL站点

  -K/--config  指定的配置文件读取

  -l/--list-only 列出ftp目录下的文件名称

  --limit-rate <rate> 设置传输速度

  --local-port<NUM> 强制使用本地端口号

  -m/--max-time <seconds> 设置最大传输时间

  --max-redirs <num> 设置最大读取的目录数

  --max-filesize <bytes> 设置最大下载的文件总量
-M/--manual  显示全手动

  -n/--netrc 从netrc文件中读取用户名和密码

  --netrc-optional 使用 .netrc 或者 URL来覆盖-n

  --ntlm          使用 HTTP NTLM 身份验证

  -N/--no-buffer 禁用缓冲输出

  -o/--output 把输出写到该文件中

  -O/--remote-name 把输出写到该文件中,保留远程文件的文件名

  -p/--proxytunnel   使用HTTP代理

  --proxy-anyauth 选择任一代理身份验证方法

  --proxy-basic   在代理上使用基本身份验证

  --proxy-digest  在代理上使用数字身份验证

  --proxy-ntlm    在代理上使用ntlm身份验证

  -P/--ftp-port <address> 使用端口地址,而不是使用PASV

  -Q/--quote <cmd>文件传输前,发送命令到服务器

  -r/--range <range>检索来自HTTP/1.1或FTP服务器字节范围

  --range-file 读取(SSL)的随机文件

  -R/--remote-time   在本地生成文件时,保留远程文件时间

  --retry <num>   传输出现问题时,重试的次数

  --retry-delay <seconds>  传输出现问题时,设置重试间隔时间

  --retry-max-time <seconds> 传输出现问题时,设置最大重试时间

  -s/--silent静音模式。不输出任何东西

  -S/--show-error   显示错误

  --socks4 <host[:port]> 用socks4代理给定主机和端口

  --socks5 <host[:port]> 用socks5代理给定主机和端口

  --stderr <file>

  -t/--telnet-option <OPT=val> Telnet选项设置

  --trace <file>  对指定文件进行debug

  --trace-ascii <file> Like --跟踪但没有hex输出

  --trace-time    跟踪/详细输出时,添加时间戳

  -T/--upload-file <file> 上传文件

  --url <URL>     Spet URL to work with

  -u/--user <user[:password]>设置服务器的用户和密码

  -U/--proxy-user <user[:password]>设置代理用户名和密码

  -v/--verbose

  -V/--version 显示版本信息

  -w/--write-out [format]什么输出完成后

  -x/--proxy <host[:port]>在给定的端口上使用HTTP代理

  -X/--request <command>指定什么命令

  -y/--speed-time 放弃限速所要的时间。默认为30

  -Y/--speed-limit 停止传输速度的限制,速度时间'秒

  -z/--time-cond  传送时间设置

  -0/--http1.0  使用HTTP 1.0

  -1/--tlsv1  使用TLSv1(SSL)

  -2/--sslv2 使用SSLv2的(SSL)

  -3/--sslv3         使用的SSLv3(SSL)

  --3p-quote      like -Q for the source URL for 3rd party transfer

  --3p-url        使用url,进行第三方传送

  --3p-user       使用用户名和密码,进行第三方传送

  -4/--ipv4   使用IP4

  -6/--ipv6   使用IP6

  -#/--progress-bar 用进度条显示当前的传送状态

  二,常用curl实例

  1,抓取页面内容到一个文件中

  [root@krlcgcms01 mytest]# curl -o home.html  http://blog.51yip.com

  [root@krlcgcms01 mytest]# curl -o home.html  http://blog.51yip.com

  2,用-O(大写的),后面的url要具体到某个文件,不然抓不下来。我们还可以用正则来抓取东西

  [root@krlcgcms01 mytest]# curl -O

  [root@krlcgcms01 mytest]# curl -O

  3,模拟表单信息,模拟登录,保存cookie信息

  [root@krlcgcms01 mytest]# curl -c ./cookie_c.txt -F log=aaaa -F pwd=****** http://blog.51yip.com/wp-login.PHP

  [root@krlcgcms01 mytest]# curl -c ./cookie_c.txt -F log=aaaa -F pwd=****** http://blog.51yip.com/wp-login.php

  4,模拟表单信息,模拟登录,保存头信息

  [root@krlcgcms01 mytest]# curl -D ./cookie_D.txt -F log=aaaa -F pwd=****** http://blog.51yip.com/wp-login.php

  [root@krlcgcms01 mytest]# curl -D ./cookie_D.txt -F log=aaaa -F pwd=****** http://blog.51yip.com/wp-login.php

  -c(小写)产生的cookie和-D里面的cookie是不一样的。

  5,使用cookie文件

  [root@krlcgcms01 mytest]# curl -b ./cookie_c.txt  http://blog.51yip.com/wp-admin

  [root@krlcgcms01 mytest]# curl -b ./cookie_c.txt  http://blog.51yip.com/wp-admin

  6,断点续传,-C(大写的)

  [root@krlcgcms01 mytest]# curl -C -O

  7,传送数据,最好用登录页面测试,因为你传值过去后,curl回抓数据,你可以看到你传值有没有成功

  [root@krlcgcms01 mytest]# curl -d log=aaaa  http://blog.51yip.com/wp-login.php

  [root@krlcgcms01 mytest]# curl -d log=aaaa  http://blog.51yip.com/wp-login.php

  8,显示抓取错误,下面这个例子,很清楚的表明了。

  [root@krlcgcms01 mytest]# curl -f http://blog.51yip.com/asdf

  curl: (22) The requested URL returned error: 404

  [root@krlcgcms01 mytest]# curl http://blog.51yip.com/asdf

  <HTML><HEAD><TITLE>404,not found</TITLE>

  。。。。。。。。。。。。

  [root@krlcgcms01 mytest]# curl -f http://blog.51yip.com/asdf

  curl: (22) The requested URL returned error: 404

  [root@krlcgcms01 mytest]# curl http://blog.51yip.com/asdf

  <HTML><HEAD><TITLE>404,not found</TITLE>

  。。。。。。。。。。。。

  9,伪造来源地址,有的网站会判断,请求来源地址。

  [root@krlcgcms01 mytest]# curl -e http://localhost http://blog.51yip.com/wp-login.php

  [root@krlcgcms01 mytest]# curl -e http://localhost http://blog.51yip.com/wp-login.php

  10,当我们经常用curl去搞人家东西的时候,人家会把你的IP给屏蔽掉的,这个时候,我们可以用代理

  [root@krlcgcms01 mytest]# curl -x 24.10.28.84:32779 -o home.html http://blog.51yip.com

  [root@krlcgcms01 mytest]# curl -x 24.10.28.84:32779 -o home.html http://blog.51yip.com
11,比较大的东西,我们可以分段下载

  [root@krlcgcms01 mytest]# curl -r 0-100 -o img.part1 http://blog.51yip.com/wp-

  content/uploads/2010/09/compare_varnish.jpg

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

  Dload  Upload   Total   Spent    Left  Speed

  100   101  100   101    0     0    105      0 --:--:-- --:--:-- --:--:--     0

  [root@krlcgcms01 mytest]# curl -r 100-200 -o img.part2 http://blog.51yip.com/wp-

  content/uploads/2010/09/compare_varnish.jpg

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

  Dload  Upload   Total   Spent    Left  Speed

  100   101  100   101    0     0     57      0  0:00:01  0:00:01 --:--:--     0

  [root@krlcgcms01 mytest]# curl -r 200- -o img.part3 http://blog.51yip.com/wp-

  content/uploads/2010/09/compare_varnish.jpg

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

  Dload  Upload   Total   Spent    Left  Speed

  100  104k  100  104k    0     0  52793      0  0:00:02  0:00:02 --:--:-- 88961

  [root@krlcgcms01 mytest]# ls |grep part | xargs du -sh

  4.0K    one.part1

  112K    three.part3

  4.0K    two.part2

  [root@krlcgcms01 mytest]# curl -r 0-100 -o img.part1 http://blog.51yip.com/wp-

  content/uploads/2010/09/compare_varnish.jpg

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

  Dload  Upload   Total   Spent    Left  Speed

  100   101  100   101    0     0    105      0 --:--:-- --:--:-- --:--:--     0

  [root@krlcgcms01 mytest]# curl -r 100-200 -o img.part2 http://blog.51yip.com/wp-

  content/uploads/2010/09/compare_varnish.jpg

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

  Dload  Upload   Total   Spent    Left  Speed

  100   101  100   101    0     0     57      0  0:00:01  0:00:01 --:--:--     0

  [root@krlcgcms01 mytest]# curl -r 200- -o img.part3 http://blog.51yip.com/wp-

  content/uploads/2010/09/compare_varnish.jpg

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

  Dload  Upload   Total   Spent    Left  Speed

  100  104k  100  104k    0     0  52793      0  0:00:02  0:00:02 --:--:-- 88961

  [root@krlcgcms01 mytest]# ls |grep part | xargs du -sh

  4.0K    one.part1

  112K    three.part3

  4.0K    two.part2

  用的时候,把他们cat一下就OK了,cat img.part* >img.jpg

  12,不会显示下载进度信息

  [root@krlcgcms01 mytest]# curl -s -o aaa.jpg 

  13,显示下载进度条

  [root@krlcgcms01 mytest]# curl -# -O 

  ######################################################################## 100.0%

  14,通过ftp下载文件

  [zhangy@BlackGhost ~]$ curl -u 用户名:密码 -O http://blog.51yip.com/demo/curtain/bbstudy_files/style.css

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

  Dload  Upload   Total   Spent    Left  Speed

  101  1934  101  1934    0     0   3184      0 --:--:-- --:--:-- --:--:--  7136

  [zhangy@BlackGhost ~]$ curl -u 用户名:密码 -O http://blog.51yip.com/demo/curtain/bbstudy_files/style.css

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

  Dload  Upload   Total   Spent    Left  Speed

  101  1934  101  1934    0     0   3184      0 --:--:-- --:--:-- --:--:--  7136

  或者用下面的方式

  [zhangy@BlackGhost ~]$ curl -O ftp://用户名:密码@ip:port/demo/curtain/bbstudy_files/style.css

  [zhangy@BlackGhost ~]$ curl -O ftp://用户名:密码@ip:port/demo/curtain/bbstudy_files/style.css

  15,通过ftp上传

  [zhangy@BlackGhost ~]$ curl -T test.sql ftp://用户名:密码@ip:port/demo/curtain/bbstudy_files/

  [zhangy@BlackGhost ~]$ curl -T test.sql ftp://用户名:密码@ip:port/demo/curtain/bbstudy_files/
原文出处:http://linux.chinaitlab.com/command/830656.html

 

http://www.5678520.com/kaiwangdian/130.html

http://www.5678520.com/kaiwangdian/129.html

http://www.5678520.com/kaiwangdian/128.html

http://www.5678520.com/kaiwangdian/127.html

http://www.5678520.com/kaiwangdian/126.html

http://www.lianzhiwei.com/News/389/20122116.html

http://www.lianzhiwei.com/News/389/20122115.html

http://www.lianzhiwei.com/News/389/20122114.html

http://www.lianzhiwei.com/News/389/20122113.html

http://www.lianzhiwei.com/News/389/20122112.html

普通分类: