【Linux】一步一步学Linux——curl命令(193)
00. 目錄
文章目錄
- 00. 目錄
- 01. 命令概述
- 02. 命令格式
- 03. 常用選項
- 04. 參考示例
- 05. 附錄
01. 命令概述
curl命令是一個利用URL規則在shell終端命令行下工作的文件傳輸工具;它支持文件的上傳和下載,所以是綜合傳輸工具,但按傳統,習慣稱curl為下載工具。
作為一款強力工具,curl支持包括HTTP、HTTPS、ftp等眾多協議,還支持POST、cookies、認證、從指定偏移處下載部分文件、用戶代理字符串、限速、文件大小、進度條等特征;做網頁處理流程和數據檢索自動化。
02. 命令格式
用法: curl [選項] [URL...]03. 常用選項
-a/--append 上傳文件時,附加到目標文件 -A/--user-agent <string> 設置用戶代理發送給服務器 -anyauth 可以使用“任何”身份驗證方法 -b/--cookie <name=string/file> cookie字符串或文件讀取位置--basic 使用HTTP基本驗證 -B/--use-ascii 使用ASCII /文本傳輸 -c/--cookie-jar <file> 操作結束后把cookie寫入到這個文件中 -C/--continue-at <offset> 斷點續轉 -d/--data <data> HTTP POST方式傳送數據--data-ascii <data> 以ascii的方式post數據--data-binary <data> 以二進制的方式post數據--negotiate 使用HTTP身份驗證--digest 使用數字身份驗證--disable-eprt 禁止使用EPRT或LPRT--disable-epsv 禁止使用EPSV -D/--dump-header <file> 把header信息寫入到該文件中--egd-file <file> 為隨機數據(SSL)設置EGD socket路徑--tcp-nodelay 使用TCP_NODELAY選項 -e/--referer 來源網址 -E/--cert <cert[:passwd]> 客戶端證書文件和密碼 (SSL)--cert-type <type> 證書文件類型 (DER/PEM/ENG) (SSL)--key <key> 私鑰文件名 (SSL)--key-type <type> 私鑰文件類型 (DER/PEM/ENG) (SSL)--pass <pass> 私鑰密碼 (SSL)--engine <eng> 加密引擎使用 (SSL). "--engine list" for list--cacert <file> CA證書 (SSL)--capath <directory> CA目錄 (made using c_rehash) to verify peer against (SSL)--ciphers <list> SSL密碼--compressed 要求返回是壓縮的形勢 (using deflate or gzip)--connect-timeout <seconds> 設置最大請求時間--create-dirs 建立本地目錄的目錄層次結構--crlf 上傳是把LF轉變成CRLF -f/--fail 連接失敗時不顯示http錯誤--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 <name=content> 模擬http表單提交數據--form-string <name=string> 模擬http表單提交數據 -g/--globoff 禁用網址序列和范圍使用{}和[] -G/--get 以get的方式來發送數據 -H/--header <line> 自定義頭信息傳遞給服務器--ignore-content-length 忽略的HTTP頭信息的長度 -i/--include 輸出時包括protocol頭信息 -I/--head 只顯示請求頭信息 -j/--junk-session-cookies 讀取文件進忽略session cookie--interface <interface> 使用指定網絡接口/地址--krb4 <level> 使用指定安全級別的krb4 -k/--insecure 允許不使用證書到SSL站點 -K/--config 指定的配置文件讀取 -l/--list-only 列出ftp目錄下的文件名稱--limit-rate <rate> 設置傳輸速度--local-port<NUM> 強制使用本地端口號 -m/--max-time <seconds> 設置最大傳輸時間--max-redirs <num> 設置最大讀取的目錄數--max-filesize <bytes> 設置最大下載的文件總量 -M/--manual 顯示全手動 -n/--netrc 從netrc文件中讀取用戶名和密碼--netrc-optional 使用 .netrc 或者 URL來覆蓋-n--ntlm 使用 HTTP NTLM 身份驗證 -N/--no-buffer 禁用緩沖輸出 -o/--output 把輸出寫到該文件中 -O/--remote-name 把輸出寫到該文件中,保留遠程文件的文件名 -p/--proxytunnel 使用HTTP代理--proxy-anyauth 選擇任一代理身份驗證方法--proxy-basic 在代理上使用基本身份驗證--proxy-digest 在代理上使用數字身份驗證--proxy-ntlm 在代理上使用ntlm身份驗證 -P/--ftp-port <address> 使用端口地址,而不是使用PASV -q 作為第一個參數,關閉 .curlrc -Q/--quote <cmd> 文件傳輸前,發送命令到服務器 -r/--range <range> 檢索來自HTTP/1.1或FTP服務器字節范圍 --range-file 讀取(SSL)的隨機文件 -R/--remote-time 在本地生成文件時,保留遠程文件時間--retry <num> 傳輸出現問題時,重試的次數--retry-delay <seconds> 傳輸出現問題時,設置重試間隔時間--retry-max-time <seconds> 傳輸出現問題時,設置最大重試時間 -s/--silent 靜默模式。不輸出任何東西 -S/--show-error 顯示錯誤--socks4 <host[:port]> 用socks4代理給定主機和端口--socks5 <host[:port]> 用socks5代理給定主機和端口--stderr <file> -t/--telnet-option <OPT=val> Telnet選項設置--trace <file> 對指定文件進行debug--trace-ascii <file> Like --跟蹤但沒有hex輸出--trace-time 跟蹤/詳細輸出時,添加時間戳 -T/--upload-file <file> 上傳文件--url <URL> Spet URL to work with -u/--user <user[:password]> 設置服務器的用戶和密碼 -U/--proxy-user <user[:password]> 設置代理用戶名和密碼 -w/--write-out [format] 什么輸出完成后 -x/--proxy <host[:port]> 在給定的端口上使用HTTP代理 -X/--request <command> 指定什么命令 -y/--speed-time 放棄限速所要的時間,默認為30 -Y/--speed-limit 停止傳輸速度的限制,速度時間04. 參考示例
4.1 獲取指定網頁的數據
[root@localhost test]# curl https://github.com/jindeng/sz14.git <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx</center> </body> </html>4.2 把輸出寫到該文件中,保留遠程文件的文件名
[root@localhost test]# curl -O https://github.com/jindeng/sz14.git% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed 100 178 100 178 0 0 326 0 --:--:-- --:--:-- --:--:-- 326 [root@localhost test]# ls sz14.git [root@localhost test]#4.3 靜默模式下載東西
[root@localhost test]# curl --silent -O https://github.com/jindeng/sz14.git [root@localhost test]#4.4 訪問需要授權的頁面時,可通過-u選項提供用戶名和密碼進行授權
[root@localhost test]# curl -uroot https://github.com/jindeng/sz14.git Enter host password for user 'root':4.5 顯示進度條
[root@localhost test]# curl --progress -O https://github.com/jindeng/sz14.git ######################################################################## 100.0% [root@localhost test]#4.6 只打印響應頭部信息
[root@localhost test]# curl -I https://github.com/jindeng/sz14.git HTTP/1.1 301 Moved Permanently Date: Fri, 30 Aug 2019 03:27:56 GMT Content-Type: text/html Content-Length: 178 Server: GitHub.com Location: https://github.com/jindeng/sz14 Vary: Accept-Encoding X-Frame-Options: DENY Vary: Accept-Encoding X-GitHub-Request-Id: 38DA:0255:A54A7D:E96FA2:5D6897BB[root@localhost test]#通過-I或者--head可以只打印出HTTP頭部信息。
4.7 用curl進行認證
[root@localhost test]# curl -u user:pwd https://github.com/jindeng/sz14.git [root@localhost test]#4.8 限制curl的下載速度
[root@localhost test]# curl URL --limit-rate 50k [root@localhost test]#使用--limit-rate限制curl的下載速度,命令中用k(千字節)和m(兆字節)指定下載速度限制。
4.9 指定可下載的最大文件大小
[root@localhost test]# curl URL --max-filesize bytes [root@localhost test]#使用--max-filesize指定可下載的最大文件大小,如果文件大小超出限制,命令則返回一個非0退出碼,如果命令正常則返回0。
4.10 用curl設置用戶代理
[root@localhost test]# curl URL --user-agent "Mozilla/5.0"#或者 [root@localhost test]# curl URL -A "Mozilla/5.0"有些網站訪問會提示只能使用IE瀏覽器來訪問,這是因為這些網站設置了檢查用戶代理,可以使用curl把用戶代理設置為IE,這樣就可以訪問了。使用--user-agent或者-A選項。
4.11 自定義頭信息傳遞給服務器
其他HTTP頭部信息也可以使用curl來發送,使用-H"頭部信息" 傳遞多個頭部信息。
[root@localhost test]# curl -H "Host:man.linuxde.net" -H "accept-language:zh-cn" URL [root@localhost test]#4.12 curl設置cookies
使用--cookie "COKKIES"選項來指定cookie,多個cookie使用分號分隔。
[root@localhost test]# curl URL --cookie "user=root;pass=123456" [root@localhost test]#將cookie另存為一個文件,使用--cookie-jar選項
[root@localhost test]# curl URL --cookie-jar cookie_file4.13 curl設置參照頁字符串
使用--referer選項指定參照頁字符串
[root@localhost test]# curl --referer URL1 URL2參照頁是位于HTTP頭部中的一個字符串,用來表示用戶是從哪個頁面到達當前頁面的,如果用戶點擊網頁A中的某個連接,那么用戶就會跳轉到B網頁,網頁B頭部的參照頁字符串就包含網頁A的URL。
4.14 斷點續傳
curl能夠從特定的文件偏移處繼續下載,它可以通過指定一個便移量來下載部分文件。
[root@localhost test]# curl URL/File -C 偏移量偏移量是以字節為單位的整數,如果讓curl自動推斷出正確的續傳位置使用-C -
[root@localhost test]# curl -C -URL4.15 抓取頁面內容到一個文件中
[root@localhost test]# curl -o git.html https://github.com/jindeng/sz14.git% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 100 178 100 178 0 0 322 0 --:--:-- --:--:-- --:--:-- 323 [root@localhost test]# ls git.html [root@localhost test]#4.16 顯示下載錯誤信息
[root@localhost test]# curl -f https://github.com/jindeng/sz14.git <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx</center> </body> </html> [root@localhost test]#4.17 通過ftp上傳
[root@localhost test]# curl -T test.sql ftp://用戶名:密碼@ip:port/demo/curtain/bbstudy_files/4.18 通過ftp下載
[root@localhost test]# curl -u 用戶名:密碼 -O URL4.19 顯示下載進度
[root@localhost test]# curl -# -O https://github.com/jindeng/sz14.git ######################################################################## 100.0% [root@localhost test]#4.20 不顯示下載進度
[root@localhost test]# curl -s -o test.git https://github.com/jindeng/sz14.git [root@localhost test]#05. 附錄
參考:【Linux】一步一步學Linux系列教程匯總
總結
以上是生活随笔為你收集整理的【Linux】一步一步学Linux——curl命令(193)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Linux】一步一步学Linux——w
- 下一篇: linux 其他常用命令