curl命令的用法

本文介绍了curl命令的各种用法,包括显示通信过程、请求和响应头信息、保存网页、处理自动跳转、显示详细通信过程以及如何控制输出内容。通过实例展示了如何通过curl获取网站的IP、端口、安全证书、请求头、响应头等信息,并演示了如何过滤和重定向输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击个人博客,查看更多文章https://elonjelinek.github.io/

在shell终端中进行测试

显示通信过程:加-v

示例:curl -v www.baidu.com/

sszxr:~ sszxr$ curl -v www.baidu.com/
 1.   Trying 14.215.177.38...
 2. TCP_NODELAY set
 3. Connected to www.baidu.com (14.215.177.38) port 80 (#0)
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: Keep-Alive
< Content-Length: 2381
< Content-Type: text/html
< Date: Fri, 11 Jan 2019 10:14:10 GMT
< Etag: "588604dc-94d"
< Last-Modified: Mon, 23 Jan 2017 13:27:56 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
< 
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
 4. Connection #0 to host www.baidu.com left intact
sszxr:~ sszxr$ 

这里的输出结果包含四部分内容:

第一部分:通信过程

显示了网站的真实IP地址,端口号

1.   Trying 14.215.177.38...
2. TCP_NODELAY set
3. Connected to www.baidu.com (14.215.177.38) port 80 (#0)
第二部分:request请求头信息

请求的方法,HTTP协议版本,域名,用户代理

> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.54.0
> Accept: */*
第三部分:response响应头信息
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: Keep-Alive
< Content-Length: 2381
< Content-Type: text/html
< Date: Fri, 11 Jan 2019 10:14:10 GMT
< Etag: "588604dc-94d"
< Last-Modified: Mon, 23 Jan 2017 13:27:56 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
第四部分:网页代码
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道…………略略略……………

如果请求的是https开头的完整网址,还会显示网站的安全证书信息

示例:curl -v https://www.baidu.com/

kdpjlnk:~ kdpjlnk$ curl -v https://www.baidu.com/
*   Trying 14.215.177.38...
* TCP_NODELAY set
* Connected to www.baidu.com (14.215.177.38) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/cert.pem
  CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: C=CN; ST=beijing; L=beijing; OU=service operation department; O=Beijing Baidu Netcom Science Technology Co., Ltd; CN=baidu.com
*  start date: Apr  3 03:26:03 2017 GMT
*  expire date: May 26 05:31:02 2019 GMT
*  subjectAltName: host "www.baidu.com" matched cert's "*.baidu.com"
*  issuer: C=BE; O=GlobalSign nv-sa; CN=GlobalSign Organization Validation CA - SHA256 - G2
*  SSL certificate verify ok.
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: Keep-Alive
< Content-Length: 2443
< Content-Type: text/html
< Date: Tue, 23 Oct 2017 04:32:00 GMT
< Etag: "58860401-98b"
< Last-Modified: Mon, 23 Jan 2017 13:24:17 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
< 
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');
                </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
* Connection #0 to host www.baidu.com left intact
kdpjlnk:~ kdpjlnk$ 

这一次的输出结果包含五部分信息,分别是:
通信过程,安全证书信息,请求头,响应头,网站代码。

只显示响应头和网站代码:-i

示例 curl -i https://www.mi.com

sszxr:~ sszxr$ curl -i https://www.mi.com
HTTP/1.1 200 OK
Expires: Fri, 11 Jan 2019 11:08:55 GMT
Date: Fri, 11 Jan 2019 11:06:55 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 324749
Last-Modified: Fri, 11 Jan 2019 11:05:01 GMT
ETag: "5c38785d-4f48d"
Accept-Ranges: bytes
Cache-Control: max-age=120
X-Cacheable: MI-WWW-Cacheable
Server: MIFE/3.0
X-Frame-Options: SAMEORIGIN
Cache_state: 2
Cache_state: 0
Cache_state: 2
X-Via: 1.1 PSjsczBGP2ay152:5 (Cdn Cache Server V2.0), 1.1 qzhdx44:8 (Cdn Cache Server V2.0), 1.1 gzhdx14:2 (Cdn Cache Server V2.0)
Connection: keep-alive

<!doctype html>
<html lang="zh-CN" xml:lang="zh-CN">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta charset="UTF-8" />
………………………………略略略…………………………………

只显示网站代码:-l

示例:curl -l https://www.mi.com

sszxr:~ sszxr$ curl -l https://www.mi.com
<!doctype html>
<html lang="zh-CN" xml:lang="zh-CN">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta charset="UTF-8" />
<title>小米商城 - 小米MIX 3、红米Note 7、小米8、小米电视官方网站</title>
<meta name="description" content="小米商城直营小米公司旗下所有产品,囊括小米手机系列小米MIX 3、小米8、小米Note 3,红米手机系列红米Note 7、红米6 Pro,智能硬件,配件及小米生活周边,同时提供小米客户服务及售后支持。" />
<meta name="keywords" content="小米,小米8,小米Note3,红米Note7,小米MIX3,小米商城" />
<meta name="viewport" content="width=1226" />
<meta http-equiv="x-dns-prefetch-control" content="on">
<link rel="dns-prefetch" href="//s01.mifile.cn" />
<link rel="dns-prefetch" href="//c1.mifile.cn" />
<link rel="dns-prefetch" href="//i3.mifile.cn" />
<link rel="dns-prefetch" href="//i2.mifile.cn" />
<link rel="dns-prefetch" href="//i1.mifile.cn" />
<link rel="dns-prefetch" href="//i8.mifile.cn" />
<link rel="dns-prefetch" href="//v.mifile.cn" />
<link rel="dns-prefetch" href="//a.huodong.mi.cn" />

<link rel="shortcut icon" href="//s01.mifile.cn/favicon.ico" type="image/x-icon" />
…………………………………………略略略……………………………………………

保存网页:-o

示例:curl -o ~/Desktop/baidu.html https://www.baidu.com

kdpjlnk:~ kdpjlnk$ curl -o ~/Desktop/baidu.html https://www.baidu.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2443  100  2443    0     0   9466      0 --:--:-- --:--:-- --:--:--  9432
kdpjlnk:~ kdpjlnk$ 

这样,桌面生成了一个名为:baidu.html的文件,打开可以看见是百度的首页。

自动跳转 : -L

示例:curl -L www.sina.com

curl -L www.sina.com

这样会自动跳转到https://www.sina.com.cn/

显示详细通信过程:--trace

示例:

curl --trace ~/Desktop/baidu.txt www.baidu.com
或者curl --trace-ascii ~/Desktop/baidu.txt www.baidu.com

打开桌面文件baidu.txt会发现里面的信息非常多。

只显示或不显示网站代码

curl命令的输出结果被shell终端分成了两部分:

标准错误
运行结果

【网站代码】部分相当于curl命令的运行结果,而【其余部分】相当于标准错误
如果想要查看详细的通信过程,却不想要网站代码(实在太长了),可以将网站代码部分输出到一个文件,或者直接舍弃,不让它输出
示例:curl -v https://www.mi.com > /dev/null
> /dev/null 表示将命令的运行结果丢弃,
>是重定向
/dev/null是Linux文件系统的一个空文件,输入到该文件的所有内容会被丢弃。

sszxr:~ sszxr$ curl -v https://www.mi.com > /dev/null
* Rebuilt URL to: https://www.mi.com/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 113.96.172.80...
* TCP_NODELAY set
* Connected to www.mi.com (113.96.172.80) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/cert.pem
  CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
} [512 bytes data]
* TLSv1.2 (IN), TLS handshake, Server hello (2):
{ [115 bytes data]
* NPN, negotiated HTTP1.1
* TLSv1.2 (IN), TLS handshake, Certificate (11):
{ [5118 bytes data]
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
{ [333 bytes data]
* TLSv1.2 (IN), TLS handshake, Server finished (14):
{ [4 bytes data]
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
} [70 bytes data]
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
} [1 bytes data]
* TLSv1.2 (OUT), TLS handshake, Unknown (67):
} [36 bytes data]
* TLSv1.2 (OUT), TLS handshake, Finished (20):
} [16 bytes data]
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
{ [1 bytes data]
* TLSv1.2 (IN), TLS handshake, Finished (20):
{ [16 bytes data]
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server did not agree to a protocol
* Server certificate:
*  subject: OU=Domain Control Validated; CN=www.mi.com
*  start date: Dec  7 07:03:32 2018 GMT
*  expire date: Dec  6 02:44:28 2019 GMT
*  subjectAltName: host "www.mi.com" matched cert's "www.mi.com"
*  issuer: C=US; ST=Arizona; L=Scottsdale; O=GoDaddy.com, Inc.; OU=http://certs.godaddy.com/repository/; CN=Go Daddy Secure Certificate Authority - G2
*  SSL certificate verify ok.
> GET / HTTP/1.1
> Host: www.mi.com
> User-Agent: curl/7.54.0
> Accept: */*
> 
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0< HTTP/1.1 200 OK
< Expires: Fri, 11 Jan 2019 11:25:42 GMT
< Date: Fri, 11 Jan 2019 11:23:42 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 324749
< Last-Modified: Fri, 11 Jan 2019 11:23:01 GMT
< ETag: "5c387c95-4f48d"
< Accept-Ranges: bytes
< Cache-Control: max-age=120
< X-Cacheable: MI-WWW-Cacheable
< Server: MIFE/3.0
< X-Frame-Options: SAMEORIGIN
< Cache_state: 2
< Cache_state: 2
< X-Via: 1.1 qzhdx44:8 (Cdn Cache Server V2.0), 1.1 PSgdmmdx4cj23:7 (Cdn Cache Server V2.0)
< Connection: keep-alive
< 
{ [4096 bytes data]
100  317k  100  317k    0     0   279k      0  0:00:01  0:00:01 --:--:--  279k
* Connection #0 to host www.mi.com left intact
sszxr:~ sszxr$ 

这样就只显示了通信过程,证书信息,请求头信息,响应头信息,而网站代码被丢弃了。

类似的curl -v https://www.mi.com 2> /dev/null相当于将标准错误丢弃,而只输出运行结果,
所以
curl -v https://www.mi.com 2> /dev/null
curl -l https://www.mi.com
这两个命令的输出结果是一样的,都只输出了网站代码。

下面再看看
curl -i https://www.mi.com > /dev/null
没有任何结果

sszxr:~ sszxr$ curl -i https://www.mi.com > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  317k  100  317k    0     0   379k      0 --:--:-- --:--:-- --:--:--  379k
sszxr:~ sszxr$ 

curl -i https://www.mi.com 2> /dev/null

sszxr:~ sszxr$ curl -i https://www.mi.com 2> /dev/null
HTTP/1.1 200 OK
Expires: Fri, 11 Jan 2019 11:38:59 GMT
Date: Fri, 11 Jan 2019 11:36:59 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 324749
Last-Modified: Fri, 11 Jan 2019 11:35:01 GMT
ETag: "5c387f65-4f48d"
Accept-Ranges: bytes
Cache-Control: max-age=120
X-Cacheable: MI-WWW-Cacheable
Server: MIFE/3.0
X-Frame-Options: SAMEORIGIN
Cache_state: 0
Cache_state: 2
X-Via: 1.1 qzhdx44:8 (Cdn Cache Server V2.0), 1.1 gzhdx14:3 (Cdn Cache Server V2.0)
Connection: keep-alive

<!doctype html>
<html lang="zh-CN" xml:lang="zh-CN">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta charset="UTF-8" />
<title>小米商城 - 小米MIX 3、红米Note 7、小米8、小米电视官方网站</title>
<meta name="description" content="小米商城直营小米公司旗下所有产品,囊括小米手机系列小米MIX 3、小米8、小米Note 3,红米手机系列红米Note 7、红米6 Pro,智能硬件,配件及小米生活周边,同时提供小米客户服务及售后支持。" />
<meta name="keywords" content="小米,小米8,小米Note3,红米Note7,小米MIX3,小米商城" />
<meta name="viewport" content="width=1226" />
<meta http-equiv="x-dns-prefetch-control" content="on">
<link rel="dns-prefetch" href="//s01.mifile.cn" />
<link rel="dns-prefetch" href="//c1.mifile.cn" />
<link rel="dns-prefetch" href="//i3.mifile.cn" />
<link rel="dns-prefetch" href="//i2.mifile.cn" />
<link rel="dns-prefetch" href="//i1.mifile.cn" />
<link rel="dns-prefetch" href="//i8.mifile.cn" />
<link rel="dns-prefetch" href="//v.mifile.cn" />
<link rel="dns-prefetch" href="//a.huodong.mi.cn" />
…………………………………………略略略略…………………………………………

命令
curl -i https://www.mi.com 2> /dev/null
curl -i https://www.mi.com
的输出结果一模一样,说明【响应头】和【网站代码】都被当成了运行结果,而其他部分相当于标准错误。

指定输出特定内容:

sszxr:~ sszxr$ curl -o /dev/null -s -w "%{http_code}\n" "http://www.baidu.com"
200
ElonJelinek:~ ElonJelinek$ curl -o /dev/null -s -w "time_total: %{time_total}\n" "http://www.baidu.com"
time_total: 0.390102
sszxr:~ sszxr$ 
参考文章:

Linux curl命令详解
点击个人博客,查看更多文章https://elonjelinek.github.io/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值