linux curl命令學習

用法:

    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

        http://site.{one,two,three}.com

        ftp://ftp.numericals.com/file[1-100].txt
        ftp://ftp.numericals.com/file[001-100].txt    (with leading zeros)
        ftp://ftp.letters.com/file[a-z].txt

        http://any.org/archive[1996-1999]/vol[1-4]/part{a,b,c}.html

    從curl 7.15.1版本開始,可以爲一個範圍指定‘步進’,可以取得每N個url匹配的資源,如下

         http://www.numericals.com/file[1-100:10].txt
         http://www.letters.com/file[a-z:2].txt

    對於一個curl命令請求多個文件的情況,curl嘗試‘重用’鏈接(re-use connections)。

PROGRESS METER(進度條):

    curl默認會將文件內容顯示到終端,可以通過linux重定向或者‘-o’選項重定向至文件,並顯示一個如下進度條

   

    wKiom1VpF-DRqHegAACpi5Dhz4o200.jpg

    如果希望出現更簡短的‘#’的進度條,可以通過‘-#’選項實現

    wKioL1VpGXXT42OAAABv3-zqK8Q892.jpg

OPTIONS(選項):

    通常,所有布爾選項通過--option開啓,--no-option 關閉

    -a/--append

        (FTP/SFTP) 在upload過程中使用該選項,告訴curl把內容附加(append)到目標文件最後,若目標不存在則創建

    -A/--user-agent <agent string>

        (HTTP)指定發送給server的User-Agent,若指定的字符串中含有空格,放到單引(single quote)內。該功能同樣可以通過‘-H/--header’選項設置。如果設置多次,最後一次的設置生效。

    效果如下

wKiom1VpILKgYQqJAACYdo3VSqs037.jpg

wKioL1VpIknS2kq1AAC8qx36MQI494.jpg





    --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 thecontent-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,命令如下

        curl -F password=@/etc/passwd www.mypasswords.com

        如果想從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>

        (SSL)

    -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

        (SSL)

    --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 "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" "http://www.kklinux.com"

    監控狀態碼

        curl -o /dev/null -s -w %{http_code} "http://www.kklinux.com"

    -0/--http1.0

        強制curl使用http1.0協議

   

  參考:http://www.cnblogs.com/wangkangluo1/archive/2012/04/17/2453975.html 

   

   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章