Curl命令

1.訪問http頁面內容,一般將下載文件輸出到stdout,將進度信息輸出到stderr,要想避免顯示進度信息,使用--silent,-o用來將下載的數據寫入指定名稱的文件中.若是須要在下載過程當中顯示如#的進度條,用--progress代替--silentphp

1
2
3
4
5
6
7
8
9
10
11
12
root@10.1.1.200:curl # curl http://www.163.com/index.html -o index.html
   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                  Dload  Upload   Total   Spent    Left  Speed
   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  465k    0  465k    0     0   374k      0 --:--:--  0:00:01 --:--:--  415k
root@10.1.1.200:curl #
root@10.1.1.200:curl # curl http://www.163.com/index.html -o index1.html  --progress
######################################################################## 100.0%
######################################################################## 100.0%
root@10.1.1.200:curl # curl http://www.163.com/index.html -o index.html --silent
root@10.1.1.200:curl # ls
index1.html  index.html


2.只打印響應頭部信息(不包括數據部分)html

   只打印響應頭部(response header) 有助於進行各類檢查或統計,例如,要求無須下載整個頁面內容就可以檢驗某個頁面是否可以打開,那麼咱們只用讀取http響應頭部就可以知道這個頁面是否可用。

   檢查HTTP頭部的一個用例就是在下載以前先查看文件大小,咱們能夠在下載以前,經過檢查HTTP頭部中的Content-Length參數來得知文件的長度,一樣還能夠從頭部檢索出其餘一些有用的參數,last-modified參數能告訴咱們遠程文件最後的改動時間

   經過-I或--head就能夠只打印HTTP頭部信息,而無須下載遠程文件。linux

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
root@10.1.1.200:curl # curl -I http://www.163.com
HTTP /1 .1 200 OK
Expires: Mon, 14 Jan 2013 03:53:33 GMT
Date: Mon, 14 Jan 2013 03:52:13 GMT
Server: nginx
Content-Type: text /html ; charset=GBK
Transfer-Encoding: chunked
Vary: Accept-Encoding,User-Agent,Accept
Cache-Control: max-age=80
Age: 66
X-Via: 1.1 jssq185:88 (Cdn Cache Server V2.0), 1.1 zjfy81:8361 (Cdn Cache Server V2.0)
Connection: keep-alive
 
root@10.1.1.200:curl # curl -I http://www.sina.com
HTTP /1 .0 301 Moved Permanently
Date: Mon, 14 Jan 2013 03:11:55 GMT
Server: Apache
Location: http: //www .sina.com.cn/
Cache-Control: max-age=3600
Expires: Mon, 14 Jan 2013 04:11:55 GMT
Vary: Accept-Encoding
Content-Length: 231
Content-Type: text /html ; charset=iso-8859-1
Age: 2491
X-Cache: HIT from sh201-20.sina.com.cn
Connection: close
 
root@10.1.1.200:curl # curl --head http://www.taobao.com
HTTP /1 .1 200 OK
Server: Tengine
Date: Mon, 14 Jan 2013 03:53:37 GMT
Content-Type: text /html ; charset=gbk
Connection: keep-alive
Vary: Accept-Encoding
Expires: Mon, 14 Jan 2013 04:53:37 GMT
Cache-Control: max-age=3600


3.指定客戶端.nginx

   有些網絡資源首先須要判斷用戶使用的是什麼瀏覽器,符合標準了纔可以下載或者瀏覽,。此時curl能夠把本身「假裝」成任何其餘瀏覽器:web

1
2
3
4
5
6
7
root@10.1.1.200:curl # curl http://www.test.com
root@10.1.1.200:apache2 #tail -f www.test.com_access.com
10.1.1.200 - - [14 /Jan/2013 :00:33:59 +0800] "GET / HTTP/1.1" 200 54646 "-" "curl/7.18.2 (x86_64-pc-linux-gnu) libcurl/7.18.2 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.8 libssh2/0.18"
 
root@10.1.1.200:curl # curl -A "Mozilla/6.0 (compatible; MSIE 5.01; Windows NT 5.0)"  http://www.test.com
root@10.1.1.200:apache2 #tail -f www.test.com_access.com
10.1.1.200 - - [14 /Jan/2013 :00:36:45 +0800] "GET / HTTP/1.1" 200 54499 "-" "Mozilla/6.0 (compatible; MSIE 6.01; Windows NT 6.0)"

      -A ""該選項能夠指定客戶端類型,服務器經過該選項判斷用戶應用的平臺及瀏覽器信息,上面這個指令表示curl假裝成了windows機器.apache


4.用curl進行認證windows

   登陸某些頁面或ftp須要先進行認證,輸入用戶名和密碼。curl的這個選項能夠直接處理這類操做,用指定的帳號和密碼進行登陸認證。後面的選項指定代理的用戶名和密碼,這樣即可以直接用這個代理訪問網頁了.-x選項爲http指定代理及端口,若是不指定端口,默認爲1080瀏覽器

 

1
curl -U user:password -x 201.36.208.19:3128 http: //www . test .com


5.斷點續傳服務器

   -C提供斷點續傳功能,若是未指定offset,或者直接用"-C -",則curl會本身分析該從什麼位置開始續傳。網絡

1
root@10.1.1.200:curl # curl  -C - http://www.sina.com


6.限定curl可佔用的帶寬

1
root@10.1.1.200:curl # curl  http://www.baidu.com --limit-rate 20k


7.分塊下載

   -r選項指定下載字節的範圍,常應用於分塊下載文件。range的表示方式有多種,如100-500,則指定從100開始的400個字節數據;-500表示 最後的500個字節;5000-表示從第5000個字節開始的全部字節;另外還能夠同時指定多個字節塊,中間用","分開.

1
2
root@10.1.1.200:curl # curl -r 0-1024 http://www.test.com/a.zip
root@10.1.1.200:curl # curl -r 1025- http://www.test.com/a.zip

      先下1M,而後再下剩下的部分


8.curl上傳

   用POST方法向服務器提交數據。這時的URL是看不到的,所以須要使用-d參數來抓取這個頁面

1
url -d "action=add&name=davehe" -d "id=1" http: //10 .1.1.200 /example .php  

   若是使用了-F參數,這個命令的實質是將本地的文件用POST上傳到服務器。-F參數以name=value的方式來指定參數內容,若是值是一個文件,則須要以name=@file的方式來指定。

1
curl -F "filename=@/dave/test.tar.gz" http: //10 .1.1.200 /example .php


8.測試參數

測試鏈接www.163.com須要多少時間

1
2
root@10.1.1.200:curl # curl -o /dev/null -s -w %{time_connect}:%{time_starttransfer}:%{time_total} www.163.com
0.013:0.022:0.089

查看網頁文件的大小

1
2
root@10.1.1.200:curl # curl -o /dev/null -s -w %{size_header} http://www.163.com
368

其餘經常使用經常使用http變量

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
http_code:http返回相似404,200,500等
  
time_total:總相應時間
  
time_namelookup:域名解析時間
  
time_connect:鏈接到目標地址耗費的時間
  
time_pretransfer:從執行到開始傳輸文件的時間間隔
  
time_starttransfer:從執行到開始傳輸文件的時間間隔
  
size_download:下載網頁或文件大小
  
size_upload:上傳文件大小
  
size_header:響應頭
  
size_request:發送請求參數大小
  
speed_download:傳輸速度
  
speed_upload:平均上傳速度
  
content_type:下載文件類型.


9 指定方式請求:       

curl -I  -H "ContentType: text" --request GET\DELETE\PUT http://www.xxxx.com


10 curl命令綁定host訪問網頁的方法:

假設訪問頁面地址爲: http://www.abc.com/test.jsp

www.abc.com 域名解析到ip: 192.168.0.1 則curl訪問方式爲以下:


curl -H "Host:www.abc.com"  "http://192.168.1.1/test.jsp"

相關文章
相關標籤/搜索