Linux curl命令簡介

在Linux中curl是一個利用URL規則在命令行下工做的文件傳輸工具,支持的協議包括 (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP),curl設計爲無用戶交互下完成工做;php


  curl提供了一大堆很是有用的功能,包括代理訪問、用戶認證、ftp上傳下載、HTTP POST、SSL鏈接、cookie支持、斷點續傳...html


語法:curl [option] [url]
json

常見參數:瀏覽器

[root@localhost src]# curl www.baidu.com|iconv -fgb2312

執行後,www.baidu.com 的html就會顯示在屏幕上了,這個用法常常用於測試一臺服務器是否能夠到達一個網站,如發現亂碼,能夠使用iconv轉碼安全


-I/--head 僅返回頭部信息,使用HEAD請求:curl -I http://www.baidu.combash


保存訪問的網頁服務器

-o:將文件保存爲命令行中指定的文件名的文件中
-O:使用URL中默認的文件名保存文件到本地,這裏
後面的url要具體到某個文件,否則抓不下來cookie

[root@localhost src]# curl www.baidu.com >> baidu.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2381  100  2381    0     0   100k      0 --:--:-- --:--:-- --:--:--  105k
[root@localhost src]# curl -o baidu.com www.baidu.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2381  100  2381    0     0  81602      0 --:--:-- --:--:-- --:--:-- 82103
[root@localhost src]# curl -O www.baidu.com
curl: Remote file name has no length!
curl: try 'curl --help' or 'curl --manual' for more information
[root@localhost src]# curl -O www.baidu.com/index.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2381  100  2381    0     0   102k      0 --:--:-- --:--:-- --:--:--  105k
[root@localhost src]#


測試網頁返回值網絡

-s/--silent    靜音模式。不輸出任何東西app

-w/--write-out [format]    什麼輸出完成後

[root@localhost src]# curl -o /dev/null -s -w "%{http_code}" www.baidu.com
200[root@localhost src]# curl -o /dev/null -s -w "%{http_code}\n" www.baidu.com
200
[root@localhost src]# curl -o /dev/null -s -w "time_total: %{time_total}\n" "http://www.baidu.com"
time_total: 0.024
[root@localhost src]#

在腳本中,這是很常見的測試網站是否正常的用法


指定proxy服務器以及其端口

-x/--proxy <host[:port]>    在給定的端口上使用HTTP代理

[root@localhost src]# curl -x 192.168.100.198:8888 http://www.baidu.com


cookie

  • -c/--cookie-jar <file>    把cookie寫入到這個文件中

[root@Super ~]# curl -d"name=123&password=456" http://192.168.100.182/index.php -v -c ./cookie

使用用戶名和密碼登陸系統,並將cookie信息存儲在當前目錄的cookie文件中

  • -D/--dump-header <file>   把header信息寫入到該文件中

  • -b/--cookie <name=string/file>    cookie字符串或文件讀取位置

[root@Super ~]# curl http://192.168.100.182/index.php -v -b ./cookie
  • ‘-cookie’直接指定cookie

[root@Super ~]# curl --cookie "name=123" http://192.168.100.182/index.php -v


模仿瀏覽器

-A/--user-agent <string>    設置用戶代理髮送給服務器

[root@localhost src]# curl -A "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0)" http://www.baidu.com

服務器端就會認爲是使用IE8.0去訪問的


僞造referer(盜鏈)

-e/--referer    來源網址

不少服務器會檢查http訪問的referer從而來控制訪問。好比:你是先訪問首頁,而後再訪問首頁中的郵箱頁面,這裏訪問郵箱的referer地址就是訪問首頁成功後的頁面地址,若是服務器發現對郵箱頁面訪問的referer地址不是首頁的地址,就判定那是個盜連了

[root@localhost src]# curl -e "www.baidu.com" http://image.baiud.com

這樣就會讓服務器其覺得你是從www.baidu.com點擊某個連接訪問image.baidu.com的


下載文件

[root@localhost src]# curl -o 51.png https://s1.51cto.com/wyfs02/M00/8D/49/wKioL1iV9-6wk8YsAACLSADynaw310.png
[root@localhost src]# curl -O https://s1.51cto.com/wyfs02/M00/8D/49/wKioL1iV9-6wk8YsAACLSADynaw310.png

循環下載

[root@localhost src]# curl -O http://www.51cto.com/justin[1-5].png

justin1.png-justin5.png所有下載下來


下載重命名

[root@localhost src]# curl -O http://www.51cto.com/{justin,peng}/justin[1-5].png
[root@localhost src]# curl -o #1_#2.png http://www.51cto.com/{justin,peng}/justin[1-5].png

第一條命令會先去下載justin下的justin1-justin5.png文件,而後再下載peng下的justin1-justin5.png文件,這樣就會把以前下載的文件覆蓋,

第二條命令下載時候重命名成justin_justin1.png justin_justin2.png的形式


分塊下載

-r/--range <range>    檢索來自HTTP/1.1或FTP服務器字節範圍

有時候下載的東西會比較大,這個時候咱們能夠分段下載

[root@localhost src]# curl -r 0-100 -o justin_part1.png http://www.51cto.com/justin.png 
[root@localhost src]# curl -r 1000-200 -o justin_part2.png http://www.51cto.com/justin.png 
[root@localhost src]# curl -r 200- -o justin_part3.png http://www.51cto.com/justin.png 
[root@localhost src]# cat justin_part* > justin.png


經過ftp下載文件

-u/--user <user[:password]>    設置服務器的用戶和密碼

-E 採用證書認證

[root@localhost src]# curl -O -u justin:peng ftp://www.51cto.com/justin.png
[root@localhost src]# curl -O ftp://justin:peng@www.51cto.com/justin.png
[root@localhost src]# curl -E cert.pem https://www.51cto.com/justin.png


下載進度條

-#/--progress-bar    進度條顯示當前的傳送狀態

[root@localhost src]# curl -# -O http://www.baidu.com/justin.png

-s 不會顯示下載進度信息

[root@localhost src]# curl -s -O http://www.baidu.com/justin.png


斷點續傳

-C/--continue-at <offset>    斷點續轉

[root@localhost src]# curl -C -O http://www.baidu.com/justin.png


上傳文件

-T/--upload-file <file>    上傳文件

[root@localhost src]# curl -T justin.png -u justin:peng ftp://www.baidu.com/img/


顯示抓取錯誤

-f/--fail    鏈接失敗時不顯示http錯誤

[root@localhost src]# curl -f http://www.baidu.com/error


-F表單提交操做

    -F命令以Content-Type:multipart/form-data的形式向serverpost數據,該命令容許提交二進制文件等。
    能夠使用@前綴來制定提交的內容爲一個文件,也能夠使用<符號來提交文件中的內容

curl -F prefile=@portrait.jpg https://example.com/upload.cgi

向服務器上傳一個圖片,圖片的表單域名爲profile,內容爲protrait.jpg的二進制


-B/--use-ascii    使用ASCII文本傳輸

-X 指定請求方式

在瀏覽器輸入一個網址訪問網站都是GET請求,在FORM表單中,能夠經過設置Method指定提交方式爲GET或者POST提交方式,默認爲GET提交方式。    
HTTP定義了與服務器交互的不一樣方法,其中最基本的四種:GET,POST,PUT,DELETE,HEAD
GET和HEAD被稱爲安全方法,由於使用GET和HEAD的HTTP請求不會產生什麼動做。不會產生動做意味着GET和HEAD的HTTP請求不會在服務器上產生任何結果。可是安全方法並非什麼動做都不產生,這裏的安全方法僅僅指不會修改信息。
GET請求,請求的數據會附加在URL以後,以?分割URL和傳輸數據,多個參數用&鏈接。URL的編碼格式採用的是ASCII編碼,而不是uniclde,便是說全部的非ASCII字符都要編碼以後再傳輸。
POST請求會把請求的數據放置在HTTP請求包的包體中

POST請求

-d/--data <data>    HTTP POST方式傳送數據,

[root@Super ~]# curl -X POST -d"name=123&password=456" http://192.168.100.182/index.php -v

-d選項爲使用POST方式向server發送數據,所以在使用-d的時候,能夠省略-X POST。使用-d時,將使用Content-type:application/x-www-form-urlencoded方式發送數據。使用JSON形式post數據,能夠使用-H指定頭部類型:

[root@Super ~]# curl -X POST -H "Content-Type:application/json" -d '{"data":"123","key":"456"}' http://192.168.100.182/index.php -v

當提交的參數值中有特殊字符就須要先轉義,如空格時,就須要轉義成%20;--data-urlencode能夠自動轉義特殊字符,無需人工事先轉義

[root@Super ~]# curl --data-urlencode "value 1" http://192.168.100.182/index.php -v

--data-urlencode參數會自動轉義特殊字符,如上面的空格

請求的時候帶上Cookie:

[root@Super ~]# curl -X POST -H "Cookie:username=123"  http://192.168.100.182/index.php -v


    --data-ascii <data>    以ascii的方式post數據

    --data-binary <data>    以二進制的方式post數據

    --connect-timeout <seconds> 設置最大請求時間

    --create-dirs    創建本地目錄的目錄層次結構

GET請求

-G/--get     以get的方式來發送數據

[root@Super ~]# /usr/bin/curl -G -d "username=justin&pwd=123456" "
[root@Super ~]# curl -X GET http://www.baidu.com

在訪問須要受權的頁面時,可經過-u選項提供用戶名和密碼進行受權;curl -u username:password URL

[root@Super ~]# curl -u ywbz http://192.168.100.127
Enter host password for user 'ywbz':


DELETE請求

[root@Super ~]# curl -I -X DELETE http://192.168.100.182/index.php -v


--interface <interface>     使用指定網絡接口/地址

-l/--list-only     列出ftp目錄下的文件名稱

--limit-rate <rate>     設置傳輸速度

curl --limit-rate 1000B -O http://192.168.100.182/index.php


--retry <num> 傳輸出現問題時,重試的次數

--retry-delay <seconds>    傳輸出現問題時,設置重試間隔時間

--retry-max-time <seconds>    傳輸出現問題時,設置最大重試時間


-S/--show-error    顯示錯誤

-y/--speed-time    放棄限速所要的時間。默認爲30

-Y/--speed-limit    中止傳輸速度的限制,速度時間'秒

-z/--time-cond    傳送時間設置

curl -z 11-Oct-18 http://192.168.100.182/index.php

若文件index.php在2018-10-11以後遊過更新纔會下載

-0/--http1.0    使用HTTP 1.0

-1/--tlsv1    使用TLSv1(SSL)

-2/--sslv2    使用SSLv2的(SSL)

-3/--sslv3    使用的SSLv3(SSL)

相關文章
相關標籤/搜索