1. 程式人生 > >Linux curl 命令詳解

Linux curl 命令詳解

 

命令概要

該命令設計用於在沒有使用者互動的情況下工作。

curl 是一個工具,用於傳輸來自伺服器或者到伺服器的資料。「向伺服器傳輸資料或者獲取來自伺服器的資料」

可支援的協議有(DICT、FILE、FTP、FTPS、GOPHER、HTTP、HTTPS、IMAP、IMAPS、LDAP、LDAPS、POP3、POP3S、RTMP、RTSP、SCP、SFTP、SMTP、SMTPS、TELNET和TFTP)。

curl提供了大量有用的技巧,比如代理支援、使用者身份驗證、FTP上傳、HTTP post、SSL連線、cookie、檔案斷點續傳、Metalink等等。正如你將在下面看到的,這些特性的數量會讓您頭暈目眩!

 

訪問的URL

你可以在命令列上指定任意數量的url。它們將按指定的順序依次獲取。

你可以指定多個url,或url的部分通過在花括號內編寫部分集,如:

1 http://site.{one,two,three}.com
2 # 參見
3 curl http://www.zhangblog.com/2019/06/16/hexo{04,05,06}/ -I  # 檢視資訊

 

或者可以使用[]得到字母數字序列的序列,如:

1 ftp://ftp.numericals.com/file[1-100].txt
2 ftp://ftp.numericals.com/file[001-100].txt   # 前導用零
3 ftp://ftp.letters.com/file[a-z].txt 
4 # 參見
5 curl http://www.zhangblog.com/2019/06/16/hexo[04-06]/ -I     # 檢視資訊

 

不支援巢狀序列,但可以使用幾個相鄰的序列:

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

 

你可以指定一個步長計數器的範圍,以獲得每第n個數字或字母:

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

 

如果指定URL而沒有protocol:// prefix,預設為HTTP。

 

常用選項一

curl通常在操作過程中顯示一個進度表,顯示傳輸的資料量、傳輸速度和估計的剩餘時間等。

-#, --progress-bar

將curl進度顯示為一個簡單的進度條;而不是標準的、具有更多資訊的進度表。

1 [root@iZ28xbsfvc4Z 20190702]# curl -O http://www.zhangblog.com/2019/06/16/hexo04/index.html  # 預設的進度表
2   %  Total    %  Received  % Xferd  Average  Speed   Time    Time     Time  Current
3                                     Dload   Upload   Total   Spent    Left  Speed
4 100  97299  100  97299     0     0   186k       0 --:--:-- --:--:-- --:--:--  186k
5 [root@iZ28xbsfvc4Z 20190702]# 
6 [root@iZ28xbsfvc4Z 20190702]# curl -# -O http://www.zhangblog.com/2019/06/16/hexo04/index.html  #簡單的進度條
7 ######################################################################## 100.0%

 

-0, --http1.0

(HTTP)強制curl使用HTTP 1.0發出請求,而不是使用其內部首選的HTTP 1.1。

-1, --tlsv1

(SSL)強制curl使用TLS 1.x 版本,當與遠端TLS服務進行協商時。
可以使用選項 --tlsv1.0、--tlsv1.1和 --tlsv1.2來更精確地控制TLS版本(如果使用的SSL後端支援這種級別的控制)。

-2, --sslv2

(SSL)強制curl使用TLS 2 版本,當與遠端TLS服務進行協商時。

-3, --sslv3

(SSL)強制curl使用TLS 3 版本,當與遠端TLS服務進行協商時。

-4, --ipv4

如果curl能夠將一個地址解析為多個IP版本(比如它支援ipv4和ipv6),那麼這個選項告訴curl只將名稱解析為IPv4地址。

-6, --ipv6

如果curl能夠將一個地址解析為多個IP版本(比如它支援ipv4和ipv6),那麼這個選項告訴curl只將名稱解析為IPv6地址。

-a, --append

(FTP/SFTP)當在上傳中使用時,這將告訴curl追加到目標檔案而不是覆蓋它。如果檔案不存在,將建立它。注意,一些SSH伺服器(包括OpenSSH)會忽略此標誌。

-A, --user-agent <agent string>

(HTTP)指定要傳送到HTTP服務端的User-Agent字串。當然也可以使用 -H, --header 選項來設定。
用於模擬客戶端,如:谷歌瀏覽器、火狐瀏覽器、IE 瀏覽器等等。

如果多次使用此選項,則將使用最後一個選項。

模仿瀏覽器訪問

curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/75.0.3770.999" http://www.zhangblog.com/2019/06/24/domainexpire/

 

--basic

(HTTP)告訴curl使用HTTP基本身份驗證。這是預設的。

 

常用選項二

(HTTP)將資料作為cookie傳遞給HTTP伺服器。它應該是之前從服務端接收到的“Set-Cookie:”行中的資料。資料格式為“NAME1=VALUE1;NAME2 = VALUE2”。

如果行中沒有使用 ‘=’ 符號,則將其視為一個檔名,用於讀取先前儲存的cookie行,如果它們匹配,則應在此會話中使用。
要讀取cookie檔案的檔案格式應該是純HTTP標頭檔案或Netscape/Mozilla cookie檔案格式。

注意:使用 -b, --cookie 指定的檔案僅用作輸入。檔案中不會儲存cookies。要儲存cookies,可以使用 -c, --cookie-jar 選項,或者您甚至可以使用 -D, --dump-header 將HTTP頭儲存到檔案中。

(HTTP)指定希望curl在完成操作後將所有cookie寫入哪個檔案。
Curl寫之前從指定檔案讀取的所有cookie,以及從遠端服務端接收的所有cookie。
如果沒有已知的cookie,則不會寫入任何檔案。該檔案將使用Netscape cookie檔案格式編寫。如果你將檔名設定為單個破折號 “-” ,cookie將被標準輸出。

該命令列選項將啟用cookie引擎,使curl記錄並使用cookies。啟用它的另一種方法是使用 -b, --cookie 選項。

如果不能建立或寫入cookie jar,那麼整個curl操作就不會失敗,甚至不能清楚地報告錯誤。使用 -v 會得到一個警告,但這是你得到的關於這種可能致命的情況的唯一可見反饋。

如果多次使用此選項,將使用最後指定的檔名。

--connect-timeout <seconds>

連線服務端的超時時間。這隻限制了連線階段,一旦curl連線了此選項就不再使用了。

也可參見:-m, --max-time 選項。

1 # 當前 https://www.zhangXX.com 是國外伺服器,訪問受限
2 [root@iZ28xbsfvc4Z ~]# curl --connect-timeout 10 https://www.zhangXX.com | head
3   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
4                                  Dload  Upload   Total   Spent    Left  Speed
5   0     0    0     0    0     0      0      0 --:--:--  0:00:10 --:--:--     0
6 curl: (28) Connection timed out after 10001 milliseconds

 

--create-dirs

當與 -o 選項一起使用時,curl將根據需要建立必要的本地目錄層次結構。

這個選項只建立與 -o 選項相關的dirs,沒有其他內容。如果 -o 檔名沒有使用dir,或者其中提到的dir已經存在,則不會建立dir。

示例

curl -o ./hexo04/index.html --create-dirs http://www.zhangblog.com/2019/06/16/hexo04

 

-C, --continue-at <offset>

按給定偏移量繼續/恢復以前的檔案傳輸。給定的偏移量是將被跳過的確切位元組數,從原始檔的開頭開始計算,然後再將其傳輸到目標檔案。

使用 “-C -“「注意有空格和無空格的情況」,告訴curl自動找出在哪裡/如何恢復傳輸。然後,它使用給定的輸出/輸入檔案來解決這個問題。

1 # 下載一個 2G 的檔案,可以反覆測試,檢視結果
2 curl -C - -o tmp.data http://www.zhangblog.com/uploads/tmp/tmp.data

 

-d, --data <data>

使用該選項,那麼預設請求方式為 POST。
(HTTP)在POST請求中向HTTP伺服器傳送指定的資料,與瀏覽器在使用者填寫HTML表單並按下submit按鈕時所做的相同。這將導致curl使用content-type application/x-www-form-urlencoded將資料傳遞給伺服器。也可參見:-F,-form 。

如果這些命令在同一個命令列使用多次,這些資料片段將使用指定的分隔符 & 合併。
因此,使用 ‘-d name=daniel -d skill=lousy’ 將生成一個類似 ‘name=daniel&skill=lousy’ 的post塊,也可以直接這樣合併使用。

-d, --data 與 --data-ascii 相同。post資料為純粹的二進位制資料時,那麼使用 --data-binary 選項。要對錶單欄位的值進行url編碼,可以使用 --data-urlencode。

如果您以字母@開始資料,那麼其餘的應該是一個檔名,以便從其中讀取資料。或者 - 如果您希望curl從stdin【標準輸入】讀取資料。檔案的內容必須已經是url編碼的。還可以指定多個檔案。因此,Posting資料名為 “foobar” 的檔案將使用 --data @foobar 完成。

示例

請求資訊:

 1 [root@iZ28xbsfvc4Z 20190712]# curl -sv --local-port 9000 -d 'user=zhang&pwd=123456' http://www.zhangblog.com/2019/06/24/domainexpire/ | head -n1 
 2 * About to connect() to www.zhangblog.com port 80 (#0)
 3 *   Trying 120.27.48.179...
 4 * Local port: 9000
 5 * Connected to www.zhangblog.com (120.27.48.179) port 80 (#0)
 6 > POST /2019/06/24/domainexpire/ HTTP/1.1   # 可見請求方式為POST
 7 > User-Agent: curl/7.29.0
 8 > Host: www.zhangblog.com
 9 > Accept: */*
10 > Content-Length: 21
11 > Content-Type: application/x-www-form-urlencoded
12 > 
13 } [data not shown]
14 * upload completely sent off: 21 out of 21 bytes
15 < HTTP/1.1 405 Not Allowed
16 < Server: nginx/1.14.2
17 < Date: Fri, 12 Jul 2019 13:34:20 GMT
18 < Content-Type: text/html
19 < Content-Length: 173
20 < Connection: keep-alive
21 < 
22 { [data not shown]
23 * Connection #0 to host www.zhangblog.com left intact
24 <html>

 

抓包資訊

[root@iZ28xbsfvc4Z tcpdump]# tcpdump -i any port 9000 -A -s 0

 

--data-ascii <data>

參見 -d, --data

--data-binary <data>

(HTTP) POST資料完全按照指定的方式,沒有任何額外的處理。

如果您以字母@開始資料,其餘的應該是檔名。
資料是以類似於 --data-ascii 的方式釋出的,只不過保留了換行,而且永遠不會進行轉換【資料不轉換】。

如果多次使用此選項,第一個選項後面的選項將按照 -d, --data 中的描述追加資料。

--data-urlencode <data>

(HTTP)這個Post 資料,與另一個 --data 選項類似,除執行url編碼以外。

-D, --dump-header <file>

將響應協議頭寫入指定的檔案。

如果多次使用此選項,則將使用最後一個選項。

當你想要儲存HTTP站點發送給你的標頭檔案時,使用此選項非常方便。

1 [root@iZ28xbsfvc4Z 20190703]# curl -D baidu_header.info www.baidu.com 
2 ………………
3 [root@iZ28xbsfvc4Z 20190703]# ll
4 total 4
5 -rw-r--r-- 1 root root 400 Jul  3 10:11 baidu_header.info  # 生成的標頭檔案

之後第二次curl呼叫通過 -b, --cookie 選項,可以從頭部讀取 cookies 。然而 -c, --cookie-jar 選項是儲存 cookies 更好的方法。

常用選項三

--digest

(HTTP)啟用HTTP摘要身份驗證。這是一種身份驗證方案,可以防止密碼以明文通過網路傳送。將此選項與普通的 -u, --user 選項組合使用,以設定使用者名稱和密碼。
相關選項請參見 --ntlm, --negotiate 和 --anyauth。

如果多次使用此選項,則只使用第一個選項。

-e, --referer <URL>

(HTTP)將 “Referer Page” 【從哪個頁面跳轉過來的】資訊傳送到HTTP伺服器。當然也可以使用 -H, --header 標誌來設定。

如果多次使用此選項,則將使用最後一個選項。

curl -e 'https:www.baidu.com' http://www.zhangblog.com/2019/06/24/domainexpire/

-f, --fail

(HTTP)在伺服器錯誤上靜默失敗(完全沒有輸出)。這主要是為了使指令碼等更好地處理失敗的嘗試。

在通常情況下,當HTTP伺服器無法交付文件時,它會返回一個HTML文件,說明原因(通常還會描述原因)。此標誌將阻止curl輸出該值並返回錯誤22。

 1 [root@iZ28xbsfvc4Z 20190713]# curl http://www.zhangblog.com/201912312
 2 <html>
 3 <head><title>404 Not Found</title></head>
 4 <body bgcolor="white">
 5 <center><h1>404 Not Found</h1></center>
 6 <hr><center>nginx/1.14.2</center>
 7 </body>
 8 </html>
 9 [root@iZ28xbsfvc4Z 20190713]# curl -f http://www.zhangblog.com/201912312
10 curl: (22) The requested URL returned error: 404 Not Found

-F, --form <name=content>

(HTTP)這允許curl模擬使用者按下submit按鈕後填充的表單。

該情況讓curl 可使用Content-Type multipart/form-data POST資料。也可以上傳二進位制檔案等。

@檔名:使一個檔案作為檔案上傳附加在post中。
<檔名:從檔案中獲取該文字欄位的內容。

例如,要將密碼檔案傳送到伺服器,其中“password”是表單欄位的名稱,/etc/passwd將作為輸入:

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

 

您還可以使用 ‘type=’ 告訴curl使用什麼 Content-Type ,方法類似於:

curl -F "[email protected];type=text/html" url.com
或
curl -F "name=daniel;type=text/foo" url.com

 

可以通過設定 filename= 更改本地上傳的檔名,如下:

curl -F "file=@localfile;filename=nameinpost" url.com

上傳的檔名從改為了 nameinpost

如果檔名/路徑包括 ‘,’ 或 ‘;’ ,必須用雙引號括起來:

curl -F "file=@\"localfile\";filename=\"nameinpost\"" url.com
或
curl -F 'file=@"localfile";filename="nameinpost"' url.com

最外層可用單引號或雙引號。

這個選項可以多次使用。

 

請勿如下使用

curl -F 'user=zhang&password=pwd' url.com   # 這種用法是錯誤的

 

--form-string <name=string>

(HTTP)類似於 --form,只是命名引數的value字串按字面意思使用。
在值中以 ‘@’ 和 ‘<’ 開頭的字元,以及 ‘;type=’ 字串沒有特殊的含義。

如果字串值有可能意外觸發 --form 的 “@” 或 “<” 特性,請優先使用此選項。

-g, --globoff

這個選項關閉了“URL全域性解析器”。當您設定這個選項時,您可以指定包含字母 {}[] 的url,而不需要curl本身來解釋它們。

注意,這些字母不是正常的合法URL內容,但是它們應該按照URI標準進行編碼。

-G, --get

使用此選項時,將使所有使用 -d, --data 或 --data-binary 指定的資料在HTTP GET請求中使用,而不是在POST請求中使用。
資料將被追加到URL的一個 ‘?’ 的分隔符後。

如果與 -I 結合使用,POST資料將被替換追加到帶有HEAD請求的URL中。

如果多次使用此選項,則只使用第一個選項。

示例

 1 [root@iZ28xbsfvc4Z 20190712]# curl -sv -G --local-port 9000 -d 'user=zhang&pwd=123456' http://www.zhangblog.com/2019/06/24/domainexpire/ | head -n1 
 2 或則
 3 [root@iZ28xbsfvc4Z 20190713]# curl -sv --local-port 9000 "http://www.zhangblog.com/2019/06/24/domainexpire/?user=zhang&pwd=123456" | head -n1
 4 * About to connect() to www.zhangblog.com port 80 (#0)
 5 *   Trying 120.27.48.179...
 6 * Local port: 9000
 7 * Connected to www.zhangblog.com (120.27.48.179) port 80 (#0)
 8 > GET /2019/06/24/domainexpire/?user=zhang&pwd=123456 HTTP/1.1  # 可見請求方式為 GET,且引數追加到了URI後
 9 > User-Agent: curl/7.29.0
10 > Host: www.zhangblog.com
11 > Accept: */*
12 > 
13 < HTTP/1.1 200 OK
14 < Server: nginx/1.14.2
15 < Date: Fri, 12 Jul 2019 14:04:19 GMT
16 < Content-Type: text/html
17 < Content-Length: 51385
18 < Last-Modified: Tue, 09 Jul 2019 13:55:19 GMT
19 < Connection: keep-alive
20 < ETag: "5d249cc7-c8b9"
21 < Accept-Ranges: bytes
22 < 
23 { [data not shown]
24 * Connection #0 to host www.zhangblog.com left intact
25 <!DOCTYPE html>

 

抓包資訊

[root@iZ28xbsfvc4Z tcpdump]# tcpdump -i any port 9000 -A -s 0

 

-H, --header <header>

(HTTP) 要傳送到服務端的自定義請求頭。

此選項可多次用於新增/替換/刪除多個headers。

1 curl -H 'Connection: keep-alive' -H 'Referer: https://sina.com.cn' -H 'User-Agent: Mozilla/1.0' http://www.zhangblog.com/2019/06/24/domainexpire/

 

--ignore-content-length

(HTTP)忽略Content-Length 頭資訊。

-i, --include

(HTTP)在輸出的內容中包含HTTP 頭資訊。

curl -i https://www.baidu.com

 

-I, --head

(HTTP/FTP/FILE)只獲取HTTP標頭檔案。
在FTP或FILE 檔案上使用時,curl只顯示檔案大小和最後修改時間。

curl -I https://www.baidu.com

 

-k, --insecure

(SSL)允許curl執行不安全的SSL連線和傳輸。
所有SSL連線都嘗試使用預設安裝的CA證書包來確保安全。

示例

 1 [root@iZ28xbsfvc4Z ~]# curl https://140.205.16.113/  # 被拒絕
 2 curl: (51) Unable to communicate securely with peer: requested domain name does not match the server's certificate.
 3 [root@iZ28xbsfvc4Z ~]# 
 4 [root@iZ28xbsfvc4Z ~]# curl -k https://140.205.16.113/  # 允許執行不安全的證書連線
 5 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
 6 <html>
 7 <head><title>403 Forbidden</title></head>
 8 <body bgcolor="white">
 9 <h1>403 Forbidden</h1>
10 <p>You don't have permission to access the URL on this server.<hr/>Powered by Tengine</body>
11 </html>

 

常用選項四

--keepalive-time <seconds>

keepalive 時長。如果使用no-keepalive,則此選項無效。

如果多次使用此選項,則將使用最後一個選項。如果未指定,該選項預設為60秒。

--key <key>

(SSL/SSH)私鑰檔名。允許你在這個單獨的檔案中提供你的私鑰。

對於SSH,如果沒有指定,curl嘗試如下順序:’~/.ssh/id_rsa’,’~/.ssh/id_dsa’,’./id_rsa’,’./id_dsa’。

如果多次使用此選項,則將使用最後一個選項。

--key-type <type>

(SSL)私鑰檔案型別。指定 --key 提供的私鑰的型別。支援DER、PEM和ENG。如果沒有指定,則定為PEM。

如果多次使用此選項,則將使用最後一個選項。

-L, --location

(HTTP/HTTPS) 跟蹤重定向
如果伺服器報告請求頁面已移動到另一個位置(用location: header和3XX響應程式碼表示),此選項將使curl在新位置上重做請求。

如果與 -i, --include 或 -I, --head 一起使用,將顯示所有請求頁面的標題。

 1 [root@iZ28xbsfvc4Z ~]# curl -I -L https://baidu.com/ 
 2 HTTP/1.1 302 Moved Temporarily  # 302 重定向
 3 Server: bfe/1.0.8.18
 4 Date: Thu, 04 Jul 2019 03:07:15 GMT
 5 Content-Type: text/html
 6 Content-Length: 161
 7 Connection: keep-alive
 8 Location: http://www.baidu.com/
 9 
10 HTTP/1.1 200 OK
11 Accept-Ranges: bytes
12 Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
13 Connection: Keep-Alive
14 Content-Length: 277
15 Content-Type: text/html
16 Date: Thu, 04 Jul 2019 03:07:15 GMT
17 Etag: "575e1f60-115"
18 Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
19 Pragma: no-cache
20 Server: bfe/1.0.8.18

 

--limit-rate <speed>

指定要使用curl的最大傳輸速率。

如果有一個有限的管道,並且希望傳輸不要使用您的全部頻寬,那麼這個特性是非常有用的。

curl --limit-rate 500 http://www.baidu.com/
curl --limit-rate 2k http://www.baidu.com/

單位:預設位元組,除非新增字尾。附加 “k” 或 “K” 表示千位元組, “m” 或 “M” 表示兆位元組,而 “g” 或 “G” 表示千兆位元組。例如:200K, 3m和1G。

給定的速率是整個傳輸過程中計算的平均速度。這意味著curl可能在短時間內使用更高的傳輸速度,但是隨著時間的推移,它只使用給定的速率。

如果多次使用此選項,則將使用最後一個選項。

--local-port <num>[-num]

指定本地的一個埠或埠範圍去連線。

請注意,埠號本質上是一種稀缺資源,有時會很忙,因此將此範圍設定為太窄可能會導致不必要的連線失敗。

curl --local-port 9000 http://www.baidu.com/
curl --local-port 9000-9999 http://www.baidu.com/

 

-m, --max-time <seconds>

允許整個操作花費的最大時間(以秒為單位)。

這對於防止由於網路或連結變慢而導致批處理作業掛起數小時非常有用。

也可參見:--connect-timeout 選項

 1 [root@iZ28xbsfvc4Z ~]# curl -m 10 --limit-rate 5 http://www.baidu.com/ | head  # 超過10秒後,斷開連線
 2   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 3                                  Dload  Upload   Total   Spent    Left  Speed
 4   2  2381    2    50    0     0      4      0  0:09:55  0:00:10  0:09:45     4
 5 curl: (28) Operation timed out after 10103 milliseconds with 50 out of 2381 bytes received
 6 <!DOCTYPE html>
 7 <!--STATUS OK--><html> <head><met
 8 ### 或
 9 [root@iZ28xbsfvc4Z ~]# curl -m 10 https://www.zhangXX.com | head   # 超過10秒後,斷開連線
10   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
11                                  Dload  Upload   Total   Spent    Left  Speed
12   0     0    0     0    0     0      0      0 --:--:--  0:00:10 --:--:--     0
13 curl: (28) Connection timed out after 10001 milliseconds

 

--max-filesize <bytes>

指定要下載的檔案的最大大小(以位元組為單位)。
如果請求的檔案大於這個值,那麼傳輸將不會啟動,curl將返回退出程式碼63。

示例

 1 [root@iZ28xbsfvc4Z ~]# curl -I http://www.zhangblog.com/uploads/hexo/00.jpg # 正常
 2 HTTP/1.1 200 OK
 3 Server: nginx/1.14.2
 4 Date: Thu, 04 Jul 2019 07:24:24 GMT
 5 Content-Type: image/jpeg
 6 Content-Length: 18196
 7 Last-Modified: Mon, 24 Jun 2019 01:43:02 GMT
 8 Connection: keep-alive
 9 ETag: "5d102aa6-4714"
10 Accept-Ranges: bytes
11 
12 [root@iZ28xbsfvc4Z ~]# echo $?
13 0
14 [root@iZ28xbsfvc4Z ~]# 
15 [root@iZ28xbsfvc4Z ~]# 
16 [root@iZ28xbsfvc4Z ~]# curl --max-filesize 1000 -I http://www.zhangblog.com/uploads/hexo/00.jpg # 受限異常
17 HTTP/1.1 200 OK
18 Server: nginx/1.14.2
19 Date: Thu, 04 Jul 2019 07:24:54 GMT
20 Content-Type: image/jpeg
21 curl: (63) Maximum file size exceeded
22 [root@iZ28xbsfvc4Z ~]# 
23 [root@iZ28xbsfvc4Z ~]# echo $?
24 63

 

--max-redirs <num>

設定允許的最大重定向跟蹤數。

如果也使用了 -L, --location,則此選項可用於防止curl在悖論中無限重定向。
預設情況下,限制為50重定向。將此選項設定為-1,使其無限。

--no-keepalive

禁用在TCP連線上使用keepalive訊息,因為預設情況下curl啟用了它們。

注意,這是文件中已否定的選項名。因此,您可以使用 --keepalive 來強制keepalive。

常用選項五

-o, --output <file>

輸出到一個檔案,而不是標準輸出。

如果使用 {} 或 [] 來獲取多個documents。可以使用 ‘#’ 後跟說明符中的一個數字。該變數將替換為正在獲取URL的當前字串。就像:

curl http://{one,two}.site.com -o "file_#1.txt"
curl http://{site,host}.host[1-5].com -o "#1_#2"

 

示例1

 1 [root@iZ28xbsfvc4Z 20190703]# curl "http://www.zhangblog.com/2019/06/16/hexo{04,05,06}/" -o "file_#1.info"   # 注意curl 的地址需要用引號括起來
 2 或
 3 [root@iZ28xbsfvc4Z 20190703]# curl "http://www.zhangblog.com/2019/06/16/hexo[04-06]/" -o "file_#1.info"   # 注意curl 的地址需要用引號括起來
 4 [1/3]: http://www.zhangblog.com/2019/06/16/hexo04/ --> file_04.info
 5   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 6                                  Dload  Upload   Total   Spent    Left  Speed
 7 100 97299  100 97299    0     0  1551k      0 --:--:-- --:--:-- --:--:-- 1557k
 8 
 9 [2/3]: http://www.zhangblog.com/2019/06/16/hexo05/ --> file_05.info
10 100 54409  100 54409    0     0   172M      0 --:--:-- --:--:-- --:--:--  172M
11 
12 [3/3]: http://www.zhangblog.com/2019/06/16/hexo06/ --> file_06.info
13 100 56608  100 56608    0     0   230M      0 --:--:-- --:--:-- --:--:--  230M
14 [root@iZ28xbsfvc4Z 20190703]# 
15 [root@iZ28xbsfvc4Z 20190703]# ll
16 total 212
17 -rw-r--r-- 1 root root 97299 Jul  4 16:51 file_04.info
18 -rw-r--r-- 1 root root 54409 Jul  4 16:51 file_05.info
19 -rw-r--r-- 1 root root 56608 Jul  4 16:51 file_06.info

 

示例2

 1 [root@iZ28xbsfvc4Z 20190703]# curl "http://www.{baidu,douban}.com" -o "site_#1.txt"  # 注意curl 的地址需要用引號括起來
 2 [1/2]: http://www.baidu.com --> site_baidu.txt
 3   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 4                                  Dload  Upload   Total   Spent    Left  Speed
 5 100  2381  100  2381    0     0  46045      0 --:--:-- --:--:-- --:--:-- 46686
 6 
 7 [2/2]: http://www.douban.com --> site_douban.txt
 8 100   162  100   162    0     0   3173      0 --:--:-- --:--:-- --:--:--  3173
 9 [root@iZ28xbsfvc4Z 20190703]# 
10 [root@iZ28xbsfvc4Z 20190703]# ll
11 total 220
12 -rw-r--r-- 1 root root  2381 Jul  4 16:53 site_baidu.txt
13 -rw-r--r-- 1 root root   162 Jul  4 16:53 site_douban.txt

 

-O, --remote-name

寫入到本地檔案,名稱與遠端檔案的名稱相同。(只使用遠端檔案的檔案部分,路徑被切斷。)

用於儲存的遠端檔名是從給定的URL中提取的,沒有其他內容。
因此,檔案將儲存在當前工作目錄中。如果希望將檔案儲存在另一個目錄中,請確保在curl呼叫 -O, --remote-name之前更改當前工作目錄!

1 [root@iZ28xbsfvc4Z 20190712]# curl -O https://www.baidu.com   # 使用了 -O 選項,必須指定到具體的檔案  錯誤使用
2 curl: Remote file name has no length!
3 curl: try 'curl --help' or 'curl --manual' for more information
4 [root@iZ28xbsfvc4Z 20190712]# curl -O https://www.baidu.com/index.html   # 使用了 -O 選項,必須指定到具體的檔案  正確使用
5   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
6                                  Dload  Upload   Total   Spent    Left  Speed
7 100  2443  100  2443    0     0  13289      0 --:--:-- --:--:-- --:--:-- 13349

 

--pass <phrase>

(SSL/SSH)私鑰密碼

如果多次使用此選項,則將使用最後一個選項。

--post301

告訴curl當301重定向時,不要將POST請求轉換為GET請求。

非rfc行為在web瀏覽器中無處不在,因此curl在預設情況下進行轉換以保持一致性。但是,伺服器可能需要在重定向之後將POST保留為POST。

這個選項只有在使用 -L, --location 時才有意義

--post302

告訴curl當302重定向時,不要將POST請求轉換為GET請求。

非rfc行為在web瀏覽器中無處不在,因此curl在預設情況下進行轉換以保持一致性。但是,伺服器可能需要在重定向之後將POST保留為POST。

這個選項只有在使用 -L, --location 時才有意義

--post303

告訴curl當303重定向時,不要將POST請求轉換為GET請求。

非rfc行為在web瀏覽器中無處不在,因此curl在預設情況下進行轉換以保持一致性。但是,伺服器可能需要在重定向之後將POST保留為POST。

這個選項只有在使用 -L, --location 時才有意義

說明:
上述三個選項都是為了防止在重定向過程中,原來的 POST 請求,變為 GET請求。為了防止該情況,有兩種處理方式。
1、使用上述選項可避免;
2、使用 -X POST 選項和命令。

示例

[root@iZ28xbsfvc4Z ~]# curl -Lsv -d 'user=zhang' https://baidu.com | head -n1

 

開始是POST請求,302 重定向後變為了 GET請求。

 

[root@iZ28xbsfvc4Z ~]# curl -Lsv -d 'user=zhang' --post301 --post302 --post303 https://baidu.com | head -n1

前後都是 POST 請求。但是選項較多。

 

[root@iZ28xbsfvc4Z ~]# curl -Lsv -d 'user=zhang' -X POST https://baidu.com | head -n1

前後都是 POST 請求。推薦使用此命令。

 

--pubkey <key>

(SSH)公鑰檔名。允許在這個單獨的檔案中提供公鑰。

如果多次使用此選項,則將使用最後一個選項。

-r, --range <range>

(HTTP/FTP/SFTP/FILE)從HTTP/1.1、FTP或SFTP伺服器或本地檔案檢索位元組範圍。範圍可以通過多種方式指定。用於分段下載。

有時檔案比較大,或者難以迅速傳輸,而利用分段傳輸,可以實現穩定、高效並且有保障的傳輸,更具有實用性,同時容易對差錯檔案進行更正。

0-499:指定前500個位元組
500-999:指定第二個500位元組
-500:指定最後500個位元組
9500-:指定9500位元組及之後的位元組
0-0,-1:指定第一個和最後一個位元組
500-700,600-799:從偏移量500開始指定300位元組
100-199,500-599:指定兩個單獨100位元組的範圍

分段下載

 1 [root@iZ28xbsfvc4Z 20190715]# curl -I http://www.zhangblog.com/uploads/hexo/00.jpg   # 檢視檔案大小
 2 HTTP/1.1 200 OK
 3 Server: nginx/1.14.2
 4 Date: Mon, 15 Jul 2019 03:23:44 GMT
 5 Content-Type: image/jpeg
 6 Content-Length: 18196   # 檔案大小
 7 Last-Modified: Fri, 05 Jul 2019 08:04:58 GMT
 8 Connection: keep-alive
 9 ETag: "5d1f04aa-4714"
10 Accept-Ranges: bytes
11 [root@iZ28xbsfvc4Z 20190715]# curl -r 0-499   -o 00-jpg.part1 http://www.zhangblog.com/uploads/hexo/00.jpg
12 [root@iZ28xbsfvc4Z 20190715]# curl -r 500-999 -o 00-jpg.part2 http://www.zhangblog.com/uploads/hexo/00.jpg
13 [root@iZ28xbsfvc4Z 20190715]# curl -r 1000-   -o 00-jpg.part3 http://www.zhangblog.com/uploads/hexo/00.jpg

 

檢視下載檔案

1 [root@iZ28xbsfvc4Z 20190715]# ll
2 total 36
3 -rw-r--r-- 1 root root   500 Jul 15 11:25 00-jpg.part1
4 -rw-r--r-- 1 root root   500 Jul 15 11:25 00-jpg.part2
5 -rw-r--r-- 1 root root 17196 Jul 15 11:26 00-jpg.part3

 

檔案合併

1 [root@iZ28xbsfvc4Z 20190715]# cat 00-jpg.part1 00-jpg.part2 00-jpg.part3 > 00.jpg
2 [root@iZ28xbsfvc4Z 20190715]# ll
3 total 56
4 -rw-r--r-- 1 root root 18196 Jul 15 11:29 00.jpg

 

-R, --remote-time

使curl嘗試獲取遠端檔案的時間戳,如果可用,則使本地檔案獲得相同的時間戳【針對修改時間戳Modify】。

curl -o nfs1.info -R http://www.zhangblog.com/2019/07/05/nfs1/

 

--retry <num>

傳輸出現問題時,重試的次數。數字設定為0將使curl不重試(這是預設值)。

出現的瞬時錯誤如:timeout、FTP 4xx響應狀程式碼或HTTP 5xx響應狀程式碼。

當curl準備重試傳輸時,它將首先等待一秒鐘,之後對於所有即將到來的重試,它將把等待時間延長一倍,直到達到10分鐘,這將是其餘重試之間的延遲。

--retry-delay <seconds>

傳輸出現問題時,設定重試間隔時間。將此延遲設定為零將使curl使用預設的延遲時間。

--retry-max-time <seconds>

傳輸出現問題時,設定最大重試時間。將此選項設定為0則不超時重試。

 

常用選項六

-s, --silent

靜默或靜音模式。不顯示進度表/條或錯誤訊息。

示例

1 [root@iZ28xbsfvc4Z 20190713]# curl https://www.baidu.com | head -n1  # 預設有進度表
2   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
3                                  Dload  Upload   Total   Spent    Left  Speed
4 100  2443  100  2443    0     0  13346      0 --:--:-- --:--:-- --:--:-- 13349
5 <!DOCTYPE html>
6 [root@iZ28xbsfvc4Z 20190713]# curl -s https://www.baidu.com | head -n1
7 <!DOCTYPE html>

 

-S, --show-error

當與 -s 一起使用時,如果curl失敗,curl將顯示一條錯誤訊息。

1 [root@iZ28xbsfvc4Z 20190713]# curl -s https://140.205.16.113/ 
2 [root@iZ28xbsfvc4Z 20190713]# 
3 [root@iZ28xbsfvc4Z 20190713]# curl -sS https://140.205.16.113/ 
4 curl: (51) Unable to communicate securely with peer: requested domain name does not match the server's certificate.

 

--stderr <file>

將錯誤資訊重定向到一個檔案。如果檔名是普通的 ‘-‘,則將其寫入stdout。

如果多次使用此選項,則將使用最後一個選項。

1 [root@iZ28xbsfvc4Z 20190713]# curl --stderr err.info https://140.205.16.113/ 
2 [root@iZ28xbsfvc4Z 20190713]# ll
3 total 92
4 -rw-r--r-- 1 root root   116 Jul 13 10:19 err.info
5 [root@iZ28xbsfvc4Z 20190713]# cat err.info 
6 curl: (51) Unable to communicate securely with peer: requested domain name does not match the server's certificate.

 

-T, --upload-file <file>

這將指定的本地檔案傳輸到遠端URL。如果指定的URL中沒有檔案部分,Curl將附加本地檔名。

注意:必須在最後一個目錄上使用尾隨 / 來真正證明Curl沒有檔名,否則Curl會認為您的最後一個目錄名是要使用的遠端檔名。這很可能導致上傳操作失敗。
如果在HTTP(S)伺服器上使用此命令,則將使用PUT命令。

同時也支援多個檔案上傳,如下:

curl -T "{file1,file2}" http://www.uploadtothissite.com
或則
curl -T "img[1-1000].png" ftp://ftp.picturemania.com/upload/

 

--trace <file>

對指定檔案進行debug。包括所有傳入和傳出資料。

此選項會覆蓋之前使用的 -v、--verbose或 --trace-ascii。

如果多次使用此選項,則將使用最後一個選項。

curl --trace trace.info https://www.baidu.com

 

--trace-ascii <file>

對指定檔案進行debug。包括所有傳入和傳出資料。

這非常類似於 --trace,但是省略了十六進位制部分,只顯示轉儲的ASCII部分。使它輸出更小,對於我們來說可能更容易閱讀。

此選項會覆蓋之前使用的 -v、--verbose或 --trace。

如果多次使用此選項,則將使用最後一個選項。

curl --trace-ascii trace2.info https://www.baidu.com

 

--trace-time

為curl顯示的每個跟蹤或冗長的行新增時間戳。

curl --trace-ascii trace3.info --trace-time https://www.baidu.com

 

-v, --verbose

顯示詳細操作資訊。主要用於除錯。

以 > 開頭的行表示curl傳送的”header data”;< 表示curl接收到的通常情況下隱藏的”header data”;而以 * 開頭的行表示curl提供的附加資訊。

 1 [root@iZ28xbsfvc4Z 20190712]# curl -v https://www.baidu.com
 2 * About to connect() to www.baidu.com port 443 (#0)
 3 *   Trying 180.101.49.12...
 4 * Connected to www.baidu.com (180.101.49.12) port 443 (#0)
 5 * Initializing NSS with certpath: sql:/etc/pki/nssdb
 6 *   CAfile: /etc/pki/tls/certs/ca-bundle.crt
 7   CApath: none
 8 * SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
 9 * Server certificate:
10 *     subject: CN=baidu.com,O="Beijing Baidu Netcom Science Technology Co., Ltd",OU=service operation department,L=beijing,ST=beijing,C=CN
11 *     start date: May 09 01:22:02 2019 GMT
12 *     expire date: Jun 25 05:31:02 2020 GMT
13 *     common name: baidu.com
14 *     issuer: CN=GlobalSign Organization Validation CA - SHA256 - G2,O=GlobalSign nv-sa,C=BE
15 > GET / HTTP/1.1
16 > User-Agent: curl/7.29.0
17 > Host: www.baidu.com
18 > Accept: */*
19 > 
20 < HTTP/1.1 200 OK
21 < Accept-Ranges: bytes
22 < Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
23 < Connection: Keep-Alive
24 < Content-Length: 2443
25 < Content-Type: text/html
26 < Date: Fri, 12 Jul 2019 08:26:23 GMT
27 < Etag: "588603eb-98b"
28 < Last-Modified: Mon, 23 Jan 2017 13:23:55 GMT
29 < Pragma: no-cache
30 < Server: bfe/1.0.8.18
31 < Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
32 < 
33 <!DOCTYPE html>
34 ………………  # curl 網頁的具體資訊

 

-w, --write-out <format>

在完成和成功操作後要在stdout上顯示什麼。

支援如下變數,具體含義請自行參見curl文件。

content_type
filename_effective
ftp_entry_path
http_code
http_connect
local_ip
local_port
num_connects
num_redirects
redirect_url
remote_ip
remote_port
size_download
size_header
size_request
size_upload
speed_download
speed_upload
ssl_verify_result
time_appconnect
time_connect
time_namelookup
time_pretransfer
time_redirect
time_starttransfer
time_total
url_effective

  

 示例

1 [root@iZ28xbsfvc4Z 20190713]# curl -o /dev/null -s -w %{content_type} www.baidu.com  # 輸出結果沒有換行
2 text/html[root@iZ28xbsfvc4Z 20190713]# 
3 [root@iZ28xbsfvc4Z 20190713]# curl -o /dev/null -s -w %{http_code} www.baidu.com  # 輸出結果沒有換行
4 200[root@iZ28xbsfvc4Z 20190713]# 
5 [root@iZ28xbsfvc4Z 20190713]# curl -o /dev/null -s -w %{local_port} www.baidu.com  # 輸出結果沒有換行
6 37346[root@iZ28xbsfvc4Z 20190713]# 
7 [root@iZ28xbsfvc4Z 20190713]#

 

-x, --proxy <[protocol://][user:password@]proxyhost[:port]>

使用指定的HTTP代理。如果沒有指定埠號,則假定它位於埠1080。

-X, --request <command>

(HTTP)指定與HTTP伺服器通訊時的請求方式。預設GET

curl -vs -X POST https://www.baidu.com | head -n1

 

curl -vs -X PUT https://www.baidu.com | head -n1

 

推薦閱讀

Linux curl 命令詳解

Linux curl 常用示例

Linux curl 表單登入或提交與cookie使用

 


 

如果覺得不錯就點個讚唄 (-^O^-) !

———END———-

&n