curl7 命令行使用

HTTP 協議

使用代理
curl --socks5-hostname 127.0.0.1:19996 http://www.google.com/
curl -x socks5://127.0.0.1:19996 http://www.google.com/


retry 設置重試次數
curl --url http://www.google.com/  --retry 3


-X 指定請求方法
curl -X post localhost:7777/ping 


爲特定主機和端口對提供自定義地址
curl --resolve example.com:7777:127.0.0.1 http://example.com:7777/ping


-L 跟隨重定向, 用 Location: 標頭和 3XX 響應代碼表示
curl -L 127.0.0.1:7777/ping


-v 打印請求的詳細信息,與之相反的是 -s
curl -v http://example.com

輸出比-v還詳細的信息
curl --trace dump.txt http://example.com

-w 寫入自定義信息, https://everything.curl.dev/usingcurl/verbose/writeout
curl -w "\nType: %{content_type}\nCode: %{response_code}\n" http://127.0.0.1:7777/ping
ok
Type: text/plain; charset=utf-8
Code: 200


-o, --output <file> 將輸出寫入 <file> 而不是 stdout
curl --url 127.0.0.1:7777/ping -o o.txt

如果文件存在則默認覆蓋,--no-clobber 可以避免
curl --url 127.0.0.1:7777/ping --no-clobber -o o.txt


將輸出寫入與我們獲取的遠程文件同名的本地文件。 (只使用了遠程文件的文件部分,路徑被截掉了。)
curl -O 127.0.0.1:7777/ping 


-H, --header <header/@file> 向服務器發送 HTTP 時要包含在請求中的額外標頭
curl -H "X-First-Name: Joe" 127.0.0.1:7777/ping


-I, --head (HTTP FTP 文件)只獲取標題
curl -I 127.0.0.1:7777/ping


--data-raw <data> posts data 發送原始數據
curl  --data-raw '{"name": "ajanuw"}' -H 'Content-type: application/json' 127.0.0.1:7777/ping
curl  -d @file.json -H 'Content-type: application/json' 127.0.0.1:7777/ping

-d, --data <data> 使用內容類型 application/x-www-form-urlencoded 將數據傳遞到服務器
curl -d 'name=ajanuw&age=12' 127.0.0.1:7777/ping


-F, --form <name=content> 使用 Content-Type multipart/form-data 來發布數據
curl -F [email protected] https://example.com/upload.cgi
curl -F name=John -F shoesize=11 https://example.com/

讀取文件中的內容,而不是上傳
curl -F "story=<hugefile.txt" https://example.com/

顯示設置文件名
curl -F "file=@localfile;filename=nameinpost" example.com

-G, --get 此選項將使所有使用 -d、--data、--data-binary 或 --data-urlencode 指定的數據用於 HTTP GET 請求
curl -G -d 'name=ajanuw&age=12' 127.0.0.1:7777/ping

SFTP 協議

列出目錄
curl -k "sftp://192.168.149.128" --user "root:123"
curl -k "sftp://192.168.149.128/root/" --user "root:123"

可以根據scheme猜測要使用哪個協議
curl -u root:123 sftp://192.168.149.128/root/

嵌入一個可能的用戶名和密碼字段
curl sftp://root:[email protected]/root/

指定端口號22
curl sftp://root:[email protected]:22/root/

(上傳)將本地的文件上傳到服務器的 /root/目錄下
curl -k "sftp://192.168.149.128/root/" --user "root:123" -T "./o.txt"

(上傳)目錄不存在加上 --ftp-create-dirs
curl -k "sftp://192.168.149.128/root/newdir/" --user "root:123" -T "./o.txt" --ftp-create-dirs

(下載)服務器上的/root/o.txt下載到本地u.txt
curl -k "sftp://192.168.149.128/root/o.txt" --user "root:123" -o "./u.txt"

(下載)使用-o選項當目錄不存在時可以使用--create-dirs創建目錄
curl -k "sftp://192.168.149.128/root/o.txt" --user "root:123" -o "./d/u.txt" --create-dirs

-Q, --quote 向遠程 FTP 或 SFTP 服務器發送任意命令

要在成功傳輸後執行命令,請在它們前面加上破折號“-”

如果服務器爲其中一個命令返回失敗,則整個操作將中止

該選項可以多次使用。 當與 FTP 服務器通話時,在命令前加上星號 (*) 以使 curl 繼續,即使命令失敗也是如此,因爲默認情況下 curl 將在第一次失敗時停止。

SFTP 引用命令的列表:

chgrp group file
  chgrp 命令將文件操作數命名的文件的組 ID 設置爲組操作數指定的組 ID。 組操作數是十進制整數組 ID

chmod mode file
  chmod命令修改指定文件的文件模式位。模式操作數是八進制整數模式數

chown user file
  chown命令將文件操作數命名的文件的所有者設置爲用戶操作數指定的用戶ID。用戶操作數是十進制整數用戶ID

ln source_file target_file
  ln和symlink命令在target_file位置創建指向source_file位置的符號鏈接

mkdir directory_name
 mkdir命令創建由directory_name操作數命名的目錄

創建 /root/sftp_newdir/ 目錄
curl -k "sftp://192.168.149.128/root/" --user "root:123" -Q '-mkdir sftp_newdir'

pwd    
  pwd命令返回當前工作目錄的絕對路徑名

rename source target
 rename命令將源操作數命名的文件或目錄重命名爲目標操作數指定的目標路徑

從命名服務器上/root/目錄下的文件
curl -k "sftp://192.168.149.128/root/" --user "root:123" -Q '-rename o.txt newname.txt'
curl -k "sftp://192.168.149.128/root/" --user "root:123" -Q '-rename newdir/o.txt newdir/newname.txt'

rm file
 rm命令刪除文件操作數指定的文件

rmdir directory
 rmdir命令刪除由目錄操作數指定的目錄條目,前提是該條目爲空

刪除 /root/sftp_newdir/ 目錄
curl -k "sftp://192.168.149.128/root/" --user "root:123" -Q '-rmdir sftp_newdir'

symlink source_file target_file
        文件連接

FILE 協議

FILE實際上不是“網絡”協議。它是一種URL方案,允許您告訴curl從本地文件系統獲取文件,而不是通過網絡從遠程服務器獲取文件

curl file:///root/oo/README.md

使用配置文件

避免控制檯輸入太長

curl -K curl_config.txt

cat curl_config.txt
-X POST
--url 127.0.0.1:7777/ping
-d 'msg=hello'

電子郵件協議

  • (讀)POP3 從服務器檢索電子郵件的協議, POP3服務器和客戶端使用TCP端口110。雖然到服務器的連接以明文形式開始,但客戶端可以使用STLS命令顯式請求升級連接,從而支持SSL/TLS通信

  • (讀)POP3S 是通過SSL/TLS連接進行的POP3。這樣的連接隱式地使用SSL/TLS開始,因此服務器和客戶端使用TCP端口995來彼此通信

  • (讀)IMAP 是一種用於訪問、控制和“讀取”電子郵件的協議, IMAP服務器和客戶端使用TCP端口143。雖然到服務器的連接以明文形式開始,但客戶端可以使用STARTTLS命令顯式請求升級連接,從而支持SSL/TLS通信

  • (讀)IMAPS 是通過SSL/TLS連接完成的IMAP。這樣的連接隱式地使用SSL/TLS開始,因此服務器和客戶端使用TCP端口993彼此通信

  • (寫)SMTP 是一種電子郵件傳輸協議, SMTP服務器和客戶端使用TCP端口25。雖然到服務器的連接以明文形式開始,但客戶端可以使用STARTTLS命令顯式請求升級連接,從而支持SSL/TLS通信

  • (寫)SMTPS 是通過SSL/TLS連接完成的SMTP。這樣的連接隱式地使用SSL/TLS開始,並且這樣的服務器和客戶端使用TCP端口465來彼此通信

curl pop3://<host> -u '<用戶名>:<密碼>'

curl --resolve example.com:110:127.0.0.1  pop3://lisi.example.com/ -u '[email protected]:123'
1 1608
2 1496
3 327


下載第3條消息,通常最新的會排在最後面
curl --resolve example.com:110:127.0.0.1  pop3://lisi.example.com/3 -u '[email protected]:123'


刪除消息1
curl  pop3://smtp.qq.com/1 -u '[email protected]:123' --ssl --request DELE


從 mailbox Drafts中獲取索引爲1的郵件
curl 'imap://smtp.qq.com/Drafts;MAILINDEX=1' -u '[email protected]:123' --ssl

See also:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章