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>