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 and TFTP),curl設計為無用戶交互下完成工作;

  curl提供了一大堆非常有用的功能,包括代理訪問、用戶認證、ftp上傳下載、HTTP POST、SSL連接、cookie支持、斷點續傳...。
 
一、curl命令語法:
curl [options] [URL...]
 
二、curl命令參數詳解:
  由於linux curl功能十分強大,所以命令參數十分多,下表只是愛E族(aiezu.com)帥選出來的部分參數,更多參數請運行“man curl”命令查看。
參數組 參數 描述
url url 需要抓取的一到多個URLs;
多個下面通配符的方式:
  1、http://{www,ftp,mail}.aiezu.com;
  2、http://aiezu.com/images/[001-999].jpg;
  3、http://aiezu.com/images/[1-999].html;
  4、ftp://aiezu.com/file[a-z].txt


-H "name: value"
--header "name: value"
(HTTP)添加一個http header(http請求頭);
-H "name:"
--header "name:"
(HTTP)移除一個http header(http請求頭);
-A "string"
--user-agent "string"
【參考】
(HTTP)設置Http請求頭“User-Agent”,服務器通過“User-Agent”可以判斷客戶端使用的瀏覽器名稱和操作系統類型,偽造此參數能導致服務器做出錯誤判斷。
也可以使用“-H”, “--header option”設置此選項;
-e <URL>
--referer <URL>
【參考】
(HTTP)設置訪問時的來源頁面,告訴http服務從哪個頁面進入到此頁面;
-e "aiezu.com"相當於“-H "Referer: www.qq.com"”;


-I
--head
(HTTP)只輸出HTTP-header,不獲取內容(HTTP/FTP/FILE)。
用於HTTP服務時,獲取頁面的http頭;
  (如:curl -I http://aiezu.com)
用於FTP/FILE時,將會獲取文件大小、最后修改時間;
  (如:curl -I file://test.txt)
-i
--include
(HTTP)輸出HTTP頭和返回內容;
-D <file>
--dump-header <file>
(HTTP)轉儲http響應頭到指定文件;
cookie -b name=data
--cookie name=data
【參考】
(HTTP)發送cookie數據到HTTP服務器,數據格式為:"NAME1=VALUE1; NAME2=VALUE2";

如果行中沒有“=”,將把參數值當作cookie文件名;

這個cookie數據可以是由服務器的http響應頭“Set-Cookie:”行發送過來的;
-c filename
--cookie-jar file name
【參考】
(HTTP)完成操作后將服務器返回的cookies保存到指定的文件;
指定參數值為“-”將定向到標准輸出“如控制台”;
-j
--junk-session-cookies
(HTTP)告訴curl放棄所有的"session cookies";
相當於重啟瀏覽器;
代理 -x host:port
-x [protocol://[user:pwd@]host[:port]
--proxy [protocol://[user:pwd@]host[:port]
【參考】
使用HTTP代理訪問;如果未指定端口,默認使用8080端口;
protocol默認為http_proxy,其他可能的值包括:
http_proxy、HTTPS_PROXY、socks4、socks4a、socks5;
如:
--proxy 8.8.8.8:8080;
-x "http_proxy://aiezu:123@aiezu.com:80"
-p
--proxytunnel
將“-x”參數的代理,作為通道的方式去代理非HTTP協議,如ftp;
--socks4 <host[:port]>
--socks4a <host[:port]>
--socks5 <host[:port]>
【參考】
使用SOCKS4代理;
使用SOCKS4A代理;
使用SOCKS5代理;
此參數會覆蓋“-x”參數;
--proxy-anyauth
--proxy-basic
--proxy-diges
--proxy-negotiate
--proxy-ntlm
http代理認證方式,參考:
--anyauth
--basic
--diges
--negotiate
--ntlm
-U <user:password>
--proxy-user <user:password>
設置代理的用戶名和密碼;
數據
傳輸
-G
--get
【參考】
如果使用了此參數,“-d/”、“--data”、“--data-binary”參數設置的數據,講附加在url上,以GET的方式請求; 
-d @file
-d "string"
--data "string"
--data-ascii "string"
--data-binary "string"
--data-urlencode "string"
【參考】
(HTTP)使用HTTP POST方式發送“key/value對”數據,相當於瀏覽器表單屬性(method="POST",enctype="application/x-www-form-urlencoded")
  -d,--data:HTTP方式POST數據;
  --data-ascii:HTTP方式POST ascii數據;
  --data-binary:HTTP方式POST二進制數據;
  --data-urlencode:HTTP方式POST數據(進行urlencode);
如果數據以“@”開頭,后緊跟一個文件,將post文件內的內容;
-F name=@file
-F name=<file
-F name=content
--form name=content
【參考】
(HTTP)使用HTTP POST方式發送類似“表單字段”的多類型數據,相當於同時設置瀏覽器表單屬性(method="POST",enctype="multipart/form-data"),可以使用此參數上傳二進制文件。

如果字段內容以“@”開頭,剩下的部分應該是文件名,curl將會上傳此文件,如:
curl -F "pic=@pic.jpg" http://aiezu.com;
curl -F "page=@a.html;type=text/html" http://aiezu.com
curl -F "page=@/tmp/a;filename=a.txt" http://aiezu.com

如果字段內容以“<”開頭,剩下的部分應該是文件名,curl將從文件中獲取作為此字段的值,如:curl -F "text=<text.txt" http://aiezu.com;
--form-string <key=value> (HTTP)類似於“--form”,但是“@”、“<”無特殊含義;
-T file
--upload-file file
通過“put”的方式將文件傳輸到遠程網址;

選項參數只使用字符"-",將通過stdin讀入文件內容;
如:
cat test.txt|curl "http://aiezu.com/a.php" -T - 
curl "http://aiezu.com/a.php" -T - <test.txt

此參數也可以使用通配符:
curl -T "{file1,file2}" http://aiezu.com
curl -T "img[1-1000].png" http://aiezu.com
斷點
續傳
-C <offset>
--continue-at <offset>
斷點續轉,從文件頭的指定位置開始繼續下載/上傳;
offset續傳開始的位置,如果offset值為“-”,curl會自動從文件中識別起始位置開始傳輸;
-r <range>
--range <range>
(HTTP/FTP/SFTP/FILE) 只傳輸內容的指定部分:
0-499:最前面500字節;
-500:最后面500字節;
9500-:最前面9500字節;
0-0,-1:最前面和最后面的1字節;
100-199,500-599:兩個100字節;



認證
--basic (HTTP)告訴curl使用HTTP Basic authentication(HTTP協議時),這是默認認證方式;
--ntlm (HTTP)使用NTLM身份驗證方式,用於HTTP協議;
一般用於IIS使用NTLM的網站;
--digest (HTTP)使用HTTP Digest authentication加密,用於HTTP協議;
配合“-u/--user”選項,防止密碼使用明文方式發送;
--negotiate (HTTP)使用GSS-Negotiate authentication方式,用於HTTP協議;
它主要目的是為它的主要目的是為kerberos5認證提供支持支持;
--anyauth (HTTP)告訴curl自動選擇合適的身份認證方法,並選用最安全的方式;
-u user:password
--user user:password
使用用戶名、密碼認證,此參數會覆蓋“-n”、“--netrc”和“--netrc-optional”選項;

如果你只提供用戶名,curl將要求你輸入密碼;

如果你使用“SSPI”開啟的curl庫做“NTLM”認證,可以使用不含用戶名密碼的“-u:”選項,強制curl使用當前登錄的用戶名密碼進行認證;

此參數相當於設置http頭“Authorization:”;
證書 -E <證書[:密碼]>
--cert <證書[:密碼]>
(SSL)指定“PEM”格式的證書文件和證書密碼;
--cert-type <type> (SSL)告訴curl所提供證書的類型:PEM、DER、ENG等;
默認為“PEM”;
--cacert <CA證書> (SSL)告訴curl所以指定的CA證書文件,必須是“PEM”格式;
--capath <CA證書路徑> (SSL)告訴curl所以指定目錄下的CA證書用來驗證;
這些證書必須是“PEM”格式;
--crlfile <file> (HTTPS/FTPS)提供一個PEM格式的文件,用於指定被吊銷的證書列表;
-k
--insecure
(SSL)設置此選項將允許使用無證書的不安全SSL進行連接和傳輸。
SSL
其他
--ciphers <list of ciphers> (SSL)指定SSL要使用的加密方式;如:“aes_256_sha_256”;
--engine <name> 設置一個OpenSSL加密引擎用於加密操作;
使用“curl --engine list”查看支持的加密引擎列表;
--random-file (SSL)指定包含隨機數據的文件路徑名;數據是用來為SSL連接產生隨機種子為;
--egd-file <file> (SSL)為隨機種子生成器EGD(Entropy Gathering Daemon socket)指定的路徑名;
-1/--tlsv1
--tlsv1.0
--tlsv1.1
--tlsv1.2
-2/--sslv2
-3/--sslv3
(SSL)使用TLS版本2與遠程服務器通訊;
(SSL)使用TLS 1.0版本與遠程服務器通訊;
(SSL)使用TLS 1.1版本與遠程服務器通訊;
(SSL)使用TLS 1.2版本與遠程服務器通訊;
(SSL)使用SSL版本2與遠程服務器通訊;
(SSL)使用SSL版本3與遠程服務器通訊;
私鑰
公鑰
--key <key> (SSL/SSH)指定一個私鑰文件名;為指定時自動嘗試使用下面文件:“~/.ssh/id_rsa”、“~/.ssh/id_dsa”、“./id_rsa'”、 “./id_dsa”;
--key-type <type> (SSL)指定私鑰文件類型,支持:DER、PEM、ENG,默認是PEM;
--pass <phrase> (SSL/SSH)指定私鑰文件的密碼;
--pubkey <key> (SSH)使用指定文件提供的您公鑰;
FTP -P
--ftp-port <接口>
(FTP)FTP主動模式時,設置一個地址等待服務器的連接,如:
網卡:eth1
IP:8.8.8.8
主機名:aiezu.com
可以加端口號:eth1:20000-21000;
--crlf (FTP)上傳時將換行符(LF)轉換為回車換行(CRLF);
--ftp-account [data] (FTP)ftp帳號信息;
--ftp-method [method] (FTP)可選值:multicwd/nocwd/singlecwd;
--ftp-pasv (FTP)使用使用PASV(被動)/EPSV模式;
--ftp-skip-pasv-ip (FTP)使用PASV的時,跳過指定IP;
--ftp-create-dirs (FTP)上傳時自動創建遠程目錄;
-l
--list-only
(FTP)列出ftp文件列表;
-B
--use-ascii
(FTP/LDAP)使用Ascii傳輸模式,用於FTP、LDAP;在ftp中相當與使用了“type=A;”模式。
--disable-epsv (FTP)告訴curl在PASV(被動模式)時不要使用EPSV;
--disable-eprt (FTP)告訴curl在主動模式時禁用EPRT和LPRT;
限速 --limit-rate <speed> 限制curl使用的最大帶寬;如果未指定單位,默認單位為“bytes/秒”,你也可以指定單位為“K”、“M”、“G”等單位,如:“--limit-rate 1m”為限制最大使用帶寬為“1m字節/秒”;
-y
--speed-time <time>
If a download is slower than speed-limit bytes per second during a speed-time period, the download gets aborted. If speed-time is used, the default speed-limit will be 1 unless set with -Y.
This option controls transfers and thus will not affect slow connects etc. If this is a concern for you, try the --connect-timeout option.
-Y
--speed-limit <speed>
If a download is slower than this given speed (in bytes per second) for speed-time seconds it gets aborted. speed-time is set with -y and is 30 if not set.
其他
選項
-0/--http1.0 (HTTP) 強制curl使用HTTP 1.0而不是使用默認的HTTP 1.1;
--interface <name> 使用指定的網卡接口訪問;
curl --interface eth0 http://aiezu.com
curl --interface 10.0.0.101 http://aiezu.com
-X <command>
--request <command>
(HTTP)指定與服務器通信使用的請求方法,如:GET、PUT、POST、DELETE等,默認GET;
--keepalive-time <seconds> 設置keepalive時間
--no-keepalive 關閉keepalive功能;
--no-buffer 禁用對輸出流緩沖;
--buffer 啟用輸出流緩沖;
-L
--location
(HTTP/HTTPS)追隨http響應頭“Location:”定向到跳轉后的頁面;
(在http響應碼為3XX時使用,如301跳轉、302跳轉)
--location-trusted (HTTP/HTTPS)同“--location”,但跳轉后會發送跳轉前的用戶名和密碼;
--compressed (HTTP)請求對返回內容使用壓縮算法進行壓縮;curl支持對gzip壓縮進行解壓;
--connect-timeout <seconds> 指定最大連接超時,單位“秒”;
-m seconds
--max-time seconds
限制整個curl操作的最長時間,單位為秒;
-s
--silent
安靜模式。不要顯示進度表或錯誤消息;
-#
--progress-bar
顯示進度條;
錯誤
選項
-f
--fail
(HTTP)連接失敗時(400以上錯誤)不返回默認錯誤頁面,而是返回一個curl錯誤碼“22”;
--retry <num>
--retry-delay <seconds>
--retry-max-time <seconds>
失敗重試次數;
重試間隔時間;
最大重試時間;
-S
--show-error
安靜模式下顯示錯誤信息;
--stderr <file> 錯誤信息保存文件;
輸出 -o file
--output file
將返回內容輸出到文件。
如果是用過通配符獲取多個url,可以使用“#”后跟“數字序號”,curl會自動將它替換對應的關鍵詞,如:
  curl "http://aiezu.com/{a,b}.txt" -o "#1.txt";
  將保存為:“a.txt”,“b.txt”;

  curl "http://aiezu.com/{a,b}_[1-3].txt" -o "#1#2.txt";
  將保存為:a1.txt、a2.txt、a3.txt、b1.txt、b2.txt、b3.txt

  如果要根據規則創建保存目錄,參考:“--create-dirs”

指定“-”將定向到標准輸出“如控制台”; 
-O
--remote-name
將返回內容輸出到當前目錄下,和url中文件名相同的文件中(不含目錄);
--create-dirs 與“-o”參數配合使用,創建必要的本地目錄層次結構
-w
--write-out format
操作完成后在返回信息尾部追加指定的內容;要追加的內容可以是一個字符串“string”、從文件中獲取“@filename”、從標准輸入中獲取“@-”

格式參數中可以用%{variable_name} 方式使用響應信息的相關變量,如:%{content_type}、%{http_code}、%{local_ip}...,更多變量參考“man curl”獲取;

格式參數可以使用“\n”、“\r”、“\t”等轉義字符;
調試 --trace <file> 轉儲所有傳入和傳出的數據到文件,包括描述信息;
使用“-”作為文件名將輸出發送到標准輸出。
--trace-ascii file 轉儲所有傳入和傳出的數據到文件,包括描述信息,只轉儲ASCII部分,更容易閱讀;
使用“-”作為文件名將輸出發送到標准輸出。
這個選項會覆蓋之前使用的-v、 --verbose、 --trace-ascii選項;
--trace-time 轉儲文件中添加時間信息;
-K
--config <config file>
從配置文件中讀取參數,參考:http://curl.haxx.se/docs/
-v
--verbose
顯示更詳細的信息,調試時使用;
幫助 -M
--manual
顯示完整的幫助手冊;
-h
--help
linux curl用法幫助;
 
三、Linux curl命令退出碼:
下面是linux curl命令的錯誤代碼和她們的相應的錯誤消息,可能會出現在惡劣的環境。
退出碼 錯誤描述
1 Unsupported protocol. This build of curl has no support for this protocol.
2 Failed to initialize.
3 URL malformed. The syntax was not correct.
5 Couldn't resolve proxy. The given proxy host could not be resolved.
6 Couldn't resolve host. The given remote host was not resolved.
7 Failed to connect to host.
8 FTP weird server reply. The server sent data curl couldn't parse.
9 FTP access denied. The server denied login or denied access to the particular resource or directory you wanted to reach. Most often you tried to change to a directory that doesn't exist on the server.
11 FTP weird PASS reply. Curl couldn't parse the reply sent to the PASS request.
13 FTP weird PASV reply, Curl couldn't parse the reply sent to the PASV request.
14 FTP weird 227 format. Curl couldn't parse the 227-line the server sent.
15 FTP can't get host. Couldn't resolve the host IP we got in the 227-line.
17 FTP couldn't set binary. Couldn't change transfer method to binary.
18 Partial file. Only a part of the file was transferred.
19 FTP couldn't download/access the given file, the RETR (or similar) command failed.
21 FTP quote error. A quote command returned error from the server.
22 HTTP page not retrieved. The requested url was not found or returned another error with the HTTP error code being 400 or above. This return code only appears if -f/--fail is used.
23 Write error. Curl couldn't write data to a local filesystem or similar.
25 FTP couldn't STOR file. The server denied the STOR operation, used for FTP uploading.
26 Read error. Various reading problems.
27 Out of memory. A memory allocation request failed.
28 Operation timeout. The specified time-out period was reached according to the conditions.
30 FTP PORT failed. The PORT command failed. Not all FTP servers support the PORT command, try doing a transfer using PASV instead!
31 FTP couldn't use REST. The REST command failed. This command is used for resumed FTP transfers.
33 HTTP range error. The range "command" didn't work.
34 HTTP post error. Internal post-request generation error.
35 SSL connect error. The SSL handshaking failed.
36 FTP bad download resume. Couldn't continue an earlier aborted download.
37 FILE couldn't read file. Failed to open the file. Permissions?
38 LDAP cannot bind. LDAP bind operation failed.
39 LDAP search failed.
41 Function not found. A required LDAP function was not found.
42 Aborted by callback. An application told curl to abort the operation.
43 Internal error. A function was called with a bad parameter.
45 Interface error. A specified outgoing interface could not be used.
47 Too many redirects. When following redirects, curl hit the maximum amount.
48 Unknown TELNET option specified.
49 Malformed telnet option.
51 The peer's SSL certificate or SSH MD5 fingerprint was not ok.
52 The server didn't reply anything, which here is considered an error.
53 SSL crypto engine not found.
54 Cannot set SSL crypto engine as default.
55 Failed sending network data.
56 Failure in receiving network data.
58 Problem with the local certificate.
59 Couldn't use specified SSL cipher.
60 Peer certificate cannot be authenticated with known CA certificates.
61 Unrecognized transfer encoding.
62 Invalid LDAP URL.
63 Maximum file size exceeded.
64 Requested FTP SSL level failed.
65 Sending the data requires a rewind that failed.
66 Failed to initialize SSL Engine.
67 The user name, password, or similar was not accepted and curl failed to log in.
68 File not found on TFTP server.
69 Permission problem on TFTP server.
70 Out of disk space on TFTP server.
71 Illegal TFTP operation.
72 Unknown TFTP transfer ID.
73 File already exists (TFTP).
74 No such user (TFTP).
75 Character conversion failed.
76 Character conversion functions required.
77 Problem with reading the SSL CA cert (path? access rights?).
78 The resource referenced in the URL does not exist.
79 An unspecified error occurred during the SSH session.
80 Failed to shut down the SSL connection.
82 Could not load CRL file, missing or wrong format (added in 7.19.0).
83 Issuer check failed (added in 7.19.0).
XX More error codes will appear here in future releases. The existing ones are meant to never change.

四、用法演示:
1、下載頁面:
1
curl -o index.html http: //aiezu .com
 
2、下載文件並顯示簡單進度條:
1
curl - # -o centos6.8.iso http://mirrors.aliyun.com/centos/6.8/isos/x86_64/CentOS-6.8-x86_64-minimal.iso
 
3、斷點續傳:
1
2
#繼續完成上次終止的未完成的下載
curl - # -o centos6.8.iso -C - http://mirrors.aliyun.com/centos/6.8/isos/x86_64/CentOS-6.8-x86_64-minimal.iso

4、偽造來源頁面:
1
2
#告訴愛E族,我是從百度來的
curl -e http: //baidu .com http: //aiezu .com

 5、偽造代理設備:
1
2
3
4
5
#告訴愛E族,我是GOOGLE爬蟲蜘蛛(其實我是curl命令)
curl -A  " Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"  http: //aiezu .com
 
#告訴愛E族,我用的是微信內置瀏覽器
curl -A  "Mozilla/5.0 AppleWebKit/600 Mobile MicroMessenger/6.0"  http: //aiezu .com
 
6、http頭:
1
2
# 看看本站的http頭是怎么樣的
curl -I  http: //aiezu .com
輸出:
1
2
3
4
5
6
7
8
9
HTTP/1.1 200 OK
Date: Fri, 25 Nov 2016 16:45:49 GMT
Server: Apache
Set-Cookie: rox__Session=abdrt8vesprhnpc3f63p1df7j4; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Vary: Accept-Encoding
Content-Type: text/html; charset=utf-8
 
6、設置http請求頭:
1
curl -H  "Cache-Control:no-cache"   http: //aiezu .com

7、發送表單數據:
1
curl -F  "pic=@logo.png"  -F  "site=aiezu"   http: //aiezu .com/

8、發送cookie:
1
curl -b  "domain=aiezu.com"   http: //aiezu .com


免責聲明!

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



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