-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
330 lines (330 loc) · 21.5 KB
/
index.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>curl使用简单介绍(转)</title>
<meta name="author" content="陈子">
<!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- Le styles -->
<link href="/assets/themes/twitter/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="/assets/themes/twitter/css/style.css?body=1" rel="stylesheet" type="text/css" media="all">
<link href="/assets/themes/twitter/css/pygments/default.css" rel="stylesheet" type="text/css">
<!-- Le fav and touch icons -->
<!-- Update these with your own images
<link rel="shortcut icon" href="images/favicon.ico">
<link rel="apple-touch-icon" href="images/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="72x72" href="images/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="images/apple-touch-icon-114x114.png">
-->
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="/">三斗室</a>
<ul class="nav">
<li><a href="/archive.html">归档</a></li>
<li><a href="/categories.html">分类</a></li>
<li><a href="/errata.html">《网站运维技术与实践》勘误</a></li>
<li><a href="/pages.html">Pages</a></li>
<li><a href="/projects.html">学习记录</a></li>
<li><a href="/tags.html">标签</a></li>
<li><a href="http://chenlinux.com/poetry/index.html" />诗文集</a></li>
<li><link title="RSS 2.0" type="application/rss+xml" href="http://chenlinux.com/feed.xml" rel="alternate" /><a href="http://chenlinux.com/feed.xml" target="_blank">RSS订阅</a></li>
</ul>
<ul class="nav pull-right"><li><a href="/about.html">有关我</a></li></ul>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="span7">
<div class="row">
<div class="page-header">
<h1>curl使用简单介绍(转) <small></small></h1>
<div class="date"><span>03 November 2009</span></div>
<ul class="tag_box pull-right">
<li><a href="/tags.html#curl-ref">curl <span>1</span></a></li>
</ul>
</div>
<div style='background-color: #FFF;'>
<p>原文地址: <a href="http://www.linuxidc.com/Linux/2008-01/10891p2.htm">http://www.linuxidc.com/Linux/2008-01/10891p2.htm</a>
Curl是Linux下一个很强大的http命令行工具,其功能十分强大。</p>
<ol>
<li>
<p>二话不说,先从这里开始吧!
$ curl http://www.linuxidc.com
回车之后,www.linuxidc.com的html就稀里哗啦地显示在屏幕上了~</p>
</li>
<li>
<p>嗯,要想把读过来页面存下来,是不是要这样呢?
$ curl http://www.linuxidc.com > page.html
当然可以,但不用这么麻烦的!
用curl的内置option就好,存下http的结果,用这个option: -o
$ curl -o page.html http://www.linuxidc.com
这样,你就可以看到屏幕上出现一个下载页面进度指示。等进展到100%,自然就 OK咯</p>
</li>
<li>
<p>什么什么?!访问不到?肯定是你的proxy没有设定了。
使用curl的时候,用这个option可以指定http访问所使用的proxy服务器及其端口: -x
$ curl -x 123.45.67.89:1080 -o page.html http://www.linuxidc.com</p>
</li>
<li>
<p>访问有些网站的时候比较讨厌,他使用cookie来记录session信息。
像IE/NN这样的浏览器,当然可以轻易处理cookie信息,但我们的curl呢?…..
我们来学习这个option: -D
这个是把http的response里面的cookie信息存到一个特别的文件中去
$ curl -x 123.45.67.89:1080 -o page.html -D cookie0001.txt http://www.linuxidc.com
这样,当页面被存到page.html的同时,cookie信息也被存到了cookie0001.txt里面了</p>
</li>
</ol>
<p>5)那么,下一次访问的时候,如何继续使用上次留下的cookie信息呢?要知道,很多网站都是靠监视你的cookie信息,来判断你是不是不按规矩访问他们的网站的。
这次我们使用这个option来把上次的cookie信息追加到http request里面去: -b
$ curl -x 123.45.67.89:1080 -o page1.html -D cookie0002.txt -b cookie0001.txt http://www.linuxidc.com
这样,我们就可以几乎模拟所有的IE操作,去访问网页了!</p>
<p>6)稍微等等
~我好像忘记什么了
~
对了!是浏览器信息
有些讨厌的网站总要我们使用某些特定的浏览器去访问他们,有时候更过分的是,还要使用某些特定的版本
NND,哪里有时间为了它去找这些怪异的浏览器呢!?
好在curl给我们提供了一个有用的option,可以让我们随意指定自己这次访问所宣称的自己的浏览器信息: -A
$ curl -A “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)” -x 123.45.67.89:1080 -o page.html -D cookie0001.txt http://www.linuxidc.com
这样,服务器端接到访问的要求,会认为你是一个运行在Windows 2000上的IE6.0,嘿嘿嘿,其实也许你用的是苹果机呢!
而”Mozilla/4.73 [en] (X11; U; Linux 2.2; 15 i686”则可以告诉对方你是一台PC上跑着的Linux,用的是Netscape 4.73,呵呵呵</p>
<p>7) 另外一个服务器端常用的限制方法,就是检查http访问的referer。比如你先访问首页,再访问里面所指定的下载页,这第二次访问的referer地址就是第一次访问成功后的页面地址。这样,服务器端只要发现对下载页面某次访问的referer地址不是首页的地址,就可以断定那是个盗连了~讨厌讨厌~我就是要盗链~!!
幸好curl给我们提供了设定referer的option: -e
$ curl -A “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)” -x 123.45.67.89:1080 -e “mail.linuxidc.com” -o page.html -D cookie0001.txt http://www.linuxidc.com
这样,就可以骗对方的服务器,你是从mail.linuxidc.com点击某个链接过来的了,呵呵呵</p>
<p>8)写着写着发现漏掉什么重要的东西了!——- 利用curl 下载文件
刚才讲过了,下载页面到一个文件里,可以使用 -o ,下载文件也是一样。比如,
$ curl -o 1.jpg http://cgi2.tky.3web.ne.jp/~zzh/screen1.JPG
这里教大家一个新的option: -O 大写的O,这么用:
$ curl -O http://cgi2.tky.3web.ne.jp/~zzh/screen1.JPG
这样,就可以按照服务器上的文件名,自动存在本地了!
再来一个更好用的。
如果screen1.JPG以外还有screen2.JPG、screen3.JPG、….、screen10.JPG需要下载,难不成还要让我们写一个script来完成这些操作?
不干!
在curl里面,这么写就可以了:
$ curl -O http://cgi2.tky.3web.ne.jp/~zzh/screen[1-10].JPG
呵呵呵,厉害吧?! ~</p>
<p>9)再来,我们继续讲解下载!
$ curl -O <a href="http://cgi2.tky.3web.ne.jp/~{zzh,nick}/[001-201].JPG">http://cgi2.tky.3web.ne.jp/~{zzh,nick}/[001-201].JPG</a>
这样产生的下载,就是
~zzh/001.JPG
~zzh/002.JPG
…
~zzh/201.JPG
~nick/001.JPG
~nick/002.JPG
…
~nick/201.JPG
够方便的了吧?哈哈哈
咦?高兴得太早了。
由于zzh/nick下的文件名都是001,002…,201,下载下来的文件重名,后面的把前面的文件都给覆盖掉了 ~
没关系,我们还有更狠的!
$ curl -o #2_#1.jpg http://cgi2.tky.3web.ne.jp/~{zzh,nick}/[001-201].JPG
—这是…..自定义文件名的下载? 对头,呵呵!
这样,自定义出来下载下来的文件名,就变成了这样:原来: ~zzh/001.JPG —-> 下载后:001-zzh.JPG 原来: ~nick/001.JPG —-> 下载后:001-nick.JPG
这样一来,就不怕文件重名啦,呵呵</p>
<p>9)继续讲下载
我们平时在windows平台上,flashget这样的工具可以帮我们分块并行下载,还可以断线续传。curl在这些方面也不输给谁,嘿嘿比如我们下载screen1.JPG中,突然掉线了,我们就可以这样开始续传
$ curl -c -O http://cgi2.tky.3wb.ne.jp/~zzh/screen1.JPG
当然,你不要拿个flashget下载了一半的文件来糊弄我。别的下载软件的半截文件可不一定能用哦 ~</p>
<p>分块下载,我们使用这个option就可以了: -r
举例说明
比如我们有一个http://cgi2.tky.3web.ne.jp/~zzh/zhao1.MP3 要下载(赵老师的电话朗诵 :D)我们就可以用这样的命令:
$ curl -r 0-10240 -o “zhao.part1” http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.MP3 &
$ curl -r 10241-20480 -o “zhao.part1” http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.MP3 &
$ curl -r 20481-40960 -o “zhao.part1” http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.MP3 &
$ curl -r 40961- -o “zhao.part1” http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.MP3
这样就可以分块下载啦。不过你需要自己把这些破碎的文件合并起来如果你用UNIX或苹果,用 cat zhao.part* > zhao.MP3就可以如果用的是Windows,用copy /b 来解决吧,呵呵
上面讲的都是http协议的下载,其实ftp也一样可以用。用法嘛,
$ curl -u name:passwd ftp://ip:port/path/file
或者大家熟悉的
$ curl ftp://name:passwd@ip:port/path/file</p>
<ol>
<li>说完了下载,接下来自然该讲上传咯上传的option是 -T
比如我们向ftp传一个文件:
$ curl -T localfile -u name:passwd ftp://upload_site:port/path/
当然,向http服务器上传文件也可以比如
$ curl -T localfile http://cgi2.tky.3web.ne.jp/~zzh/abc.cgi
注意,这时候,使用的协议是HTTP的PUT method</li>
</ol>
<p>刚才说到PUT,嘿嘿,自然让老服想起来了其他几种methos还没讲呢! GET和POST都不能忘哦。
http提交一个表单,比较常用的是POST模式和GET模式
GET模式什么option都不用,只需要把变量写在url里面就可以了比如:
$ curl http://www.linuxidc.com/login.cgi?user=nickwolfe&password=12345</p>
<p>而POST模式的option则是 -d
比如:
$ curl -d “user=nickwolfe&password=12345” http://www.linuxidc.com/login.cgi
就相当于向这个站点发出一次登陆申请~
到底该用GET模式还是POST模式,要看对面服务器的程序设定。</p>
<p>一点需要注意的是,POST模式下的文件上的文件上传,比如</p>
<div class="highlight"><pre><code class="language-html" data-lang="html"><span class="nt"><form</span> <span class="na">method=</span><span class="s">"POST"</span> <span class="na">enctype=</span><span class="s">"multipar/form-data"</span> <span class="na">action=</span><span class="s">"http://cgi2.tky.3web.ne.jp/~zzh/up_file.cgi"</span><span class="nt">></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">submit</span> <span class="na">name=</span><span class="s">nick</span> <span class="na">value=</span><span class="s">"go"</span><span class="nt">></span></code></pre></div>
<p>这样一个HTTP表单,我们要用curl进行模拟,就该是这样的语法:
$ curl -F upload=@localfile -F nick=go http://cgi2.tky.3web.ne.jp/~zzh/up_file.cgi
罗罗嗦嗦讲了这么多,其实curl还有很多很多技巧和用法比如 https的时候使用本地证书,就可以这样
$ curl -E localcert.pem https://remote_server
再比如,你还可以用curl通过dict协议去查字典~
$ curl dict://dict.org/d:computer</p>
<ol>
<li>
<p>开启gzip请求
curl -I http://www.sina.com.cn/ -H “Accept-Encoding:gzip,defalte”</p>
</li>
<li>
<p>监控网页的响应时间
curl -o /dev/null -s -w “time_connect:%{time_connect}\ntime_starttransfer:%{time_starttransfer}\ntime_total:%{time_total}\n” http://www.kklinux.com</p>
</li>
<li>
<p>监控站点可用性
curl -o /dev/null -s -w %{http_code} http://www.kklinux.com</p>
</li>
</ol>
<hr>
<div class="pagination">
<ul>
<li class="prev"><a href="/2009/11/03/using-shell-variable-in-awk-script" title="awk调用shell变量">← Previous</a></li>
<li><a href="/archive.html">Archive</a></li>
<li class="next"><a href="/2009/11/03/zz-dd-usage" title="dd命令使用详解(转)">Next →</a></li>
</ul>
</div>
<hr>
<!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
<span class="jiathis_txt">分享到:</span>
<a class="jiathis_button_tsina">新浪微博</a>
<a class="jiathis_button_weixin">微信</a>
<a class="jiathis_button_renren">人人网</a>
<a class="jiathis_button_ydnote">有道云笔记</a>
<a class="jiathis_button_gmail">Gmail邮箱</a>
<a class="jiathis_button_twitter">Twitter</a>
<a class="jiathis_button_googleplus">Google+</a>
<a class="jiathis_button_hi">百度空间</a>
<a class="jiathis_button_fb">Facebook</a>
<a class="jiathis_button_douban">豆瓣</a>
<a href="http://www.jiathis.com/share?uid=1589850" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank">更多</a>
<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" >
var jiathis_config={
data_track_clickback:true,
summary:"",
ralateuid:{
"tsina":"1035836154"
},
shortUrl:false,
hideMore:false
}
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=1589850" charset="utf-8"></script>
<!-- JiaThis Button END -->
<!-- UY BEGIN -->
<div id="uyan_frame"></div>
<script type="text/javascript" id="UYScript" src="http://v1.uyan.cc/js/iframe.js?UYUserId=1589850" async=""></script>
<!-- UY END -->
</div>
</div>
</div>
<div class="span4">
<div class="well sidebar-nav">
<ul id="relate_blog" class="nav nav-list">
<li class="nav-header">最近文章</li>
<li><a href="/2015/08/25/kibana-custom-field-formatters">【翻译】Kibana 字段的自定义展示格式开发</a></li>
<li><a href="/2015/04/03/types-mapping-conflict-in-one-index">Elasticsearch 同一索引不同类型下同名字段的映射冲突实例</a></li>
<li><a href="/2015/03/14/spark-streaming-kafka">spark streaming 接收 kafka 数据示例</a></li>
<li><a href="/2015/03/14/kibana3-source-code-analysis">Kibana 3 源码解析</a></li>
<li><a href="/2015/03/06/kibana4-for-slowlog">用 Kibana4 实现 PHP 慢日志函数堆栈分析</a></li>
<li><a href="/2015/02/25/kibana-aggregation-execution-order-and-you">【翻译】kibana 的聚合执行次序</a></li>
<li><a href="/2015/02/25/kibana-4-rc1-is-now-available">【翻译】Kibana 4 RC1 发布</a></li>
<li><a href="/2015/02/25/kibana-4-literally">【翻译】kibana 4 正式就位</a></li>
<li><a href="/2015/02/25/kibana-4-for-investigating-pacs-super-pacs-and-your-neighbors">【翻译】用 kibana 4 调查你邻居可能投票给的人</a></li>
</ul>
</div>
<div class="well sidebar-nav">
<iframe width="100%" height="550" class="share_self" frameborder="0" scrolling="no" src="http://widget.weibo.com/weiboshow/index.php?language=&width=0&height=550&fansRow=1&ptype=1&speed=0&skin=2&isTitle=1&noborder=1&isWeibo=1&isFans=1&uid=1035836154&verifier=a26926d5&dpc=1"></iframe>
</div>
<div class="well sidebar-nav">
<div id="uyan_list_time_frame"></div>
<script type="text/javascript" id="UYScriptTime" src="http://v1.uyan.cc/js/iframe_time_list.js?UYUserId=1589850&rankType=time" async=""></script>
</div>
<div class="well sidebar-nav">
<ul id="linklists" class="nav nav-list">
<li class="nav-header">友情链接(英文)</li>
<li><a href="http://codeascraft.com/" title="Etsy 运维团队博客">Code as Craft</a></li>
<li><a href="http://devopsanywhere.blogspot.jp/" title="">devopsanywhere</a></li>
<li><a href="http://www.jedi.be/blog/" title="">Jong En Dynamische Informatica</a></li>
<li><a href="http://www.planetdevops.net/" title="">planetdevops</a></li>
<li><a href="http://www.kitchensoap.com/" title="《网站运维》作者,Etsy 运维">Kitchen Soap</a></li>
<li><a href="http://blog.johngoulah.com" title="Musings of linux, open source, cloud computing and systems">John Goulah</a></li>
<li><a href="http://serverfault.com/" title="stackexchange下属的系统工程师问答网站">serverfault</a></li>
<li><a href="http://www.thegeekstuff.com/" title="各种超酷Linux命令用法">TheGeekStuff</a></li>
<li><a href="http://neilb.org/" title="The good,the bad,and the beautiful">neilb</a></li>
<li><a href="http://www.reddit.com/r/perl/" title="">reddit perl 频道</a></li>
<li><a href="http://jpetazzo.github.io/" title="">~jpetazzo</a></li>
<li><a href="http://www.perfplanet.com/" title="News and views from the web performance blogosphere">Performance Planet</a></li>
<li><a href="http://cuddletech.com/blog/" title="Use UNIX or die">Cuddle Tech</a></li>
<li><a href="http://showmetheco.de/" title="Viacheslav Tykhanovskyi(PocketIO/Text::Haml)">No time to wait</a></li>
<li><a href="http://blog.dataloop.io/" title="A new SaaS monitoring tool for DevOps & Operations">Dataloop.IO</a></li>
<li><a href="http://www.ducea.com/" title="">MDLog:/sysadmin</a></li>
<li><a href="http://planeteria.org/perl6/" title="Perl6 文集">Planet Perl 6</a></li>
<li><a href="http://www.metaforsoftware.com/blog/" title="">metafor</a></li>
<li><a href="http://blog.kablamo.org/" title="Eric Johnson,一个游走在伦敦和北京的 Perler">kablamo</a></li>
<li><a href="http://aphyr.com/posts" title="call me maybe 吐槽系列">Aphyr</a></li>
</ul>
</div>
<div class="well sidebar-nav">
<ul id="linklists" class="nav nav-list">
<li class="nav-header">友情链接(中文)</li>
<li><a href="http://www.nginxs.com/" title="">eric</a></li>
<li><a href="http://www.hellodb.net/" title="Ali DBA 张瑞">Hello DBA</a></li>
<li><a href="http://blog.nosqlfan.com/" title="not only sql信息集散地">NoSQLfan</a></li>
<li><a href="http://ourmysql.com/" title="">OurMySQL</a></li>
<li><a href="http://timo.piqiu.me/" title="TIMO IS MY OASIS">Timo</a></li>
<li><a href="http://www.liurongxing.com/" title="">刘荣星</a></li>
<li><a href="http://www.cnadn.net/" title="F5工程师">应用交付学习之路</a></li>
<li><a href="http://scmbob.org/" title="杭州NSN工程师,shell高人~">扛一肩记忆</a></li>
<li><a href="http://www.php-oa.com/" title="音悦台技术经理">扶凯</a></li>
<li><a href="http://www.lark.net.cn/" title="王剑">lark's cloud</a></li>
<li><a href="http://log.heartoutside.com/" title="HeartOutSide">HeartOutside</a></li>
<li><a href="http://blog.liulantao.com/" title="刘兰涛">Lax</a></li>
<li><a href="http://niubie.me/" title="莫言">莫言</a></li>
<li><a href="http://noops.me/" title="小米运维部">NoOps</a></li>
<li><a href="http://www.searchtech.pro/" title="">云端分布式搜索技术</a></li>
<li><a href="http://www.usefulshare.com" title="当当网安全运维">UsefulShare</a></li>
<li><a href="http://junqili.com/" title="深入研究puppet">纸飞机</a></li>
<li><a href="http://www.chinaxing.org/" title="">ChinaXing</a></li>
<li><a href="http://bubbyroom.com/" title="守住每一天">Liuyu's blog</a></li>
<li><a href="http://blog.aka-cool.net/" title="">Aka.Why</a></li>
<li><a href="http://blog.l8ii.com/" title="刘侨">LairdNote</a></li>
<li><a href="http://flw.tools/" title="">flw的工具箱</a></li>
<li><a href="http://blog.mcshell.org/" title="">mcshell</a></li>
<li><a href="http://novoland.github.io/" title="">克鲁斯卡尔的博客</a></li>
</ul>
</div>
<div class="well sidebar-nav">
<ul id="booklists" class="nav nav-list">
<li class="nav-header">我写的技术书籍</li>
<li><a href='http://product.china-pub.com/3769604'><img src='http://images.china-pub.com/ebook3765001-3770000/3769604/shupi.jpg' border='0' alt='网站运维技术与实践'/></a></li>
<li class="nav-header">我参与翻译的技术书籍</li>
<li><a href='http://product.china-pub.com/4582714'><img src='http://images.china-pub.com/ebook4580001-4585000/4582714/shupi.jpg' border='0' alt='Puppet 实战手册'/></a></li>
<li class="nav-header">我写的电子书籍</li>
<li><a href='http://chenryn.gitbooks.io/logstash-best-practice/'><img src='https://sm3lir.cloudimage.io/s/cdn/x/https://www.gitbook.io/cover/book/chenryn/logstash-best-practice?build=1416299695472' border='0' alt='LogStash 最佳实践'/></a></li>
<li><a href='http://kibana.logstash.es/'><img src='https://sm3lir.cloudimage.io/s/cdn/x/https://www.gitbook.io/cover/book/chenryn/kibana-guide-cn?build=1415872457562' border='0' alt='Kibana 中文指南'/></a></li>
</div>
</div>
</div> <!-- row -->
<footer>
<p>© 陈子 2015
with help from <a href="http://jekyllbootstrap.com" target="_blank" title="The Definitive Jekyll Blogging Framework">Jekyll Bootstrap</a>
and <a href="http://twitter.github.com/bootstrap/" target="_blank">Twitter Bootstrap</a>
</p>
</footer>
</div> <!-- /container -->
</body>
</html>