curl 命令詳解

curl命令

網絡應用

curl命令是一個利用URL規則在命令行下工作的文件傳輸工具。它支持文件的上傳和下載,所以是綜合傳輸工具,但按傳統,習慣稱curl為下載工具。作為一款強力工具,curl支持包括HTTP、HTTPS、ftp等眾多協議,還支持POST、cookies、認證、從指定偏移處下載部分文件、用戶代理字符串、限速、文件大小、進度條等特征。做網頁處理流程和數據檢索自動化,curl可以祝一臂之力。

語法

<pre style="margin: 0px 0px 25px; background-color: rgb(230, 233, 237); border-left-width: 4px; border-left-style: solid; border-left-color: rgb(122, 208, 58); color: rgb(34, 34, 34); font-style: normal; font-variant-caps: normal; font-weight: normal; font-stretch: normal; font-size: 12px; line-height: 20px; font-family: Consolas, "Courier New", Courier, monospace; padding: 10px 12px; white-space: pre-wrap; word-wrap: break-word; overflow: hidden; background-position: initial initial; background-repeat: initial initial;">curl(選項)(參數)</pre>

選項

| -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/--header <line> | 自定義頭信息傳遞給服務器 |
| --ignore-content-length | 忽略的HTTP頭信息的長度 |
| -i/--include | 輸出時包括protocol頭信息 |
| -I/--head | 只顯示請求頭信息 |
| -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 | 作為第一個參數,關閉 .curlrc |
| -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]> | 設置代理用戶名和密碼 |
| -w/--write-out [format] | 什么輸出完成后 |
| -x/--proxy <host[:port]> | 在給定的端口上使用HTTP代理 |
| -X/--request <command> | 指定什么命令 |
| -y/--speed-time | 放棄限速所要的時間,默認為30 |
| -Y/--speed-limit | 停止傳輸速度的限制,速度時間 |

實例

文件下載

curl命令可以用來執行下載、發送各種HTTP請求,指定HTTP頭部等操作。如果系統沒有curl可以使用[yum](http://man.linuxde.net/yum "yum命令") [install](http://man.linuxde.net/install "install命令") curl安裝,也可以下載安裝。curl是將下載文件輸出到stdout,將進度信息輸出到stderr,不顯示進度信息使用--silent選項。

<pre style="margin: 0px 0px 25px; background-color: rgb(230, 233, 237); border-left-width: 4px; border-left-style: solid; border-left-color: rgb(122, 208, 58); color: rgb(34, 34, 34); font-style: normal; font-variant-caps: normal; font-weight: normal; font-stretch: normal; font-size: 12px; line-height: 20px; font-family: Consolas, "Courier New", Courier, monospace; padding: 10px 12px; white-space: pre-wrap; word-wrap: break-word; overflow: hidden; background-position: initial initial; background-repeat: initial initial;">curl URL --silent</pre>

這條命令是將下載文件輸出到終端,所有下載的數據都被寫入到stdout。

使用選項-O將下載的數據寫入到文件,必須使用文件的絕對地址:

<pre style="margin: 0px 0px 25px; background-color: rgb(230, 233, 237); border-left-width: 4px; border-left-style: solid; border-left-color: rgb(122, 208, 58); color: rgb(34, 34, 34); font-style: normal; font-variant-caps: normal; font-weight: normal; font-stretch: normal; font-size: 12px; line-height: 20px; font-family: Consolas, "Courier New", Courier, monospace; padding: 10px 12px; white-space: pre-wrap; word-wrap: break-word; overflow: hidden; background-position: initial initial; background-repeat: initial initial;">curl http://man.linuxde.net/text.iso --silent -O</pre>

選項-o將下載數據寫入到指定名稱的文件中,并使用--progress顯示進度條:

<pre style="margin: 0px 0px 25px; background-color: rgb(230, 233, 237); border-left-width: 4px; border-left-style: solid; border-left-color: rgb(122, 208, 58); color: rgb(34, 34, 34); font-style: normal; font-variant-caps: normal; font-weight: normal; font-stretch: normal; font-size: 12px; line-height: 20px; font-family: Consolas, "Courier New", Courier, monospace; padding: 10px 12px; white-space: pre-wrap; word-wrap: break-word; overflow: hidden; background-position: initial initial; background-repeat: initial initial;">curl http://man.linuxde.net/test.iso -o filename.iso --progress
######################################### 100.0%</pre>

斷點續傳

curl能夠從特定的文件偏移處繼續下載,它可以通過指定一個便宜量來下載部分文件:

<pre style="margin: 0px 0px 25px; background-color: rgb(230, 233, 237); border-left-width: 4px; border-left-style: solid; border-left-color: rgb(122, 208, 58); color: rgb(34, 34, 34); font-style: normal; font-variant-caps: normal; font-weight: normal; font-stretch: normal; font-size: 12px; line-height: 20px; font-family: Consolas, "Courier New", Courier, monospace; padding: 10px 12px; white-space: pre-wrap; word-wrap: break-word; overflow: hidden; background-position: initial initial; background-repeat: initial initial;">curl URL/File -C 偏移量

偏移量是以字節為單位的整數,如果讓curl自動推斷出正確的續傳位置使用-C -:

curl -C -URL</pre>

使用curl設置參照頁字符串

參照頁是位于HTTP頭部中的一個字符串,用來表示用戶是從哪個頁面到達當前頁面的,如果用戶點擊網頁A中的某個連接,那么用戶就會跳轉到B網頁,網頁B頭部的參照頁字符串就包含網頁A的URL。

使用--referer選項指定參照頁字符串:

<pre style="margin: 0px 0px 25px; background-color: rgb(230, 233, 237); border-left-width: 4px; border-left-style: solid; border-left-color: rgb(122, 208, 58); color: rgb(34, 34, 34); font-style: normal; font-variant-caps: normal; font-weight: normal; font-stretch: normal; font-size: 12px; line-height: 20px; font-family: Consolas, "Courier New", Courier, monospace; padding: 10px 12px; white-space: pre-wrap; word-wrap: break-word; overflow: hidden; background-position: initial initial; background-repeat: initial initial;">curl --referer http://www.google.com http://man.linuxde.net</pre>

用curl設置cookies

使用--cookie "COKKIES"選項來指定cookie,多個cookie使用分號分隔:

<pre style="margin: 0px 0px 25px; background-color: rgb(230, 233, 237); border-left-width: 4px; border-left-style: solid; border-left-color: rgb(122, 208, 58); color: rgb(34, 34, 34); font-style: normal; font-variant-caps: normal; font-weight: normal; font-stretch: normal; font-size: 12px; line-height: 20px; font-family: Consolas, "Courier New", Courier, monospace; padding: 10px 12px; white-space: pre-wrap; word-wrap: break-word; overflow: hidden; background-position: initial initial; background-repeat: initial initial;">curl http://man.linuxde.net --cookie "user=root;pass=123456"</pre>

將cookie另存為一個文件,使用--cookie-jar選項:

<pre style="margin: 0px 0px 25px; background-color: rgb(230, 233, 237); border-left-width: 4px; border-left-style: solid; border-left-color: rgb(122, 208, 58); color: rgb(34, 34, 34); font-style: normal; font-variant-caps: normal; font-weight: normal; font-stretch: normal; font-size: 12px; line-height: 20px; font-family: Consolas, "Courier New", Courier, monospace; padding: 10px 12px; white-space: pre-wrap; word-wrap: break-word; overflow: hidden; background-position: initial initial; background-repeat: initial initial;">curl URL --cookie-jar cookie_file</pre>

用curl設置用戶代理字符串

有些網站訪問會提示只能使用IE瀏覽器來訪問,這是因為這些網站設置了檢查用戶代理,可以使用curl把用戶代理設置為IE,這樣就可以訪問了。使用--user-agent或者-A選項:

<pre style="margin: 0px 0px 25px; background-color: rgb(230, 233, 237); border-left-width: 4px; border-left-style: solid; border-left-color: rgb(122, 208, 58); color: rgb(34, 34, 34); font-style: normal; font-variant-caps: normal; font-weight: normal; font-stretch: normal; font-size: 12px; line-height: 20px; font-family: Consolas, "Courier New", Courier, monospace; padding: 10px 12px; white-space: pre-wrap; word-wrap: break-word; overflow: hidden; background-position: initial initial; background-repeat: initial initial;">curl URL --user-agent "Mozilla/5.0"
curl URL -A "Mozilla/5.0"</pre>

其他HTTP頭部信息也可以使用curl來發送,使用-H"頭部信息" 傳遞多個頭部信息,例如:

<pre style="margin: 0px 0px 25px; background-color: rgb(230, 233, 237); border-left-width: 4px; border-left-style: solid; border-left-color: rgb(122, 208, 58); color: rgb(34, 34, 34); font-style: normal; font-variant-caps: normal; font-weight: normal; font-stretch: normal; font-size: 12px; line-height: 20px; font-family: Consolas, "Courier New", Courier, monospace; padding: 10px 12px; white-space: pre-wrap; word-wrap: break-word; overflow: hidden; background-position: initial initial; background-repeat: initial initial;">curl -H "Host:man.linuxde.net" -H "accept-language:zh-cn" URL</pre>

curl的帶寬控制和下載配額

使用--limit-rate限制curl的下載速度:

<pre style="margin: 0px 0px 25px; background-color: rgb(230, 233, 237); border-left-width: 4px; border-left-style: solid; border-left-color: rgb(122, 208, 58); color: rgb(34, 34, 34); font-style: normal; font-variant-caps: normal; font-weight: normal; font-stretch: normal; font-size: 12px; line-height: 20px; font-family: Consolas, "Courier New", Courier, monospace; padding: 10px 12px; white-space: pre-wrap; word-wrap: break-word; overflow: hidden; background-position: initial initial; background-repeat: initial initial;">curl URL --limit-rate 50k</pre>

命令中用k(千字節)和m(兆字節)指定下載速度限制。

使用--max-filesize指定可下載的最大文件大小:

<pre style="margin: 0px 0px 25px; background-color: rgb(230, 233, 237); border-left-width: 4px; border-left-style: solid; border-left-color: rgb(122, 208, 58); color: rgb(34, 34, 34); font-style: normal; font-variant-caps: normal; font-weight: normal; font-stretch: normal; font-size: 12px; line-height: 20px; font-family: Consolas, "Courier New", Courier, monospace; padding: 10px 12px; white-space: pre-wrap; word-wrap: break-word; overflow: hidden; background-position: initial initial; background-repeat: initial initial;">curl URL --max-filesize bytes</pre>

如果文件大小超出限制,命令則返回一個非0退出碼,如果命令正常則返回0。

用curl進行認證

使用curl選項 -u 可以完成HTTP或者FTP的認證,可以指定密碼,也可以不指定密碼在后續操作中輸入密碼:

<pre style="margin: 0px 0px 25px; background-color: rgb(230, 233, 237); border-left-width: 4px; border-left-style: solid; border-left-color: rgb(122, 208, 58); color: rgb(34, 34, 34); font-style: normal; font-variant-caps: normal; font-weight: normal; font-stretch: normal; font-size: 12px; line-height: 20px; font-family: Consolas, "Courier New", Courier, monospace; padding: 10px 12px; white-space: pre-wrap; word-wrap: break-word; overflow: hidden; background-position: initial initial; background-repeat: initial initial;">curl -u user:pwd http://man.linuxde.net
curl -u user http://man.linuxde.net</pre>

只打印響應頭部信息

通過-I或者-head可以只打印出HTTP頭部信息:

<pre style="margin: 0px 0px 25px; background-color: rgb(230, 233, 237); border-left-width: 4px; border-left-style: solid; border-left-color: rgb(122, 208, 58); color: rgb(34, 34, 34); font-style: normal; font-variant-caps: normal; font-weight: normal; font-stretch: normal; font-size: 12px; line-height: 20px; font-family: Consolas, "Courier New", Courier, monospace; padding: 10px 12px; white-space: pre-wrap; word-wrap: break-word; overflow: hidden; background-position: initial initial; background-repeat: initial initial;">[root@localhost text]# curl -I http://man.linuxde.net
HTTP/1.1 200 OK
Server: nginx/1.2.5
date: Mon, 10 Dec 2012 09:24:34 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Pingback: http://man.linuxde.net/xmlrpc.php</pre>

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,797評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,179評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,628評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,642評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,444評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,948評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,040評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,185評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,717評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,602評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,794評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,316評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,045評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,418評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,671評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,414評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,750評論 2 370

推薦閱讀更多精彩內容

  • linux命令之curl curl命令詳解 1、curl命令簡介 curl命令是一個利用URL規則在命令行下工作的...
    道無虛閱讀 1,667評論 0 0
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,778評論 18 139
  • 轉載自:http://www.cnblogs.com/txw1958/archive/2013/01/19/286...
    php_bruce閱讀 2,290評論 1 5
  • 一、什么是CURL? cURL 是一個利用URL語法規定來傳輸文件和數據的工具,支持很多協議,如HTTP、FTP、...
    伊Summer閱讀 1,285評論 0 4
  • CURLOPT_RETURNTRANSFER 選項:curl_setopt($ch, CURLOPT_RETURN...
    Talentisan閱讀 16,890評論 0 2