天天看點

curlcurl

curl

  • -# 顯示進度(具體顯示啥進度測試不出來)
  • -0 指定使用http1.0協定發起請求
$curl -svI   ab.text.com/jdk-8u101-linux-x64.tar.gz 
......
> HEAD /big/SGWS5.PC6game.zip HTTP/1.1  #預設協定為1.1

$curl -0  -svI   ab.text.com/jdk-8u101-linux-x64.åtar.gz
......
> HEAD /big/SGWS5.PC6game.zip HTTP/1.0  #預設協定已經修改為http 1.0了
           
  • 進行https通路 -1 強制使用 TLS1.x版本通路(ssl 1版本),-2 ssl 2版本 -3 ssl 3版本 
  •  -4 将位址解析為ipv4位址(有ipv6的情況下)  -6 将位址解析為ipv6位址(有ipv6的情況下)(不知道怎麼測試,先放着繼續往下)
  •  -a 在進行ftp/sftp上傳的時,使用-a參數可以将本地檔案追加到目錄下面 (測試出問題了)
  • -A 使用某個用戶端通路,指定多個用戶端的時候,以最後一個為主
$curl -A 'Mozilla/5.0' -I  ab.text.com/jdk-8u101-linux-x64.tar.gz

# tail -1  /data/logs/www/ab.text.com.log   #通過下面的日志就可以看出使用的是Mozilla進行通路,而不是通過curl通路的
10.168.1.102 | - | 22/Oct/2019:09:53:53 -0400 | HEAD /jdk-8u101-linux-x64.tar.gz HTTP/1.1 | 200 | 256 | 0 | ab.text.com | - | Mozilla/5.0 | - | - | - | 0.000 | -
           
  • -b 将用戶端資料作為cookie發送給伺服器,在浏覽器通路某個網站的時候,浏覽器會自動将本地cookie資訊打包發送給伺服器。
#發送單個cookie
$curl -b "test=test1" -vI  ab.text.com/jdk-8u101-linux-x64.tar.gz
*   Trying 10.28.88.199...
* TCP_NODELAY set
* Connected to ab.text.com (10.28.88.199) port 80 (#0)
> HEAD /jdk-8u101-linux-x64.tar.gz HTTP/1.1
> Host: ab.text.com
> User-Agent: curl/7.54.0
> Accept: */*
> Cookie: test=test1

#發送多個cookie 文法:“name1=value1;name2=value2” 中間用;隔開
$curl -b "test=test1;test2=test2" -vI  ab.text.com/jdk-8u101-linux-x64.tar.gz
*   Trying 10.28.88.199...
* TCP_NODELAY set
* Connected to ab.text.com (10.28.88.199) port 80 (#0)
> HEAD /jdk-8u101-linux-x64.tar.gz HTTP/1.1
> Host: ab.text.com
> User-Agent: curl/7.54.0
> Accept: */*
> Cookie: test=test1;test2=test2

#讀取本地cookei 文法: -b filename(沒有看到cookie資訊)
$curl -b 1.txt -vI  ab.text.com
           
  • -c 把伺服器發送過來的cookie存儲到本地,如果沒有傳回任何cookie,則不會寫入任何檔案
$curl -c cookie.txt -vL  www.baidu.com  #需要加 -L 才能看到set-cookie資訊
......
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/

$ls cookie.txt
cookie.txt

$cat cookie.txt
# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

.baidu.com	TRUE	/	FALSE	1571841604	BDORZ	27315
           
  • -X 指定發起請求所使用的請求方法(後面跟着各種請求方法,什麼prot,put,HTTPS_PROXY等等) 預設是get
  •  -H  指定一個header(标頭,指定請求頭部分 )選項 值是key=value 需要指定其他header時,curl需要加 -H參數
  •   -d  發送post請求的資料體  --data-binary 上傳二進制資料  --data-urlencode 自動将資料進行URL編碼(這3個參數的功能大緻一樣)

注:将post請求體發送給伺服器,使用-d參數後,header請求頭會自動添加 application / x-www-form-urlencoded 标頭,也會把請求方法自動轉換為POST方法,不用 -X post   用法: -d '{具體資料}'

#用法基本是這樣
$ curl -H "Content-Type:application/json" -XPUT 'http://10.28.88.199:9200/ac/book/1?pretty' -d '{"title":"創始道紀"}' 
           
  • -D 将header資訊寫入檔案
$curl -D header.txt  -vI  www.baidu.com

s$ls header.txt
header.txt

$cat header.txt
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Tue, 22 Oct 2019 15:07:55 GMT
Etag: "575e1f72-115"
Last-Modified: Mon, 13 Jun 2016 02:50:26 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
           
  • --digest 啟用http身份驗證(防止密碼以明文方式傳輸) 一般配合-u參數使用。
  • -e 設定referer資訊,referer主要表示目前來源頁面,在我的header資訊中,這個referer有詳細介紹
$curl -e "ab.text.com/a.html" http://www.baidu.com

$curl -H  "referer:ab.text.com/a.html" http://www.baidu.com # -H "referer:" 與 -e效果一樣

#為什麼這麼做,那我現在也測試不出來
           
  • -E  通過curl進行https,ftps基于ssl協定通路時候需要指定用戶端證書檔案,并且證書必須為PEM格式,文法: -E, --cert <certificate[:password]>

 --cert-type <type> 指定curl證書支援類型,支援PEM,DER,ENG  --cacert <CA certificate>  指定curl使用指定證書進行驗證   --capath <CA certificate directory> 指定證書目錄多個檔案用;分開(path1;path2)

  • -F 用來向伺服器上傳二進制檔案就是模拟表單送出
#一般用法
$curl -F '[email protected]' ab.text.com/user

#指定檔案類型 使用type指令
$curl -F '[email protected];type=text/plain' ab.text.com/user

#改名(将檔案傳送到伺服器之後進行改名) 使用filename指令
$curl -F '[email protected];filename=cookie' ab.text.com/user

注:有個前提是你的伺服器可以接收你所上傳的資料,并且進行處理
           
  • -G 主要是将-d(-d 預設是post請求方法)指定的資料通過GET請求方法發送給伺服器,簡單來說:-G可以把-d預設的post請求方法改為get請求方法。
#預設通路方式
$curl -v -d 'c=wwert' -d 'count=30'  ab.text.com
* Rebuilt URL to: ab.text.com/
*   Trying 10.28.88.199...
* TCP_NODELAY set
* Connected to ab.text.com (10.28.88.199) port 80 (#0)
> POST / HTTP/1.1
> Host: ab.text.com
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Length: 16
> Content-Type: application/x-www-form-urlencoded


#加了 -G  請求方法立馬變get,并且URI變成/?c=wwert&count=30
$curl -v  -G -d 'c=wwert' -d 'count=30'  ab.text.com
*   Trying 10.28.88.199...
* TCP_NODELAY set
* Connected to ab.text.com (10.28.88.199) port 80 (#0)
> GET /?c=wwert&count=30 HTTP/1.1
> Host: ab.text.com
> User-Agent: curl/7.54.0
> Accept: */*

#在加個 -I 請求方法立馬變為HEAD
$curl -v -GI -d 'c=wwert' -d 'count=30'  ab.text.com
*   Trying 10.28.88.199...
* TCP_NODELAY set
* Connected to ab.text.com (10.28.88.199) port 80 (#0)
> HEAD /?c=wwert&count=30 HTTP/1.1
> Host: ab.text.com
> User-Agent: curl/7.54.0
> Accept: */*
>
           
  • -i 擷取header資訊及響應内容(預設不列印請求頭資訊)
$curl -i ab.text.com/a.html   # -i 隻顯示響應頭資訊及通路内容
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 23 Oct 2019 03:53:28 GMT
Content-Type: text/html; charset=utf8
Content-Length: 143
Last-Modified: Tue, 15 Oct 2019 03:59:21 GMT
Connection: keep-alive
ETag: "5da54419-8f"
Accept-Ranges: bytes

#這是a頁面
是不是很意外?是不是很驚喜?這就是我的a頁面。
    <!--引入b.html-->
    <!--# include file="b.html" -->
[email protected]:~$
           
  •  -I 隻擷取header資訊(預設不列印請求頭資訊)
$curl -I ab.text.com/a.html
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 23 Oct 2019 03:55:32 GMT
Content-Type: text/html; charset=utf8
Content-Length: 143
Last-Modified: Tue, 15 Oct 2019 03:59:21 GMT
Connection: keep-alive
ETag: "5da54419-8f"
Accept-Ranges: bytes
           
  • --interface <name> 指定某個接口執行操作,接口名稱可以是主機名或主機名 例如:  curl --interface eth0:1 http://www.netscape.com/(沒看見怎麼用過)
  • -k 跳過ssl檢測進行通路(用于顯示不安全ssl連接配接時)

-K 從某個配置檔案中擷取curl參數

檔案内容示例:以Example開頭,以Example結尾

# --- Example file ---
# this is a comment
url = "curl.haxx.se"
output = "curlhere.html"
user-agent = "superagent/1.0"

# and fetch another URL too
url = "curl.haxx.se/docs/manpage.html"
 -O
referer = "http://nowhereatall.com/"
# --- End of example file ---
           
  • --keepalive-time <seconds> 設定連接配接逾時時間
  • --key 指定私鑰,未指定私鑰檔案,預設以“〜/ .ssh / id_rsa”,“〜/ .ssh / id_dsa”,“ ./ id_rsa”,“ ./ id_dsa”方式讀取私鑰,存在多個,則使用最後一個。
  • --key-type <type> 指定私鑰檔案類型,預設是PEM,支援DER,PEM,ENG等檔案類型
  • -l 列出FTP目錄
  • -L 進行重定向頁面跟蹤,用于擷取重定向的頁面,curl預設不擷取重定向頁面
  • --limit-rate <speed> 進行傳輸限速,一般用于模拟網速慢的場景,支援k,m,g 例如:200k,3m.1g
  • -m 指定整個操作花費的最長時間,超過此時間,不管操作有沒有完成,都會斷開連接配接
  • --max-filesize <bytes> 指定最大下載下傳檔案大小,如果檔案大于此值,則不進行傳輸
  • -N 禁止緩沖,預設情況下curl使用标準緩沖進行輸出也就是分塊傳輸資料
  • -o 将響應内容寫到檔案,檔案名需自定義
$curl -o ssd ab.text.com/a.html  #-o 需要在後面加檔案名 類似wget下載下傳功能
$ls ssd
ssd
           
  • -O 将響應内容儲存到本地,預設以URI為檔案名
$curl -O  ab.text.com/a.html
$ls a.html
a.html
           
  • -p 使用http代理進行通路 等于 -x --proxy
  • -P +端口 以被動模式通路FTP
  • --post301 在進行301重定向時,curl不得将post請求轉換為get請求
  • --post302 在進行302重定向時,curl不得将post請求轉換為get請求
  • --post303 在進行303重定向時,curl不得将post請求轉換為get請求
  • --proxy-anyauth 在代理上選擇任一身份驗證
  • --proxy-basic 在代理上選擇基本身份驗證
  • --proxy-digest 在代理上選擇數字驗證
  • --proxy-ntlm 在代理商選擇ntlm驗證
  • -proxy1.0  <代理主機[:端口]> 指定http1.0進行代理,預設是1080端口 這裡是http1.0協定 -x proxy是預設使用http1.1協定
  • -r --range <range>  檢索來自HTTP/1.1或FTP伺服器位元組範圍,前提是伺服器開啟此功能。
  • -R 在本地生成檔案時,保留檔案在伺服器最後修改的時間
  • –resolve HOST:PORT:ADDRESS  将 HOST:PORT 強制解析到 ADDRESS(一般是用于查詢備用伺服器上的東西,也就是沒有做dns解析的網站,dns做了解析的通過curl +url方式可以擷取頁面内容,沒有做解析的話,隻能通過–resolve強制調整,或者在hosts裡面添加對應的記錄,指定對應的域名及ip也可以擷取url結果,不建議後者,太麻煩了。
$curl --resolve m.2234sad.com:80:192.168.1.11 http://m.2234sad.com/games/jinbeihaolemenqipai/

#直接擷取的是目前網頁資訊
           
  •   --retry <num> 指定在失敗後的重試次數
  •  -s 靜音模式 不顯示進度表及錯誤資訊
  • -S 顯示錯誤  與 -s 一起使用時,發生錯誤的時候隻顯示為一條錯誤資訊 
  • --socks4 <host[:port]> 指定socks4代理,不填寫端口,預設端口為1080,同時也會覆寫 -x --proxy使用方法
  • --socks5-basic 使用socks5代理連接配接時,需要進行使用者及密碼驗證
  • -T 進行檔案上傳,需要伺服器支援此方法才行
文法:
curl -T "{file1,file2}" http://www.uploadtothissite.com

示例:
$curl -v -T header.txt ab.text.com/user
           
  • --unix-socket <path> 使用unix套接字進行連接配接
  •  -u  <user:password>  輸入伺服器身份驗證的使用者名及密碼   curl -u 'ab:123456' ab.text.com/login
  • -U <user:password> 輸入代理身份驗證使用者名及密碼   curl -U 'ab:123456' ab.text.com/login
  • --url <URL> 指定擷取URL
  • -v 輸出整個通信過程,可以跟 -I 聯合使用,隻顯示所有的header頭部資訊
$curl -v  ab.text.com/a.html
#顯示通信資訊
*   Trying 10.28.88.199...
* TCP_NODELAY set
* Connected to ab.text.com (10.28.88.199) port 80 (#0)

#顯示請求頭資訊
> GET /a.html HTTP/1.1
> Host: ab.text.com
> User-Agent: curl/7.54.0
> Accept: */*

#顯示響應資訊
< HTTP/1.1 200 OK
< Server: nginx
< Date: Wed, 23 Oct 2019 06:16:13 GMT
< Content-Type: text/html; charset=utf8
< Content-Length: 143
< Last-Modified: Tue, 15 Oct 2019 03:59:21 GMT
< Connection: keep-alive
< ETag: "5da54419-8f"
< Accept-Ranges: bytes

#顯示響應體資訊
#這是a頁面
是不是很意外?是不是很驚喜?這就是我的a頁面。
    <!--引入b.html-->
    <!--# include file="b.html" -->
* Connection #0 to host ab.text.com left intact
           
  • -w 在指令完成後輸出哪些資訊,具體參數如下: 
-w 後面的可以使用的參數
url_effective
http_code 狀态碼
http_connect
time_total 請求總用時
time_namelookup DNS 域名解析的時候,就是把 https://baidu.com 轉換成 ip 位址的過程
time_connect TCP 連接配接建立的時間,就是三次握手的時間
time_appconnect SSL/SSH 等上層協定建立連接配接的時間,比如 connect/handshake 的時間
time_redirect 從開始到最後一個請求事務的時間
time_pretransfer 從請求開始到響應開始傳輸的時間
time_starttransfer 從請求開始到第一個位元組将要傳輸的時間
size_download
size_upload
size_header
size_request
speed_download
speed_upload
content_type
num_connects
num_redirects
ftp_entry_path

#示例 -w意思是将你想要擷取的内容顯示出來,如果不加-o,将擷取整個标頭資訊
$ curl -sI -w "%{http_code}" -o /dev/null  http://www.pc6.com/az/403867.html
200
           
  • -x <[協定://] [user:password @] proxyhost [:port]>  指定http代理通路,如果沒有指定協定預設使用http協定,端口預設為1080端口,還有個前提你通路的網站得支援代理才行 

       在7.21.7版本之後可以使用 protocol:// 這種方式代理,跟-x一樣的用法,并且預設為http代理

        其他幾個特殊代理:socks4://  socks4a://  socks5://(這3個是原來的意思)  socks5h://(這個特殊些,是指--socks5-hostname)

$ curl -v -x 192.168.1.11:80 http://m.2234sad.com/games/jinbeihaolemenqipai/

* About to connect() to proxy 175.6.2.179 port 80 (#0)
*   Trying 192.168.1.11...
* Connected to 192.168.1.11 (192.168.1.11) port 80 (#0)
> GET http://m.2234sad.com/games/jinbeihaolemenqipai/ HTTP/1.1
> User-Agent: curl/7.29.0
> Host: m.22234sad.com
> Accept: */*
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx
< Date: Sun, 29 Sep 2019 01:02:53 GMT
< Content-Type: text/html
< Content-Length: 178
< Connection: keep-alive
< Location: https://m.2234sad.com/games/alogdyan/
<
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Connection #0 to host 192.168.1.11 left intact
           
  • -X 指定請求方法,預設為GET
$ curl -H "Content-Type:application/json" -XPUT 'http://10.28.88.199:9200/ac/book/1?pretty' -d '{"title":"創始道紀"}' 
           
  • -z  <date expression>|<file> 擷取晚于此最後修改時間最後的檔案或者是在該時間之前修改的檔案,比如我要擷取3天之前檔案,類似于find  ctime參數
  • -h 擷取幫助
  • -M 打開man手冊
  • -V 顯示跟curl版本相關資訊

繼續閱讀