歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> linux curl命令詳解

linux curl命令詳解

日期:2017/3/3 13:03:20   编辑:Linux技術
curl是一個利用URL規則在命令行下工作的文件傳輸工具。它支持文件的上傳和下載,所以是綜合傳輸工具,但按傳統,習慣稱url為下載工具。

-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/--help 幫助
 -H/--header <line>自定義頭信息傳遞給服務器
 --ignore-content-length  忽略的HTTP頭信息的長度
 -i/--include 輸出時包括protocol頭信息
 -I/--head  只顯示文檔信息
 從文件中讀取-j/--junk-session-cookies忽略會話Cookie
 - 界面<interface>指定網絡接口/地址使用
 - krb4 <級別>啟用與指定的安全級別krb4
 -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/--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]>設置代理用戶名和密碼
 -v/--verbose
 -V/--version 顯示版本信息
 -w/--write-out [format]什麼輸出完成後
 -x/--proxy <host[:port]>在給定的端口上使用HTTP代理
 -X/--request <command>指定什麼命令
 -y/--speed-time 放棄限速所要的時間。默認為30
 -Y/--speed-limit 停止傳輸速度的限制,速度時間'秒
 -z/--time-cond  傳送時間設置
 -0/--http1.0  使用HTTP 1.0
 -1/--tlsv1  使用TLSv1(SSL)
 -2/--sslv2 使用SSLv2的(SSL)
 -3/--sslv3         使用的SSLv3(SSL)
 --3p-quote      like -Q for the source URL for 3rd party transfer
 --3p-url        使用url,進行第三方傳送
 --3p-user       使用用戶名和密碼,進行第三方傳送
 -4/--ipv4   使用IP4
 -6/--ipv6   使用IP6
 -#/--progress-bar 用進度條顯示當前的傳送狀態

最後更新 2013-05-04 17:44:08
合作編輯者如下:張映
有誤,我來改正
(0)
正確
(8)
收藏
(0)
2
例1
a,抓取頁面內容到一個文件中
[root@krlcgcms01 mytest]# curl -o home.html http://blog.51yip.com
b,用-O(大寫的),後面的url要具體到某個文件,不然抓不下來。我們還可以用正則來抓取東西
[root@krlcgcms01 mytest]# curl -Ohttp://blog.51yip.com/wp-content/uploads/2010/09/compare_varnish.jpg  
[root@krlcgcms01 mytest]# curl -Ohttp://blog.51yip.com/wp-content/uploads/2010/[0-9][0-9]/aaaaa.jpg
c,模擬表單信息,模擬登錄,保存cookie信息
[root@krlcgcms01 mytest]# curl -c ./cookie_c.txt -F log=aaaa -F pwd=******http://blog.51yip.com/wp-login.php
d,模擬表單信息,模擬登錄,保存頭信息
[root@krlcgcms01 mytest]# curl -D ./cookie_D.txt -F log=aaaa -F pwd=******http://blog.51yip.com/wp-login.php
e,使用cookie文件
[root@krlcgcms01 mytest]# curl -b ./cookie_c.txt http://blog.51yip.com/wp-admin
f,斷點續傳,-C(大寫的)
[root@krlcgcms01 mytest]# curl -C -Ohttp://blog.51yip.com/wp-content/uploads/2010/09/compare_varnish.jpg
-----------------------g,傳送數據,最好用登錄頁面測試,因為你傳值過去後,curl回抓數據,你可以看到你傳值有沒有成功
[root@krlcgcms01 mytest]# curl -d log=aaaa http://blog.51yip.com/wp-login.php
h,顯示抓取錯誤,下面這個例子,很清楚的表明了。
[root@krlcgcms01 mytest]# curl -fhttp://blog.51yip.com/asdf  
curl: (22) The requested URL returned error: 404  
[root@krlcgcms01 mytest]# curlhttp://blog.51yip.com/asdf
i,偽造來源地址,有的網站會判斷,請求來源地址。
[root@krlcgcms01 mytest]# curl -ehttp://localhost http://blog.51yip.com/wp-login.php
j,當我們經常用curl去搞人家東西的時候,人家會把你的IP給屏蔽掉的,這個時候,我們可以用代理
[root@krlcgcms01 mytest]# curl -x 24.10.28.84:32779 -o home.htmlhttp://blog.51yip.com
k,比較大的東西,我們可以分段下載
[root@krlcgcms01 mytest]# curl -r 0-100 -o img.part1http://blog.51yip.com/wp- 
content/uploads/2010/09/compare_varnish.jpg
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 Dload  Upload   Total   Spent    Left  Speed
100   101  100   101    0     0    105      0 --:--:-- --:--:-- --:--:--     0
[root@krlcgcms01 mytest]# curl -r 100-200 -o img.part2http://blog.51yip.com/wp- 
content/uploads/2010/09/compare_varnish.jpg
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 Dload  Upload   Total   Spent    Left  Speed
100   101  100   101    0     0     57      0  0:00:01  0:00:01 --:--:--     0
[root@krlcgcms01 mytest]# curl -r 200- -o img.part3http://blog.51yip.com/wp- 
content/uploads/2010/09/compare_varnish.jpg
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 Dload  Upload   Total   Spent    Left  Speed
100  104k  100  104k    0     0  52793      0  0:00:02  0:00:02 --:--:-- 88961
[root@krlcgcms01 mytest]# ls |grep part | xargs du -sh
4.0K    one.part1
112K    three.part3
4.0K    two.part2
l,不會顯示下載進度信息
[root@krlcgcms01 mytest]# curl -s -o aaa.jpg http://blog.51yip.com/wp-content/uploads/2010/09/compare_varnish.jpg
m,顯示下載進度條
[root@krlcgcms01 mytest]# curl -# -O http://blog.51yip.com/wp-content/uploads/2010/09/compare_varnish.jpg  
    ######################################################################## 100.0%
n,通過ftp下載文件
[zhangy@BlackGhost ~]$ curl -u 用戶名:密碼 -Ohttp://blog.51yip.com/demo/curtain/bbstudy_files/style.css  
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current  
 Dload  Upload   Total   Spent    Left  Speed  
101  1934  101  1934    0     0   3184      0 --:--:-- --:--:-- --:--:--  7136
o,通過ftp上傳
[zhangy@BlackGhost ~]$ curl -T test.sqlftp://用戶名:密碼@ip:port/demo/curtain/bbstudy_files/

下邊對-w參數做個詳細的解釋,由我(DigDeeply)翻譯。有不對的地方請大家指出。(英文原文:http://curl.haxx.se/docs/manpage.html)
以下是可用的變量名:
 -w, --write-out 
   以下變量會按CURL認為合適的格式輸出,輸出變量需要按照%{variable_name}的格式,如果需要輸出%,double一下即可,即%%,同時,n是換行,r是回車,t是TAB。

url_effective The URL that was fetched last. This is most meaningful if you've told curl to follow location: headers.

filename_effective The ultimate filename that curl writes out to. This is only meaningful if curl is told to write to a file with the --remote-name or --output option. It's most useful in combination with the --remote-header-name option. (Added in 7.25.1)

http_code http狀態碼,如200成功,301轉向,404未找到,500服務器錯誤等。(The numerical response code that was found in the last retrieved HTTP(S) or FTP(s) transfer. In 7.18.2 the alias response_code was added to show the same info.)

http_connect The numerical code that was found in the last response (from a proxy) to a curl CONNECT request. (Added in 7.12.4)

time_total 總時間,按秒計。精確到小數點後三位。 (The total time, in seconds, that the full operation lasted. The time will be displayed with millisecond resolution.)

time_namelookup DNS解析時間,從請求開始到DNS解析完畢所用時間。(The time, in seconds, it took from the start until the name resolving was completed.)

time_connect 連接時間,從開始到建立TCP連接完成所用時間,包括前邊DNS解析時間,如果需要單純的得到連接時間,用這個time_connect時間減去前邊time_namelookup時間。以下同理,不再贅述。(The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.)

time_appconnect 連接建立完成時間,如SSL/SSH等建立連接或者完成三次握手時間。(The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed. (Added in 7.19.0))

time_pretransfer 從開始到准備傳輸的時間。(The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.)

time_redirect 重定向時間,包括到最後一次傳輸前的幾次重定向的DNS解析,連接,預傳輸,傳輸時間。(The time, in seconds, it took for all redirection steps include name lookup, connect, pretransfer and transfer before the final transaction was started. time_redirect shows the complete execution time for multiple redirections. (Added in 7.12.3))

time_starttransfer 開始傳輸時間。在發出請求之後,Web 服務器返回數據的第一個字節所用的時間(The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.)

size_download 下載大小。(The total amount of bytes that were downloaded.)

size_upload 上傳大小。(The total amount of bytes that were uploaded.)

size_header  下載的header的大小(The total amount of bytes of the downloaded headers.)

size_request 請求的大小。(The total amount of bytes that were sent in the HTTP request.)

speed_download 下載速度,單位-字節每秒。(The average download speed that curl measured for the complete download. Bytes per second.)

speed_upload 上傳速度,單位-字節每秒。(The average upload speed that curl measured for the complete upload. Bytes per second.)

content_type 就是content-Type,不用多說了,這是一個訪問我博客首頁返回的結果示例(text/html; charset=UTF-8);(The Content-Type of the requested document, if there was any.)

num_connects Number of new connects made in the recent transfer. (Added in 7.12.3)

num_redirects Number of redirects that were followed in the request. (Added in 7.12.3)

redirect_url When a HTTP request was made without -L to follow redirects, this variable will show the actual URL a redirect would take you to. (Added in 7.18.2)

ftp_entry_path The initial path libcurl ended up in when logging on to the remote FTP server. (Added in 7.15.4)

ssl_verify_result ssl認證結果,返回0表示認證成功。( The result of the SSL peer certificate verification that was requested. 0 means the verification was successful. (Added in 7.19.0))

若多次使用-w參數,按最後一個的格式輸出。If this option is used several times, the last one will be used.
Copyright © Linux教程網 All Rights Reserved