curl常用命令选项详解-Linux运维管理

一、概念

curl命令是一个利用URL规则在命令行下工作的文件传输工具。它支持文件的上传和下载,支持包括HTTP、HTTPS、ftp等众多协议,还支持POST、cookies、认证、从指定偏移处下载部分文件、用户代理字符串、限速、文件大小、进度条等特征。

二、语法格式

1.  curl [选项] URL

三、curl常用选项

1.不带选项,返回网站源码

[root@test1 ~]$curl www.sina.com
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

2.-A / --user-agent [string],设置HTTP Request头部的user-agent,通过curl访问网站的默认user-agent是"curl/版本号"

[root@test1 ~]$curl -A 'xxxxx ' www.baidu.com

 

默认user-agent

 

 

3. -e / --referer [string],设置HTTP Request头部的referer,既来源网站

[root@test1 ~]$curl -e  'referer:www.bing.com' www.baidu.com

 4.-H / --header <line>,添加自定义的HTTP头部

[root@test1 ~]$curl -H 'testHeader:test123456' www.baidu.com

5.-l / --list-only ,列出ftp目录下的文件名称

[root@test1 ~]$curl -l xxx.xxxxx.xxx/pub/

6.-s / --silent,不输出任何内容

[root@test1 ~]$curl -s www.baidu.com

7.-u / --user  user[:password], 指定服务器认证的用户名、密码

8.-U / --proxy-user  user[:password] ,指定代理认证的用户名、密码

9.-w / --write-out [format],完成后输出什么

[root@test1 ~]$curl -o /dev/null www.baidu.com -w 'Hello!'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2381  100  2381    0     0  32560      0 --:--:-- --:--:-- --:--:-- 33069
Hello!

10. -o / --output,将输出写入文件

11.-v / --verbose,显示详细的操作信息

12.-T / --upload-file FILE ,将文件上传到指定位置

13.-x / --proxy   [protocol://]host[:port],在指定端口上使用代理

14.-a / append , 添加要上传的文件

15.-L ,自动跳转到(重定向的)新网址

[root@test1 ~]$curl -vLo /dev/null www.sina.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* About to connect() to www.sina.com port 80 (#0)
*   Trying 117.21.216.80...
* Connected to www.sina.com (117.21.216.80) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.sina.com
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Server: nginx
< Date: Sat, 28 Mar 2020 10:55:10 GMT
< Content-Type: text/html
< Content-Length: 178
< Connection: keep-alive
< Location: http://www.sina.com.cn/
< Expires: Sat, 28 Mar 2020 10:56:49 GMT
< 
* Ignoring the response-body
{ [data not shown]
100   178  100   178    0     0    564      0 --:--:-- --:--:-- --:--:--   565
* Connection #0 to host www.sina.com left intact
* Issue another request to this URL: 'http://www.sina.com.cn/'
* About to connect() to www.sina.com.cn port 80 (#1)
*   Trying 117.21.216.80...
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to www.sina.com.cn (117.21.216.80) port 80 (#1)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.sina.com.cn
> Accept: */*
> 
< HTTP/1.1 302 Moved Temporarily
< Server: nginx
< Date: Sat, 28 Mar 2020 10:55:11 GMT
< Content-Type: text/html
< Content-Length: 138
< Connection: keep-alive
< Location: https://www.sina.com.cn/

> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.sina.com.cn
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: edge-esnssl-1.17.3-14.3
< Date: Sat, 28 Mar 2020 10:55:11 GMT
< Content-Type: text/html
< Content-Length: 536734
< Connection: keep-alive
< Vary: Accept-Encoding
< ETag: "5e7f2cca-7dd8c"V=CCD0B746
< X-Powered-By: shci_v1.03
< Expires: Sat, 28 Mar 2020 10:56:10 GMT
< Cache-Control: max-age=60
< X-Via-SSL: ssl.25.sinag1.yzyh.lb.sinanode.com
< Age: 1
{ [data not shown]
100  524k  100  524k    0     0   597k      0 --:--:-- --:--:-- --:--:--  597k
* Connection #2 to host www.sina.com.cn left intact

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值