用法:
curl [options] [URL...]
DESCRIPTION(描述):
curl用來‘從server傳輸資料’或者‘傳輸資料到server’,支援多種協定(HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE)。
curl提供了大量有用的技巧像 proxy support, user authentication, FTP upload, HTTP post, SSL connections, cookies, file transfer resume and more。
curl由libcurl支援完成transfer-related(傳輸相關)的功能
URL:
最好是 協定://url的形式。可以采用如下方式指定多個url
ftp://ftp.numericals.com/file[1-100].txt
ftp://ftp.numericals.com/file[001-100].txt (with leading zeros)
從curl 7.15.1版本開始,可以為一個範圍指定‘步進’,可以取得每N個url比對的資源,如下
http://www.numericals.com/file[1-100:10].txt
對于一個curl指令請求多個檔案的情況,curl嘗試‘重用’連結(re-use connections)。
PROGRESS METER(進度條):
curl預設會将檔案内容顯示到終端,可以通過linux重定向或者‘-o’選項重定向至檔案,并顯示一個如下進度條
如果希望出現更簡短的‘#’的進度條,可以通過‘-#’選項實作
OPTIONS(選項):
通常,所有布爾選項通過--option開啟,--no-option 關閉
-a/--append
(FTP/SFTP) 在upload過程中使用該選項,告訴curl把内容附加(append)到目标檔案最後,若目 标不存在則建立
-A/--user-agent <agent string>
(HTTP)指定發送給server的User-Agent,若指定的字元串中含有空格,放到單引(single quote) 内。該功能同樣可以通過‘-H/--header’選項設定。如果設定多次,最後一次的設定生效。
效果如下
--anyauth
(HTTP)
-b/--cookie <name=data>
(HTTP)将資料作為cookie傳遞給server。它會認為這些data是server之前通過"Set-Cookie:"指令在用戶端設定的。data格式為"NAME1=VALUE1;NAME2=VALUE2"。
-B/--use-ascii
--basic
--ciphers <list of ciphers>
--compressed
(HTTP) Request a compressed response using one of the algorithms libcurl supports, and return the uncompressed document. If this option is used and the server sends an unsupported encoding, curl will report an error.
--connect-timeout <seconds>
設定與server建立連結的逾時時間,一旦與server建立連接配接成功,則此選項便不再使用
-c/--cookie-jar <file name>
-C/--continue-at <offset>
續傳相關
--create-dirs
--crlf
(FTP)在上産過程中将LF轉換成CRLF
--crlfile <file>
(HTTPS/FTPS)
-d/--data <data>
(HTTP)通過POST請求向server發送指定的data。這和在浏覽器填寫一個表單并點選送出按鈕的過程相同。This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded(注意和 -F/--form 的差別)
-d/--data和--data-ascii一樣,如果想以純二進制POST 資料(To post data purely binary),應該使用--data-binary選項;想以URL-encode方式POST資料,使用--data-urlencode。
如果這些選項在一個curl指令裡出現多次,data片段會被用‘&’組合成一串data,例如‘-d name=daniel -d skill=lousy’會被自動組合為'name=daniel&skill=lousy' 的形式。
如果資料部分以 @ 開頭,則剩餘部分應該是一個檔案,curl從該檔案讀取内容傳遞給server; 如果資料部分以 - 開頭,表示希望從stdin讀取資料。檔案的内容必須是經過URL-encoded編碼的,也可以指定多個檔案。
--data-binary <data>
--data-urlencode <data>
--delegation
--digest
(HTTP) Enables HTTP Digest authentication. This is a authentication that prevents the password from being sent over the wire in clear text. Use this in combination with the normal -u/--user option to set user name and password. See also --ntlm, --negotiate and --anyauth for related options.
--disable-eprt
-D/--dump-header <file>
将協定頭資訊(protocol headers)寫進指定的檔案
-e/--referer <URL>
(HTTP)發送"Referer Page"資訊到server。可以用來欺騙server繞過一些簡單的防盜鍊設定。同樣可以通過-H/--header選項設定。當使用-L/--location時可以将該選項參數設為”;auto“。自動将--referer 設定為前一個url。
-F/--form <name=content>
(HTTP)讓curl仿真完成浏覽器端填寫表單并送出的過程。This causes curl to POST data using the Content-Type multipart/form-data(注意和 -d/--data <data> 的差別)。如果要将'内容'部分指定為檔案,在檔案前面加上 @;如果僅要讀出檔案裡的内容,在檔案前面加 < 。@和<的差別在于:
@以檔案形式post;< 将檔案内容取出來post。
舉例:上傳你的密碼檔案到server,表單名字為’password‘,檔案為/etc/passwd,指令如下
如果想從stdin讀取content,用 -
也可以通過’type=‘告訴curl以何種Content-Type post,如下
curl -F "[email protected];type=text/html" url.com
--form-string <name=string>
-g/--globoff
-G/--get
-H/--header <header>
(HTTP)請求頁面是額外的請求頭資訊。可以設定任意數量的額外請求頭資訊,如果你設定的頭資訊的名稱和curl預設使用的頭資訊相同,則你額外設定的頭資訊會覆寫curl預設會使用的。若想删除某個請求頭資訊,則将其值指定為空即可,例如 -H "Host:"
例如:開啟gzip傳輸
curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte
-i/--include
在輸出中包含響應頭資訊
--interface <name>
指定走哪個網絡接口
-I/--head
(HTTP/FTP/FILE)僅取回請求檔案的頭資訊
-j/--junk-session-cookies
(HTTP) When curl is told to read cookies from a given file, this option will make it discard all "session cookies". This will basically have the same effect as if a new session is started. Typical browsers always discard session cookies when they’re closed down.
-k/--insecure
(SSL)
--keepalive-time <seconds>
keepalived逾時時間
--key <key>
(SSL/SSH)
--key-type <type>
-K/--config <config file>
指定配置檔案。可以講curl的參數寫進檔案,每個選項及其參數一行,分隔符為:空格、冒号、等号(推薦用 等号);若參數中包含空格,則放到引号中;以 # 注釋一行;長選項在config檔案裡不需要加--;
--limit-rate <speed>
指定最大傳輸速度, 預設以bytes/second來衡量,Examples: 200K, 3m and 1G。該選項指定傳輸過程中的平均速率,允許出現峰值高過該設定值的情況。
-l/--list-only
(FTP)
--local-port <num>[-num]
指定本地連接配接server時端口範圍
-L/--location
(HTTP/HTTPS)如果遇到重定向的頁面,該選項會是curl重發連接配接至新的location。如果和-i/--include or -I/--head一起使用,所有請求過的server的響應頭資訊都會被顯示。可以通過--max-redirs選項限制跳轉的最多次數。
當使用認證(authentication)時,curl隻将user+password發送到第一台server(對比--location-trusted)
--location-trusted
和-L/--location相似,但是有認證時,會将user+password傳遞給所有被重定向到的server,這可能會不安全。
--max-filesize <bytes>
設定下載下傳檔案的最大值(in bytes),如果請求的頁面大于設定的值,傳輸不會開始并且curl會傳回錯誤碼63。
-m/--max-time <seconds>
設定整個curl指令執行的最長時間,防止任務一直被hung住(聯想下--connect-timeout)
-N/--no-buffer
禁止’輸出緩沖‘。使用--buffer選項強制使用’緩沖‘。
--no-keepalive
禁用keepalive,預設curl開始keepalived。使用--keepalive強制開啟。
--no-sessionid
--noproxy <no-proxy-list>
--ntlm (HTTP) 允許NTLM認證,NTLM認證是微軟開發的,用于IIS。
-o/--output <file>
将輸出寫入指定檔案而非标準輸出。如果使用了{}或[]請求多個頁面,可以使用 '#' 來自動生成對應的頁面檔案,例如:
curl http://{one,two}.site.com -o "file_#1.txt"
curl http://{site,host}.host[1-5].com -o "#1_#2"
-O/--remote-name
将輸出寫入到本地檔案,以下載下傳的遠端檔案名命名。
--remote-name-all
--pass <phrase> (SSL/SSH)
--post301
告訴curl當POST請求傳回301狀态碼時不将POST請求轉為GET請求(web浏覽器普遍會進行這種轉換)。該選項僅在使用 -L/--location 的前提下可用
--post302 同上
--proxy-anyauth
--proxy-basic
--proxy-digest
--proxy-negotiate(協商、談判)
--proxy-ntlm
--proxy1.0 <proxyhost[:port]>
-p/--proxytunnel
--pubkey <key>
-P/--ftp-port <address>
-q
-Q/--quote <command> (FTP/SFTP)
--random-file <file> (SSL)
-r/--range <range>
(HTTP/FTP/SFTP/FILE) 請求接收的位元組範圍(請求頁面的一部分)。對于HTTP/1.1, FTP or SFTP server or a local FILE,範圍可以用如下方式指定
0-499 第一個500位元組
500-999 第二個500位元組
-500 最後的500位元組
9500-
0-0,-1 指定第一和最後一位元組
500-700,600-799
100-199,500-599 指定兩個分開的100位元組
--raw
該選項禁用所有的’http内部解碼‘(internal HTTP decoding of content)或’傳輸過程編碼(transfer encodings)‘,以為改變的形式傳遞内容。
-R/--remote-time
When used, this will make libcurl attempt to figure out the timestamp of the remote file, and if that is available make the local file get that same timestamp.
--retry <num>
重試次數
--retry-delay <seconds>
重試間隔
--retry-max-time <seconds>
-s/--silent
安靜模式不顯示進度條和錯誤資訊
-S/--show-error
和-s一起用,隻輸出錯誤資訊
--tcp-nodelay
開啟核心的TCP_NODELAY。
-u/--user <user:password>
指定使用者名密碼
-U/--proxy-user <user:password>
指定代理需要的使用者名密碼
--url <URL>
指定url,該選項隻在config檔案裡需要
-v/--verbose
-V/--version
-w/--write-out <format>
定義當傳輸完成時将顯示到标準輸出的資訊(類似于定義日志格式)
例如:監控網頁的響應時間
監控狀态碼
curl -o /dev/null -s -w %{http_code} "http://www.kklinux.com"
-0/--http1.0
強制curl使用http1.0協定
本文轉自kai404 51CTO部落格,原文連結:http://blog.51cto.com/kaifly/1656626,如需轉載請自行聯系原作者