天天學一個 Linux 命令(73):curl

命令簡介

curl 命令使用 HTTP、HTTPS、FTP、FTPS、SCP、SFTP、TFTP、DICT、TELNET、LDAP或FILE支持的協議之一,將數據傳輸到網絡服務器或從網絡服務器傳輸數據。它很是適合在 Shell 腳本中使用。html

curl 命令提供代理支持,用戶身份驗證,FTP 上傳,HTTP 發佈,SSL 鏈接,cookie,文件傳輸恢復,metalink 和其餘功能。很是值得一看的 Curl 用法指南linux

語法格式

curl [options] [URL...]

選項說明

-A  #用戶代理
-b  #發送 Cookie 信息
-c  #將 Cookie 寫入文件
-d  #發送 POST 請求的數據體
-e  #設置 HTTP 的標頭 Referer 字段
-F  #向服務器上傳文件
-G  #構造 URL 的查詢字符串
-H  #添加 HTTP 請求的標頭
-i  #打印服務器迴應的 HTTP 標頭
-I  打印服務器迴應的 HEAD 標頭
-k  #跳過 SSL 檢測
-L  #跟隨服務器的重定向
–limit-rate   #限制請求和迴應的帶寬
-o  #將服務器的迴應保存成文件(下載文件,而後從新命名)
-O  #將服務器的迴應保存成文件(下載多個文件)
-s  #不輸出錯誤和進度信息
-S  #只輸出錯誤信息
-u  #設置認證的用戶名和密碼
-v  #打印調試信息
-x  #設置請求代理
-X  #指定請求的方法

應用舉例

打印版本信息nginx

[root@CentOS7-1 ~]# curl --version
curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.44 zlib/1.2.7 libidn/1.28 libssh2/1.8.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp 
Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz unix-sockets

下載文件git

[root@CentOS7-1 download]# curl -O http://nginx.org/download/nginx-1.18.0.zip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1671k  100 1671k    0     0   300k      0  0:00:05  0:00:05 --:--:--  414k
[root@CentOS7-1 download]# ll
total 1672
-rw-r--r-- 1 root root 1711619 Mar 13 09:29 nginx-1.18.0.zip
#將下載下來的文件從新更名成新的指定的文件名
[root@CentOS7-1 download]# curl -o $(date +%F)_download_nginx  http://nginx.org/download/nginx-1.18.0.zip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1671k  100 1671k    0     0   480k      0  0:00:03  0:00:03 --:--:--  480k
[root@CentOS7-1 download]# ll
total 5016
-rw-r--r-- 1 root root 1711619 Mar 13 09:31 2021-03-13_download_nginx
-rw-r--r-- 1 root root 1711619 Mar 13 09:30 F_download_nginx
-rw-r--r-- 1 root root 1711619 Mar 13 09:29 nginx-1.18.0.zip

POST請求舉例github

[root@CentOS7-1 ~]# curl http://192.168.1.199
this is a www web stie
#顯示所有信息
[root@CentOS7-1 ~]# curl -i http://192.168.1.199
HTTP/1.1 200 OK
Date: Wed, 10 Mar 2021 17:05:04 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Wed, 10 Mar 2021 17:03:20 GMT
ETag: "17-5bd31a4e35769"
Accept-Ranges: bytes
Content-Length: 23
Content-Type: text/html; charset=UTF-8
this is a www web stie
#只顯示頭部信息
[root@CentOS7-1 ~]# curl -l http://192.168.1.199
this is a www web stie
#顯示get請求全過程解析信息
[root@CentOS7-1 ~]# curl -v http://192.168.1.199
* About to connect() to 192.168.1.199 port 80 (#0)
*   Trying 192.168.1.199...
* Connected to 192.168.1.199 (192.168.1.199) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.1.199
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Wed, 10 Mar 2021 17:05:16 GMT
< Server: Apache/2.4.6 (CentOS)
< Last-Modified: Wed, 10 Mar 2021 17:03:20 GMT
< ETag: "17-5bd31a4e35769"
< Accept-Ranges: bytes
< Content-Length: 23
< Content-Type: text/html; charset=UTF-8
< 
this is a www web stie
* Connection #0 to host 192.168.1.199 left intact

登陸服務器web

[root@CentOS7-1 ~]# curl -u mingongge:passwdPassWd  https://github.com/mingongge/
[root@CentOS7-1 ~]# curl -u mingongge  https://github.com/mingongge/
Enter host password for user 'mingongge':

上傳文件(如上傳文件到文件服務器)服務器

[root@CentOS7-1 ~]# curl -T user1.png ftp://username:password@ip:port/www/web/user_download/

打印下載進度條cookie

[root@CentOS7-1 ~]# curl -# -O http://nginx.org/download/nginx-1.18.0.zip
######################################################################## 100.0%

天天學一個 Linux 命令(71):traceroute網絡

天天學一個 Linux 命令(72):tcpdumpssh

相關文章
相關標籤/搜索