Curl是一種支持多種協議(http、https,ftp)等,利用url規則在命令行下工作的文件傳輸工具,還支持POST、cookies、認證、從指定偏移處下載部分文件、用戶代理字符串、限速、文件大小、進度條等特征
1.語法:
curl [options] [URL...]
2.參數:
-a/--append?用于上傳文件時,指定附加到目標文件,文件存在則覆蓋,不存在則創建
-A/--user-agent?指定客戶端agent,即瀏覽器類型
-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方式傳送數據
-D/--dump-header <file>??把header信息寫入到該文件中
-e/--referer??來源網址
--create-dirs建立本地目錄的目錄層次結構
-F/--form <name=content>模擬http表單提交數據
-G/--get???以get的方式來發送數據
-H/--header <line>???自定義頭信息傳遞給服務器
--ignore-content-length??忽略的HTTP頭信息的長度
-i/--include??輸出時包括protocol頭信息
-I/--head??只顯示請求頭信息
--interface <interface>??使用指定網絡接口/地址
-K/--config???指定的配置文件讀取
-l/--list-only????列出ftp目錄下的文件名稱
--limit-rate <rate>??設置傳輸速度
-r/ --range?用于分段下載使用 單位為bytes
--local-port<NUM>強制使用本地端口號
-m/--max-time <seconds>??設置最大傳輸時間
-o/--output???把輸出寫到該文件中
-O/--remote-name??把輸出寫到該文件中,保留遠程文件的文件名
-p/--proxytunnel??使用HTTP代理
-Q/--quote <cmd>??文件傳輸前,發送命令到服務器
-R/--remote-time??在本地生成文件時,保留遠程文件時間
--retry <num>傳輸出現問題時,重試的次數
--retry-delay <seconds>??傳輸出現問題時,設置重試間隔時間
--retry-max-time <seconds>??傳輸出現問題時,設置最大重試時間
-s/--silent???靜默模式。不輸出任何東西
-S/--show-error???顯示錯誤
--trace <file>????對指定文件進行debug
-T/--upload-file <file>??上傳文件
--url <URL>???Spet URL to work with
-u/--user <user[:password]>設置服務器的用戶和密碼
-U/--proxy-user <user[:password]>??設置代理用戶名和密碼
-x/--proxy <host[:port]>在給定的端口上使用HTTP代理,默認1080
-X/--request <command>???指定什么命令
-w/--write-out [format]??指定輸出變量格式,變量需按%{變量}的格式,如果要輸出自己定義的名字,直接使用%%
?
3.具體用法
1)查看url的請求頭信息
curl -I??http://club.xywy.com
?
HTTP/1.1 200 OK
Server: XT-server/0.0
Date: Mon, 28 Sep 2015 02:51:38 GMT
Content-Type: text/html
Connection: close
Vary: Accept-Encoding
XT_ID: cweb-4
2)自定義請求頭信息,返回請求狀態
curl -I -H "HOST:club.xywy.com"??http://115.182.211.132
?
HTTP/1.1 200 OK
Server: XT-server/0.0
Date: Mon, 28 Sep 2015 02:54:53 GMT
Content-Type: text/html
Connection: close
Vary: Accept-Encoding
XT_ID: cweb-4
?
-H?指定請求頭信息,
curl -H "Connection:keep-alive \n User-Agent: Mozilla/5.0 \n Host:bbs.beyond.com"???http://192.168.1.1/index.php??-I
3)查看url的各類響應時間
curl -o /dev/null -s -w http_code:%{http_code}\\ntime_namelookup:%{time_namelookup}\\ntime_redirect:%{time_redirect}\\ntime_pretransfer:%{time_pretransfer}\\ntime_connect:%{time_connect}\\ntime_starttransfer:%{time_starttransfer}\\ntime_total:%{time_total}\\nspeed_download:%{speed_download}\\n??http://club.xywy.com??
?
http_code:200?????????//返回的狀態嗎
time_namelookup:0.032?????//dns解析域名的時間
time_redirect:0.000???????//重定向時間?
time_pretransfer:0.037????????//從開始到準備傳輸的時間
time_connect:0.037????????????//建立連接的總時間
time_starttransfer:0.044???????//從發出請求之后,web返回第一個字節所用時間
time_total:0.463??????????????????//client發出請求;到web的server發送會所有的相應數據的時間
speed_download:617565.000?????????//下載速度,單位?byte/s
?
?
?
size_upload ? ? ? ? ?//上傳文件大小
size_header ? ? ? ? ?//響應頭
size_request ? ? ? ? ?//發送請求參數大小
speed_download ? ? ?//傳輸速度
speed_upload ? ? ? ? //平均上傳速度
content_type?
?
?
?
建立tcp連接的時間:time_connect - time_namelookup
?
4)指定來源地址,指定瀏覽器類型
curl -I -A "Mozilla/5.0 Firefox/21.0"??-e "http://www.baidu.com" -H "Host:bbs.beyond.com"???http://192.168.1.1/index.php
?
5)保存輸出到文件
curl -o baidu.html http://www.baidu.com
curl -O https://www.baidu.com/img/bd_logo1.png
?
-o?指定保存文件的位置,把輸出重定向到一個文件
-O?下載url中文件,所以url中需指明到某個文件
6)分段下載,并限速
目標文件大小:900M
現在分三次下載,每次300M,并限速1M/s
[root@test01 ~]#??curl -r??0-300000000 -o test.sh.part1 --limit-rate 1024000 -H "HOST:bbs.beyond.com"??http://127.0.0.1/test.sh??-w speed_download:%{speed_download}\\ntime:%{time_total}\\n?
??% Total????% Received % Xferd??Average Speed???Time????Time?????Time??Current
?????????????????????????????????Dload??Upload???Total???Spent????Left??Speed
100??286M??100??286M????0?????0???998k??????0??0:04:53??0:04:53 --:--:-- 1142k
speed_download:1022549.000
time:293.384
[root@test01 ~]# du -sh test.sh.part1?
287M????test.sh.part1
?
斷點續傳,使用#顯示進度
curl -C -???-# -r 0-1024000000 -o test.sh.part1 --limit-rate 10M -H "HOST:bbs.beyond.com"??http://127.0.0.1/test.sh??-w speed_download:%{speed_download}\\ntime:%{time_total}\\n
7)使用代理及用戶名密碼
curl -x 1.1.1.1:80 http://www.baidu.com
curl -x 1.1.1.2:80 -U user:password?http://www.baidu.com
?
當網站設置用戶身份驗證時,不加用戶名密碼是不行的
[root@test01 ~]#??curl??-I -H "HOST:bbs.beyond.com"??http://127.0.0.1/test.sh
HTTP/1.1 401 Unauthorized
Server: nginx
Date: Mon, 28 Sep 2015 07:05:52 GMT
Content-Type: text/html
Content-Length: 188
Connection: keep-alive
WWW-Authenticate: Basic realm="plese input your name and password"
現在使用-u參數指定用戶名和密碼
[root@test01 ~]#??curl??-I -H "HOST:bbs.beyond.com" -u fuzj:fuzj123??http://127.0.0.1/test.sh?
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 28 Sep 2015 07:06:27 GMT
Content-Type: application/octet-stream
Content-Length: 943718400
Last-Modified: Mon, 28 Sep 2015 06:25:42 GMT
Connection: keep-alive
ETag: "5608dd66-38400000"
Accept-Ranges: bytes
?
注意:-u是指定的網站授權的用戶名和密碼,-U指定的是代理服務器授權的用戶名密碼
8)保留cookie信息到本地,并使用
curl??-c cookie.txt??-I -H "HOST:bbs.beyond.com" -u fuzj:fuzj123??http://127.0.0.1/forum.php
curl??-b cookie.txt??-I -H "HOST:bbs.beyond.com" -u fuzj:fuzj123??http://127.0.0.1/forum.php
?
9)curl 訪問 302跳轉后的頁面
?
curl -s -L -w '%{url_effective}\n' dns.ip.cn
?
10)忽略url中的特殊符號:
抱錯:curl: (3) [globbing] bad range specification in column 120"
Escape [ and ] or use -g or --globoff
An example URL that will cause the error is this:
curl "http://www.example.com/?test[]=123"
Either escape the square brackets like this:
curl "http://www.example.com/?test\[\]=123"
or use --globoff like this:
curl --globoff "http://www.example.com/?test[]=123"
or the shorter -g like this:
curl --g "http://www.example.com/?test[]=123"
?