linux curl是什么

linux中,cURL是一個非常實用的、用來與服務器之間傳輸數據的工具,是一個利用URL規則在命令行下工作的文件傳輸工具;它支持文件的上傳和下載,是綜合傳輸工具。curl提供了一大非常有用的功能,包括代理訪問、用戶認證、ftp上傳下載、http POST、ssl連接、Cookie支持、斷點續傳等等。

linux curl是什么

本教程操作環境:linux7.3系統、Dell G3電腦。

在Linux中curl是一個利用URL規則在命令行下工作的文件傳輸工具,可以說是一款很強大的http命令行工具。它支持文件的上傳和下載,是綜合傳輸工具,但按傳統,習慣稱url為下載工具。

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命令

1、發送GET請求

curl URL
curl URL?a=1&b=nihao

2、發送POST請求

curl -X POST -d 'a=1&b=nihao' URL

3、發送json格式請求:

curl -H "Content-Type: application/json" -X POST -d '{"abc":123,"bcd":"nihao"}' URL
curl -H "Content-Type: application/json" -X POST -d @test.json URL

其中,-H代表header頭,-X是指定什么類型請求(POST/GET/HEAD/delete/PUT/PATCH),-d代表傳輸什么數據。這幾個是最常用的。

查看所有curl命令: man curl或者curl -h
請求頭:H,A,e
響應頭:I,i,D
cookie:b,c,j
傳輸:F(POST),G(GET),T(PUT),X
輸出:o,O,w
斷點續傳:r
調試:v,–trace,–trace-ASCII,–trace-time

二、curl命令語法與curl命令參數詳解

1、curl命令語法

curl [options] [URL...]

2、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?
–referer
(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?
–dump-header
(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
–socks4a
–socks5
使用SOCKS4代理;
使用SOCKS4A代理;
使用SOCKS5代理;
此參數會覆蓋“-x”參數;
–proxy-anyauth
–proxy-basic
–proxy-diges
–proxy-negotiate
–proxy-ntlm
http代理認證方式,參考:
–anyauth
–basic
–diges
–negotiate
–ntlm
-U? –proxy-user 設置代理的用戶名和密碼;
數據
傳輸
-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= -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

如果字段內容以“

–form-string (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 –
此參數也可以使用通配符:
curl -T “{file1,file2}” http://aiezu.com
curl -T “img[1-1000].png” http://aiezu.com

斷點
續傳
-C?
continue-at?
斷點續轉,從文件頭的指定位置開始繼續下載/上傳;
offset續傳開始的位置,如果offset值為“-”,curl會自動從文件中識別起始位置開始傳輸;
-r?
–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 (SSL)告訴curl所提供證書的類型:PEM、DER、ENG等;
默認為“PEM”;
–cacert (SSL)告訴curl所以指定的CA證書文件,必須是“PEM”格式;
–capath (SSL)告訴curl所以指定目錄下的CA證書用來驗證;
這些證書必須是“PEM”格式;
–crlfile (HTTPS/FTPS)提供一個PEM格式的文件,用于指定被吊銷的證書列表;
-k
–insecure
(SSL)設置此選項將允許使用無證書的不安全SSL進行連接和傳輸。
SSL
其他
–ciphers (SSL)指定SSL要使用的加密方式;如:“aes_256_sha_256”;
–engine 設置一個OpenSSL加密引擎用于加密操作;
使用“curl –engine list”查看支持的加密引擎列表;
–random-file (SSL)指定包含隨機數據的文件路徑名;數據是用來為SSL連接產生隨機種子為;
–egd-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 (SSL/ssh)指定一個私鑰文件名;為指定時自動嘗試使用下面文件:“~/.ssh/id_rsa”、“~/.ssh/id_dsa”、“./id_rsa’”、 “./id_dsa”;
–key-type (SSL)指定私鑰文件類型,支持:DER、PEM、ENG,默認是PEM;
–pass? (SSL/SSH)指定私鑰文件的密碼;
–pubkey (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? 限制curl使用的最大帶寬;如果未指定單位,默認單位為“bytes/秒”,你也可以指定單位為“K”、“M”、“G”等單位,如:“–limit-rate 1m”為限制最大使用帶寬為“1m字節/秒”;
-y
–speed-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
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 使用指定的網卡接口訪問;
curl –interface eth0 http://aiezu.com
curl –interface 10.0.0.101 http://aiezu.com
-X?
–request
(HTTP)指定與服務器通信使用的請求方法,如:GET、PUT、POST、DELETE等,默認GET;
–keepalive-time 設置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 指定最大連接超時,單位“秒”;
-m seconds
–max-time seconds
限制整個curl操作的最長時間,單位為秒;
-s
–silent
安靜模式。不要顯示進度表或錯誤消息;
-#
–progress-bar
顯示進度條;
錯誤
選項
-f
–fail
(HTTP)連接失敗時(400以上錯誤)不返回默認錯誤頁面,而是返回一個curl錯誤碼“22”;
–retry
–retry-delay
–retry-max-time?
失敗重試次數;
重試間隔時間;
最大重試時間;
-S
–show-Error
安靜模式下顯示錯誤信息;
–stderr 錯誤信息保存文件;
輸出 -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 轉儲所有傳入和傳出的數據到文件,包括描述信息;
使用“-”作為文件名將輸出發送到標準輸出。
–trace-ascii file 轉儲所有傳入和傳出的數據到文件,包括描述信息,只轉儲ASCII部分,更容易閱讀;
使用“-”作為文件名將輸出發送到標準輸出。
這個選項會覆蓋之前使用的-v、 –verbose、 –trace-ascii選項;
–trace-time 轉儲文件中添加時間信息;
-K
–config
從配置文件中讀取參數,參考:http://curl.haxx.se/docs/
-v
–verbose
顯示更詳細的信息,調試時使用;
幫助 -M
–manual
顯示完整的幫助手冊;
-h
–help
linux curl用法幫助;

curl -h 的完整參數解釋:

-a/–append???????????????????????????? 上傳文件時,附加到目標文件
–anyauth?????????????????????????????? 可以使用“任何”身份驗證方法
–basic???????????????????????????????? 使用HTTP基本驗證
-B/–use-ascii????????????????????????? 使用ASCII文本傳輸
-d/–data ??????????????????????? HTTP POST方式傳送數據
–data-ascii ???????????????????? 以ascii的方式post數據
–data-binary ??????????????????? 以二進制的方式post數據
–negotiate???????????????????????????? 使用HTTP身份驗證
–digest??????????????????????????????? 使用數字身份驗證
–disable-eprt????????????????????????? 禁止使用EPRT或LPRT
–disable-epsv????????????????????????? 禁止使用EPSV
–egd-file ?????????????????????? 為隨機數據(SSL)設置EGD socket路徑
–tcp-nodelay?????????????????????????? 使用TCP_NODELAY選項
-E/–cert ?????????????? 客戶端證書文件和密碼 (SSL)
–cert-type ????????????????????? 證書文件類型 (DER/PEM/ENG) (SSL)
–key ???????????????????????????? 私鑰文件名 (SSL)
–key-type ?????????????????????? 私鑰文件類型 (DER/PEM/ENG) (SSL)
–pass? ????????????????????????? 私鑰密碼 (SSL)
–engine ????????????????????????? 加密引擎使用 (SSL). “–engine list” for list
–cacert ???????????????????????? CA證書 (SSL)
–capath ??????????????????? CA目?? (made using c_rehash) to verify peer against (SSL)
–ciphers ??????????????????????? SSL密碼
–compressed??????????????????????????? 要求返回是壓縮的形勢 (using deflate or gzip)
–connect-timeout ???????????? 設置最大請求時間
–create-dirs?????????????????????????? 建立本地目錄的目錄層次結構
–crlf????????????????????????????????? 上傳是把LF轉變成CRLF
–ftp-create-dirs?????????????????????? 如果遠程目錄不存在,創建遠程目錄
–ftp-method [multicwd/nocwd/singlecwd] 控制CWD的使用
–ftp-pasv????????????????????????????? 使用 PASV/EPSV 代替端口
–ftp-skip-pasv-ip????????????????????? 使用PASV的時候,忽略該IP地址
–ftp-ssl?????????????????????????????? 嘗試用 SSL/TLS 來進行ftp數據傳輸
–ftp-ssl-reqd????????????????????????? 要求用 SSL/TLS 來進行ftp數據傳輸
-F/–form ??????????????? 模擬http表單提交數據
-form-string ????????????? 模擬http表單提交數據
-g/–globoff??????????????????????????? 禁用網址序列和范圍使用{}和[]
-G/–get??????????????????????????????? 以get的方式來發送數據
-h/–help?????????????????????????????? 幫助
-H/–header ????????????????????? 自定義頭信息傳遞給服務器
–ignore-content-Length???????????????? 忽略的HTTP頭信息的長度
-i/–include??????????????????????????? 輸出時包括protocol頭信息
-I/–head?????????????????????????????? 只顯示文檔信息
-j/–junk-session-cookies?????????????? 讀取文件時忽略session cookie
–interface ???????????????? 使用指定網絡接口/地址
–krb4 ????????????????????????? 使用指定安全級別的krb4
-k/–insecure?????????????????????????? 允許不使用證書到SSL站點
-K/–config???????????????????????????? 指定的配置文件讀取
-l/–list-only????????????????????????? 列出ftp目錄下的文件名稱
–limit-rate ???????????????????? 設置傳輸速度
–local-port?????????????????????? 強制使用本地端口號
-m/–max-time ???????????????? 設置最大傳輸時間
–max-redirs ????????????????????? 設置最大讀取的目錄數
–max-filesize ????????????????? 設置最大下載的文件總量
-M/–manual???????????????????????????? 顯示全手動
-n/–netrc????????????????????????????? 從netrc文件中讀取用戶名和密碼
–netrc-optional??????????????????????? 使用 .netrc 或者 URL來覆蓋-n
–ntlm????????????????????????????????? 使用 HTTP NTLM 身份驗證
-N/–no-buffer????????????????????????? 禁用緩沖輸出
-p/–proxytunnel??????????????????????? 使用HTTP代理
–proxy-anyauth???????????????????????? 選擇任一代理身份驗證方法
–proxy-basic?????????????????????????? 在代理上使用基本身份驗證
–proxy-digest????????????????????????? 在代理上使用數字身份驗證
–proxy-ntlm??????????????????????????? 在代理上使用ntlm身份驗證
-P/–ftp-port

????????????????
使用端口地址,而不是使用PASV
-Q/–quote ??????????????????????? 文件傳輸前,發送命令到服務器
–range-file??????????????????????????? 讀取(SSL)的隨機文件
-R/–remote-time??????????????????????? 在本地生成文件時,保留遠程文件時間
–retry ?????????????????????????? 傳輸出現問題時,重試的次數
–retry-delay ???????????????? 傳輸出現問題時,設置重試間隔時間
–retry-max-time ????????????? 傳輸出現問題時,設置最大重試時間
-S/–show-error???????????????????????? 顯示錯誤
–socks4 ????????????????? 用socks4代理給定主機和端口
–socks5 ????????????????? 用socks5代理給定主機和端口
-t/–telnet-option ??????????? Telnet選項設置
–trace ????????????????????????? 對指定文件進行debug
–trace-ascii ??????????????????? Like –跟蹤但沒有hex輸出
–trace-time??????????????????????????? 跟蹤/詳細輸出時,添加時間戳
–url ???????????????????????????? Spet URL to work with
-U/–proxy-user ?????? 設置代理用戶名和密碼
-V/–version??????????????????? 顯示版本信息
-X/–request ????????? 指定什么命令
-y/–speed-time???????????????? 放棄限速所要的時間。默認為30
-Y/–speed-limit??????????????? 停止傳輸速度的限制,速度時間’秒
-z/–time-cond????????????????? 傳送時間設置
-0/–http1.0??????????????????? 使用HTTP 1.0
-1/–tlsv1????????????????????? 使用TLSv1(SSL)
-2/–sslv2????????????????????? 使用SSLv2的(SSL)
-3/–sslv3????????????????????? 使用的SSLv3(SSL)
–3p-quote????????????????????? like -Q for the source URL for 3rd party transfer
–3p-url??????????????????????? 使用url,進行第三方傳送
–3p-user?????????????????????? 使用用戶名和密碼,進行第三方傳送
-4/–ipv4?????????????????????? 使用IP4
-6/–ipv6?????????????????????? 使用IP6

三、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、下載(option:-o或者option:-O)

1.1、下載頁面:

curl -o dodo1.jpg http:www.linux.com/dodo1.JPG
#要注意-O這里后面的url要具體到某個文件,不然抓不下來
curl -O http://www.linux.com/dodo1.JPG

1.2:循環下載
有時候下載圖片可以能是前面的部分名稱是一樣的,就最后的尾椎名不一樣。這樣就會把dodo1,dodo2,dodo3,dodo4,dodo5全部保存下來
curl -O http://www.linux.com/dodo[1-5].JPG

1.3:下載重命名
在hello/dodo1.JPG的文件下載下來就會變成hello_dodo1.JPG,其他文件依此類推,從而有效的避免了文件被覆蓋
curl -o #1_#2.JPG http://www.linux.com/{hello,bb}/dodo[1-5].JPG

由于下載的hello與bb中的文件名都是dodo1,dodo2,dodo3,dodo4,dodo5。因此第二次下載的會把第一次下載的覆蓋,這樣就需要對文件進行重命名。
curl -O http://www.linux.com/{hello,bb}/dodo[1-5].JPG

1.4:分塊下載(option:-r)
curl -r 0-100 -o dodo1_part1.JPG http://www.linux.com/dodo1.JPG
curl -r 100-200 -o dodo1_part2.JPG http://www.linux.com/dodo1.JPG
curl -r 200- -o dodo1_part3.JPG http://www.linux.com/dodo1.JPG
cat dodo1_part* > dodo1.JPG ?#這樣就可以查看dodo1.JPG的內容了

1.5:通過ftp下載文件(option:-u)
curl可以通過ftp下載文件,curl提供兩種從ftp中下載的語法
curl -O -u 用戶名:密碼 ftp://www.linux.com/dodo1.JPG
curl -O ftp://用戶名:密碼@www.linux.com/dodo1.JPG

1.6:下載,顯示進度條(option:-#)或不顯示進度條(option:-s)
curl -# -O http://www.linux.com/dodo1.JPG ?
curl -s -O http://www.linux.com/dodo1.JPG

1.7、下載,斷點續傳(-C )
斷點續轉,從文件頭的指定位置開始繼續下載/上傳;offset續傳開始的位置,如果offset值為“-”,curl會自動從文件中識別起始位置開始傳輸;
curl -# -o centos6.8.iso -C – http://mirrors.aliyun.com/centos/6.8/isos/x86_64/CentOS-6.8-x86_64-minimal.iso
curl -C -O http://www.linux.com/dodo1.JPG

2、上傳文件(option:-T)

curl -T dodo1.JPG -u 用戶名:密碼 ftp://www.linux.com/img/

3、偽造來源頁面|偽造referer|盜鏈 (option:-e)

很多服務器會檢查http訪問的referer從而來控制訪問。比如:你是先訪問首頁,然后再訪問首頁中的郵箱頁面,這里訪問郵箱的referer地址就是訪問首頁成功后的頁面地址,如果服務器發現對郵箱頁面訪問的referer地址不是首頁的地址,就斷定那是個盜連了
#這樣就會讓服務器其以為你是從www.linux.com點擊某個鏈接過來的
curl -e “www.linux.com” http://mail.linux.com
#告訴愛E族,我是從百度來的
curl -e http://baidu.com http://aiezu.com

4、偽造代理設備(模仿瀏覽器)

有些網站需要使用特定的瀏覽器去訪問他們,有些還需要使用某些特定的版本。curl內置option:-A可以讓我們指定瀏覽器去訪問網站
curl -A “Mozilla/4.0 (compatible; MSIE 8.0; windows NT 5.0)” http://www.linux.com
#告訴愛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

5、設置http請求

5.1、設置http請求頭(或option:-H或option:–head)
curl -H “Cache-Control:no-cache” ?http://aiezu.com

5.2、指定proxy服務器以及其端口(option::-x)
#很多時候上網需要用到代理服務器(比如是使用代理服務器上網或者因為使用curl別人網站而被別人屏蔽IP地址的時候),幸運的是curl通過使用內置option:-x來支持設置代理
curl -x 192.168.100.100:1080 http://www.linux.com

6、http響應頭

6.1、查看http響應頭(option:-I)
# 看看本站的http頭是怎么樣的
curl -I ?http://aiezu.com
輸出:
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.2、保存http的response里面的header信息(option:-D)
curl -D cookied.txt http://www.linux.com
執行后cookie信息就被存到了cookied.txt里面了
注意:-c(小寫)產生的cookie和-D里面的cookie是不一樣的。

7、發送表單數據

curl -F “pic=@logo.png” -F “site=aiezu” ?http://aiezu.com/

8、cookie

8.1、發送cookie(option:-b)
#有些網站是使用cookie來記錄session信息。對于chrome這樣的瀏覽器,可以輕易處理cookie信息,但在curl中只要增加相關參數也是可以很容易的處理cookie
curl -b “domain=aiezu.com” ?http://aiezu.com
#很多網站都是通過監視你的cookie信息來判斷你是否按規矩訪問他們的網站的,因此我們需要使用保存的cookie信息。內置option: -b
curl -b cookiec.txt http://www.linux.com

8.2、保存http的response里面的cookie信息(option:-c)
執行后http的response里面的cookie信息就被存到了cookiec.txt里面了
curl -c cookiec.txt ?http://www.linux.com

9、測試一個網址

9.1、測試一個網址是否可達
curl -v http://www.linux.com

9.2、測試網頁返回值(option:-w [format])
curl -o /dev/NULL -s -w %{http_code} www.linux.com

10、保存訪問的網頁(>>)

2.1:使用linux的重定向功能保存
curl http://www.linux.com >> linux.html

11、請求方式

curl -i -v -H ” -X POST -d ” http:www.test.com/a/b
其中,-X POST -d, -X GET -d, -X PUT -d 分別等價于-F, -G -d, -P

以post請求為例:
11.1、 -X POST -d
(11.1.1)、POST application/x-www-form-urlencoded
application/x-www-form-urlencoded是默認的
curl -X POST -d “param1=value1&param2=value2” http://localhost:3000/data
等價于
curl -H “Content-Type:application/x-www-form-urlencoded” -X POST -d “param1=value1&param2=value2” http://localhost:3000/data
使用數據文件
curl -X POST -d “@data.txt” http://localhost:3000/data
其中data.txt內容如下:param1=value1&param2=value2

(11.1.2)、POST application/json
curl -H “Content-Type:application/json” -X POST -d ‘{“key1″:”value1″,”key2″:”value2”}’ http://localhost:3000/data
使用數據文件的話:
curl -X POST -d “@data.json” http://localhost:3000/data
其中data.json內容如下:{“key1″:”value1″,”key2″:”value2”}
再舉個例子:
curl -H “Content-type:application/json” -X POST -d “{“app_key”:”$appKey”,”time_stamp”:”$time”}” http://www.test.com.cn/a/b

11.2、-F
curl ?-v -H “Token: 222” -F “file=@/Users/fungleo/Downloads/401.png” localhost:8000/api/v1/upimg?

curl -f http://www.linux.com/error

11.3、其它舉例

(11.3.1)、

curl ?-X POST “http://www.test.com/e/f” -H “Content-Type:application/x-www-form-urlencoded;charset=UTF-8”
-d “a=b”
-d “c=d”
-d “e=f”
-d “g=h”

(11.3.2)、錯誤:curl -i -G -d “a=b#1&c=d” http://www.test.com/e/f
正確:要把參數值是特殊符號的用urlencode轉換過來
curl -i -G -d “a=b%231&c=d” http://www.test.com/e/f

12、調試

curl -v可以顯示一次http通信的整個過程,包括端口連接和http request頭信息。
如果覺得還不夠,那么下面的命令可以查看更詳細的通信過程:
curl –trace output.txt www.baidu.com 或者 curl –trace-ascii output.txt www.baidu.com?
運行后,請打開output.txt文件查看。
curl –trace output.txt ?http://www.baidu.com
curl –trace-ascii output2.txt ?http://www.baidu.com
curl –trace output3.txt –trace-time http://www.baidu.com
curl –trace-ascii output4.txt –trace-time http://www.baidu.com?

linux curl是什么

linux curl是什么

舉例:有需求每5分鐘請求一次http://www.test.com/a/b生成一個日志文件。希望一月的日志(正確的和錯誤的)能寫入一個日志文件
day=`date +%F`
logfile=’/var/logs/www.test.com_’`date +%Y%m`’.log’
/usr/bin/echo -e “nn[${day}] Start request n ” >> ${logfile}
/bin/curl?-v?“http://www.test.com/a/b” -d “ccccc” 1>> ${logfile} 2>> ${logfile}?–trace-time
/usr/bin/echo -e “nn[${day}] End requestn” >> ${logfile}

13、顯示抓取錯誤

curl?-f?http://www.linux.com/error

相關推薦:《Linux視頻教程

以上就是

? 版權聲明
THE END
喜歡就支持一下吧
點贊12 分享