1. 程式人生 > >Linux curl 常用示例

Linux curl 常用示例

 

本篇文章包含了curl的常用案例使用。

如果想了解curl選項的詳細說明,請參考前一篇文章「Linux curl 命令詳解」。

常見網頁訪問示例

基本用法

訪問一個網頁

1 curl https://www.baidu.com

執行後,相關的網頁資訊會打印出來

 

進度條展示

有時候我們不需要進度表展示,而需要進度條展示。比如:下載檔案時。

可以通過 -#, --progress-bar 選項實現。

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  11662      0 --:--:-- --:--:-- --:--:-- 11688
5 <!DOCTYPE html>
6 [root@iZ28xbsfvc4Z 20190713]# curl -# https://www.baidu.com | head -n1  # 進度條顯示
7 ######################################################################## 100.0%
8 <!DOCTYPE html>

 

靜默模式與錯誤資訊列印

當我們做一些操作時,可能會出現進度表。這時我們可以使用 -s, --silent 靜默模式去掉這些不必要的資訊。

如果使用 -s, --silent 時,還需要列印錯誤資訊,那麼還需要使用 -S, --show-error 選項。

靜默模式示例

1 [root@iZ28xbsfvc4Z ~]# 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  11874      0 --:--:-- --:--:-- --:--:-- 11859
5 <!DOCTYPE html>
6 [root@iZ28xbsfvc4Z ~]# curl -s https://www.baidu.com | head -n1
7 <!DOCTYPE html>

 

靜默模式結合錯誤資訊列印

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.

 

顯示詳細操作資訊

使用 -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 網頁的具體資訊

 

指定訪問的請求方法

當然curl預設使用GET方式訪問。使用了 -d, --data <data> 選項,那麼會預設為 POST方法訪問。如果此時還想實現 GET 訪問,那麼可以使用 -G, --get 選項強制curl 使用GET方法訪問。

同時 -X, --request <command> 選項也可以指定訪問方法。

POST請求和資料傳輸

為了抓包檢視資訊所以使用了 --local-port <num>[-num] 選項,在實際應用中不需要該選項。

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

 

抓包資訊

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

 

指定請求方法

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

 

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

 

儲存訪問網頁

使用linux的重定向功能儲存

1 curl www.baidu.com >> baidu.html

 

使用curl的大O選項

通過 -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

 

使用curl的小o選項

通過 -o, --output <file> 選項實現。

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

 

允許不安全訪問

當我們使用curl進行https訪問訪問時,如果SSL證書是我們自簽發的證書,那麼這個時候需要使用 -k, --insecure 選項,允許不安全的訪問。

 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>

 

獲取HTTP響應狀態碼

在指令碼中,這是很常見的測試網站是否正常的用法。

通過 -w, --write-out <format> 選項實現。

1 [root@iZ28xbsfvc4Z 20190713]# curl -o /dev/null -s -w %{http_code} https://baidu.com
2 302[root@iZ28xbsfvc4Z 20190713]# 
3 [root@iZ28xbsfvc4Z 20190713]# 
4 [root@iZ28xbsfvc4Z 20190713]# curl -o /dev/null -s -w %{http_code} https://www.baidu.com
5 200[root@iZ28xbsfvc4Z 20190713]#

 

指定proxy伺服器以及其埠

很多時候上網需要用到代理伺服器(比如是使用代理伺服器上網或者因為使用curl別人網站而被別人遮蔽IP地址的時候),幸運的是curl通過使用 -x, --proxy <[protocol://][user:password@]proxyhost[:port]> 選項來支援設定代理。

1 curl -x 192.168.100.100:1080 https://www.baidu.com

 

模仿瀏覽器訪問

有些網站需要使用特定的瀏覽器去訪問他們,有些還需要使用某些特定的瀏覽器版本。我們可以通過 -A, --user-agent <agent string> 或者 -H, --header <header> 選項實現模擬瀏覽器訪問。

1 curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/75.0.3770.999" http://www.zhangblog.com/2019/06/24/domainexpire/ 
2 或者
3 curl -H 'User-Agent: Mozilla/5.0' http://www.zhangblog.com/2019/06/24/domainexpire/

 

偽造referer(盜鏈)

有些網站的網頁對http訪問的連結來源做了訪問限制,這些限制幾乎都是通過referer來實現的。

比如:要求是先訪問首頁,然後再訪問首頁中的郵箱頁面,這時訪問郵箱的referer地址就是訪問首頁成功後的頁面地址。如果伺服器發現對郵箱頁面訪問的referer地址不是首頁的地址,就斷定那是個盜連了。

可以通過 -e, --referer 或則 -H, --header <header> 實現偽造 referer 。

1 curl -e 'https://www.baidu.com' http://www.zhangblog.com/2019/06/24/domainexpire/
2 或者
3 curl -H 'Referer: https://www.baidu.com' http://www.zhangblog.com/2019/06/24/domainexpire/

 

構造HTTP請求頭

可以通過 -H, --header <header> 實現構造http請求頭。

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/

 

儲存響應頭資訊

可以通過 -D, --dump-header <file> 選項實現。

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  # 生成的標頭檔案

 

限時訪問

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

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

 

-m, --max-time <seconds> 允許整個操作花費的最大時間(以秒為單位)。這對於防止由於網路或連結變慢而導致批處理作業掛起數小時非常有用。

 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

 

顯示抓取錯誤

當我們請求訪問失敗時或者沒有該網頁時,網站一般都會給出一個錯誤的提示頁面。

如果我們不需要這個錯誤頁面,只想得到簡潔的錯誤資訊。那麼可以通過 -f, --fail 選項實現。

 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

 

表單登入與cookie使用

參見:「Linux curl 表單登入或提交與cookie使用」

 

檔案上傳與下載

涉及 FTP 服務,簡單快速搭建可參考:《CentOS7下安裝FTP服務》「https://www.cnblogs.com/zhi-leaf/p/5983550.html」

檔案下載

網頁檔案下載

1 # 以進度條展示,而不是進度表展示
2 [root@iZ28xbsfvc4Z 20190715]# curl -# -o tmp.data2 http://www.zhangblog.com/uploads/tmp/tmp.data
3 ######################################################################## 100.0%

 

FTP檔案下載

說明1:其中 ftp1 使用者是ftp服務端的賬號,具體家目錄是:/mnt/ftp1

說明2:當我們使用 curl 通過 FTP 進行下載時,後面跟的路徑都是:當前使用的 ftp 賬號家目錄為基礎的相對路徑,然後找到的目標檔案。

示例1

1 # 其中 tmp.data 的絕對路徑是:/mnt/ftp1/tmpdata/tmp.data ;ftp1 賬號的家目錄是:/mnt/ftp1
2 # 說明:/tmpdata/tmp.data 這個路徑是針對 ftp1 賬號的家目錄而言的
3 [yun@nginx_proxy01 20190715]$ curl -O ftp://ftp1:[email protected]:21/tmpdata/tmp.data  
4 # 或者
5 [yun@nginx_proxy01 20190715]$ curl -O -u ftp1:123456 ftp://172.16.1.195:21/tmpdata/tmp.data
6   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
7                                  Dload  Upload   Total   Spent    Left  Speed
8 100 2048M  100 2048M    0     0  39.5M      0  0:00:51  0:00:51 --:--:--  143M

 

示例2

1 # 其中 nginx-1.14.2.tar.gz 的絕對路徑是:/tmp/nginx-1.14.2.tar.gz ;ftp1 賬號的家目錄是:/mnt/ftp1
2 # 說明:/../../tmp/nginx-1.14.2.tar.gz 這個路徑是針對 ftp1 賬號的家目錄而言的
3 [yun@nginx_proxy01 20190715]$ curl -O ftp://ftp1:[email protected]:21/../../tmp/nginx-1.14.2.tar.gz  
4 # 或者
5 [yun@nginx_proxy01 20190715]$ curl -O -u ftp1:123456 ftp://172.16.1.195:21/../../tmp/nginx-1.14.2.tar.gz
6   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
7                                  Dload  Upload   Total   Spent    Left  Speed
8 100  991k  100  991k    0     0  5910k      0 --:--:-- --:--:-- --:--:-- 5937k

 

檔案上傳

FTP檔案上傳

可以通過 -T, --upload-file <file> 選項實現。

說明1:其中 ftp1 使用者是ftp服務端的賬號,具體家目錄是:/mnt/ftp1

1 # 其中 tmp_client.data 是客戶端本地檔案; 
2 # /tmpdata/ 這個路徑是針對 ftp1 賬號的家目錄而言的,且上傳時該目錄必須是存在的,否則上傳失敗。
3 # 因此上傳後文件在ftp服務端的絕對路徑是:/mnt/ftp1/tmpdata/tmp_client.data
4 [yun@nginx_proxy01 20190715]$ curl -T tmp_client.data ftp://ftp1:[email protected]:21/tmpdata/
5 # 或者
6 [yun@nginx_proxy01 20190715]$ curl -T tmp_client.data -u ftp1:123456 ftp://172.16.1.195:21/tmpdata/
7   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
8                                  Dload  Upload   Total   Spent    Left  Speed
9 100 2048M    0     0  100 2048M      0  95.4M  0:00:21  0:00:21 --:--:-- 49.3M

 

斷點續傳

使用 -C, --continue-at <offset> 選項實現。其中使用 “-C -“「注意有空格和無空格的情況」,告訴curl自動找出在哪裡/如何恢復傳輸。

網頁端斷點續傳下載

1 curl -C - -o tmp.data http://www.zhangblog.com/uploads/tmp/tmp.data   # 下載一個 2G 的檔案

 

FTP斷點續傳下載

細節就不多說了,可參見上面的「FTP檔案下載」

1 curl -C - -o tmp.data1 ftp://ftp1:[email protected]:21/tmpdata/tmp.data  # 下載一個 2G 的檔案
2 # 或則
3 curl -C - -o tmp.data1 -u ftp1:123456 ftp://172.16.1.195:21/tmpdata/tmp.data  # 下載一個 2G 的檔案

 

分段下載

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

可使用 -r, --range <range> 選項實現。

如下示例使用了同一張圖片,大小為 18196 位元組。

網頁端分段下載

分段下載

 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 ### 分段下載一個檔案
12 [root@iZ28xbsfvc4Z 20190715]# curl -r 0-499   -o 00-jpg.part1 http://www.zhangblog.com/uploads/hexo/00.jpg
13 [root@iZ28xbsfvc4Z 20190715]# curl -r 500-999 -o 00-jpg.part2 http://www.zhangblog.com/uploads/hexo/00.jpg
14 [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 00.jpg
3 total 56
4 -rw-r--r-- 1 root root 18196 Jul 15 11:29 00.jpg

 

FTP分段下載

分段下載

1 [yun@nginx_proxy01 20190715]$ curl -r 0-499   -o 00-jpg.part1 ftp://ftp1:[email protected]:21/tmpdata/00.jpg
2 [yun@nginx_proxy01 20190715]$ curl -r 500-999 -o 00-jpg.part2 ftp://ftp1:[email protected]:21/tmpdata/00.jpg
3 [yun@nginx_proxy01 20190715]$ curl -r 1000-   -o 00-jpg.part3 ftp://ftp1:[email protected]:21/tmpdata/00.jpg

 

檢視下載檔案

1 [yun@nginx_proxy01 20190715]$ ll 00-jpg.part*
2 -rw-rw-r-- 1 yun yun   500 Jul 15 17:59 00-jpg.part1
3 -rw-rw-r-- 1 yun yun   500 Jul 15 18:00 00-jpg.part2
4 -rw-rw-r-- 1 yun yun 17196 Jul 15 18:00 00-jpg.part3

 

檔案合併

1 [yun@nginx_proxy01 20190715]$ cat 00-jpg.part1 00-jpg.part2 00-jpg.part3 > 00.jpg
2 [yun@nginx_proxy01 20190715]$ ll 00.jpg 
3 -rw-rw-r-- 1 yun yun 18196 Jul 15 18:02 00.jpg

 

推薦閱讀

Linux curl 命令詳解

Linux curl 常用示例

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

 


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

———END———-

&n