文章來源:http://dbajun.iteye.com/blog/1813801
curl命令詳解:http://blog.sina.com.cn/s/blog_94cf845f0102w8vt.html
curl命令是一個功能強大的網絡工具,它能夠通過http、ftp等方式下載文件,也能夠上傳文件。其實curl遠不止前面所說的那些功能,大家可以通過man curl閱讀手冊頁獲取更多的信息。類似的工具還有wget。
常用參數
curl命令參數很多,這里只列出我曾經用過、特別是在shell腳本中用到過的那些。
-v/--verbose 小寫的v參數,用於打印更多信息,包括發送的請求信息,這在調試腳本是特別有用。
-m/--max-time <seconds> 指定處理的最大時長
-H/--header <header> 指定請求頭參數
-s/--slient 減少輸出的信息,比如進度
--connect-timeout <seconds> 指定嘗試連接的最大時長
-x/--proxy <proxyhost[:port]> 指定代理服務器地址和端口,端口默認為1080
-T/--upload-file <file> 指定上傳文件路徑
-o/--output <file> 指定輸出文件名稱
-d/--data/--data-ascii <data> 指定POST的內容
--retry <num> 指定重試次數
-e/--referer <URL> 指定引用地址
-I/--head 僅返回頭部信息,使用HEAD請求
使用示例
示例一 獲取指定網頁
[root@jfht ~]# curl http://www.sunrisecorp.net/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<meta name="title" content="歡迎您 - 上海騰一" />
<meta name="keyword" content="上海騰一,融合通信,電子商務,語音通信,數據通信,基站工程外包托管,物聯網,網站建設,電子商務解決方案,移動互聯網,短信,彩信,呼叫中心,多方會議,PBX,IVR,電子商務策划方案,設備代維,網絡優化,通信工程,電信增值,3G" />
<meta name="description" content="上海騰一信息技術有限公司專注於電信增值、通信工程、電子商務等領域,擁有近十年的經驗。" />
<title>
歡迎您 - 上海騰一
</title>
</body>
</html>[root@jfht ~]#
示例二 查看響應頭信息
[root@jfht ~]# curl -I http://www.sunrisecorp.net/
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"17801-1285643951000"
Last-Modified: Tue, 28 Sep 2010 03:19:11 GMT
Content-Type: text/html
Content-Length: 17801
Date: Tue, 12 Oct 2010 12:49:20 GMT
[root@jfht ~]#
===========================================================================
通過curl的-w參數我們可以自定義curl的輸出
代碼如下
# curl -I -m 10 -o /dev/null -s -w %{http_code} IP地址或者網址
上面的輸出是不含換行的,如果需要換行的話,加上\n
代碼如下
# curl -I -m 10 -o /dev/null -s -w %{http_code} IP地址或者網址
200
# curl -I -m 10 -o /dev/null -s -w %{http_code}"\n" IP地址或者網址
200
============================================================================
curl下載
在官網下載win32or64.zip,官網下載
下載缺失的dll文件
用dos進入解壓目錄,運行curl命令即可
命令實例
1、開啟gzip請求
curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte
2、監控網頁的響應時間
curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" "http://www.kklinux.com"
3. 監控站點可用性
curl -o /dev/null -s -w %{http_code} "http://www.kklinux.com"
4、以http1.0協議請求(默認為http1.1)
curl -0 ..............
監控站點首頁下載時間:
curl -o /dev/null -s -w ‘%{time_total}’ http://www.miotour.com
curl -o /dev/null -s -w ‘%{http_code}’ http://www.miotour.com
curl -o /dev/null -s -w %{http_code}:%{time_connect}:%{time_starttransfer}:%{time_total} http://www.miotour.com
結果:2.547
-s 靜默輸出;沒有-s的話就是下面的情況,這是在腳本等情況下不需要的信息。
[ec2-user@ip-10-122-250-19 ~]$ curl -o /dev/null -w ‘%{time_total}’ http://www.miotour.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 67770 0 67770 0 0 19228 0 –:–:– 0:00:03 –:–:– 20705
結果:3.524
監控首頁各項時間指標:
curl -o /dev/null -s -w ‘%{time_connect}:%{time_starttransfer}:%{time_total}’ http://www.miotour.com
結果: 0.244: 1.044: 2.672
時間指標解釋 :
time_connect 建立到服務器的 TCP 連接所用的時間
time_starttransfer 在發出請求之后,Web 服務器返回數據的第一個字節所用的時間
time_total 完成請求所用的時間
在 發出請求之后,Web 服務器處理請求並開始發回數據所用的時間是
(time_starttransfer)1.044 - (time_connect)0.244 = 0.8 秒
客戶機從服務器下載數據所用的時間是
(time_total)2.672 - (time_starttransfer)1.044 = 1.682 秒
指定特定主機IP地址訪問網站
curl -x 61.135.169.105:80 http://www.baidu.com
curl -x 61.135.169.125:80 http://www.baidu.com
curl用法大全
-x 指定訪問IP與端口號
curl -x 192.168.1.1:80 http://www.miotour.com
-I 僅僅取文件的http頭部
curl -I -x 192.168.1.1:80 http://www.miotour.com
用referer做的防盜鏈,就可以使用-e來設置
curl -e “http://www.qiecuo.org” http:// www.miotour.com -v -I
-H去構造你想要的http頭部
curl -H “X-Forward-For:8.8.8.8″ http://www.miotour.com -v -I
curl反饋時間,例如連接時間,下載時間等信息
curl -w %{time_connect}:%{time_starttransfer}:%{time_total} -s -o /dev/null
將一個文件保存到硬盤上,命名為file.html
curl -o file.html http://www.miotour.com/index.html
下載index.html文件, -O是大寫的字母
curl -O http://www.miotour.com/index.html
curl提交用戶名和密碼
curl http://name:passwd@www.miotour.com
curl -u name:passwd http://www.miotour.com
-b “cookie” 此參數用來構造一個攜帶cookie的請求