linux之curl命令


curl命令 是一個利用URL規則在命令行下工作的文件傳輸工具。它支持文件的上傳和下載,所以是綜合傳輸工具,但按傳統,習慣稱curl為下載工具。作為一款強力工具,curl支持包括HTTP、HTTPS、ftp等眾多協議,還支持POST、cookies、認證、從指定偏移處下載部分文件、用戶代理字符串、限速、文件大小、進度條等特征。

命令語法

> curl (選項)(參數)

命令選項

-A/--user-agent <string>              設置用戶代理發送給服務器
-b/--cookie <name=string/file>    cookie字符串或文件讀取位置
-c/--cookie-jar <file>                    操作結束后把cookie寫入到這個文件中
-C/--continue-at <offset>            斷點續轉
-D/--dump-header <file>              把header信息寫入到該文件中
-e/--referer                                  來源網址
-f/--fail                                          連接失敗時不顯示http錯誤
-o/--output                                  把輸出寫到該文件中
-O/--remote-name                      把輸出寫到該文件中,保留遠程文件的文件名
-r/--range <range>                      檢索來自HTTP/1.1或FTP服務器字節范圍
-s/--silent                                    靜音模式。不輸出任何東西
-T/--upload-file <file>                  上傳文件
-u/--user <user[:password]>      設置服務器的用戶和密碼
-w/--write-out [format]                什么輸出完成后
-x/--proxy <host[:port]>              在給定的端口上使用HTTP代理
-#/--progress-bar                        進度條顯示當前的傳送狀態

文件下載

  • curl命令可以用來執行下載、發送各種HTTP請求,指定HTTP頭部等操作
  • curl是將下載文件輸出到stdout,將進度信息輸出到stderr,不顯示進度信息使用--silent選項。
> curl https://rumenz.com --silent

下載文件到指定的文件小寫-o,大寫 -O 保存文件和它的原始文件名

> curl https://rumenz.com/1.html -o 2.html

大寫 -O 保存文件和它的原始文件名

> curl https://rumenz.com/1.html -O

--progress顯示進度條

> curl https://rumenz.com/2.html -o 2.html --progress

斷點續傳

> curl -O -u 'rumenz':'test' ftp://rumenz.com/jdk.tar.gz

然后你的連接突然斷開,你可以用以下命令繼續下載

> curl -C -  -O  -u 'rumenz':'test' ftp://rumenz.com/jdk.tar.gz

注意斷點續傳的參數是-C, 要自動續傳的話要使用 -C -, 否則需要手工指定斷點的字節位置.

偽造請求來源

> curl -e https://json.im https://rumenz.com

參照頁是位於HTTP頭部中的一個字符串,用來表示用戶是從哪個頁面到達當前頁面的,如果用戶點擊網頁A中的某個連接,那么用戶就會跳轉到B網頁,網頁B頭部的參照頁字符串就包含網頁A的URL。也可以使用--referer選項指定參照頁字符串.

-H參數可以通過直接添加標頭 Referer,達到同樣效果

> curl -H 'Referer: https://json.im' https://rumenz.com

設置請求header

> curl -H "Host:rumenz.com" -H "accept-language:zh-cn" URL

curl的帶寬控制

> curl --limit-rate 200k  https://rumenz.com/1.html

用curl進行認證

使用curl選項 -u 可以完成HTTP或者FTP的認證,可以指定密碼,也可以不指定密碼在后續操作中輸入密碼:

> curl -u user:pwd https://rumenz.com
> curl -u user https://rumenz.com

只打印響應頭

> curl -I https://rumenz.com
HTTP/1.1 200 OK
Server: openresty/1.19.3.1
Date: Wed, 02 Jun 2021 13:37:41 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive

使用curl模擬get請求

直接顯示網頁內容

> curl https://json.im/1.txt
123
456

顯示請求頭和網頁內容

> curl -i https://json.im/1.txt
HTTP/1.1 200 OK
Server: openresty
Date: Wed, 02 Jun 2021 14:02:30 GMT
Content-Type: text/plain
Content-Length: 8
Last-Modified: Wed, 02 Jun 2021 14:00:57 GMT
Connection: keep-alive
ETag: "60b78f19-8"
Accept-Ranges: bytes

123
456

直接顯示網頁內容

> curl -l https://json.im/1.txt

顯示get請求全過程解析

> curl -v https://json.im/1.txt

·

使用curl模擬post請求

> curl -d "param1=value1&param2=value2" https://json.im/login
> curl -d'login=rumenz&password=123' -X POST https://json.im/login
> curl -d 'login=rumenz' -d 'password=123' -X POST  https://json.im/login

--data-urlencode 參數等同於-d,發送 POST請求的數據體,區別在於會自動將發送的數據進行 URL 編碼.

> curl --data-urlencode 'comment=hello world' https://json.im/login

上傳文本文件

> curl -d '@data.txt' https://json.im/upload

post json格式的數據

> curl -l -H 'Content-type: application/json' -X POST -d '{"rumenz":"123"}' https://json.im/123.json
> curl https://json.im --cookie "user=rumenz&pass=123456"

Cookie寫入到一個文件

> curl -c cookies.txt https://json.im

上傳二進制文件

-F 參數用來向服務器上傳二進制文件。

> curl -F "file=@123.png" https://json.im/uploadfile

上面命令會給 HTTP 請求加上標頭 Content-Type: multipart/form-data ,然后將文件123..png作為file字段上傳。

-F 參數可以指定MIME 類型。

> curl -F 'file=@123.png;type=image/png'  https://json.im/uploadfile

上面命令指定 MIME 類型為image/png,否則 curl 會把 MIME 類型設為 application/octet-stream。

-F參數也可以指定文件名。

> curl -F 'file=@123.png;filename=rumenz.png' https://json.im/uploadfile 

上面命令中,原始文件名為123.png,但是服務器接收到的文件名為rumenz.png

請求跟隨服務器的重定向

-L參數會讓 HTTP 請求跟隨服務器的重定向。curl 默認不跟隨重定向。

> curl -L -d 'rumenz=123' https://json.im/

調試參數

-v 參數輸出通信的整個過程,用於調試。

> curl -v https://json.im/1.txt
*   Trying 150.109.147.28...
* TCP_NODELAY set
* Connected to json.im (150.109.147.28) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/cert.pem
  CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: CN=json.im
*  start date: Apr 27 14:50:23 2021 GMT
*  expire date: Jul 26 14:50:23 2021 GMT
*  subjectAltName: host "json.im" matched cert's "json.im"
*  issuer: C=US; O=Let's Encrypt; CN=R3
*  SSL certificate verify ok.
> GET /1.txt HTTP/1.1
> Host: json.im
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: openresty
< Date: Wed, 02 Jun 2021 14:31:36 GMT
< Content-Type: text/plain
< Content-Length: 8
< Last-Modified: Wed, 02 Jun 2021 14:00:57 GMT
< Connection: keep-alive
< ETag: "60b78f19-8"
< Accept-Ranges: bytes
<
123
456
* Connection #0 to host json.im left intact

--trace參數也可以用於調試,還會輸出原始的二進制數據。

> curl --trace - https://json.im

原文鏈接:https://rumenz.com/rumenbiji/linux-curl.html
微信公眾號:入門小站


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM