curl命令是一个利用URL规则在命令行下工作的文件传输工具。它支持文件的上传和下载,支持包括HTTP、HTTPS、ftp等众多协议,还支持POST、cookies、认证、从指定偏移处下载部分文件、用户代理字符串、限速、文件大小、进度条等特征。
1. curl [选项] URL
1.不带选项,返回网站源码
[root@test1 ~]$curl www.sina.com
301 Moved Permanently
301 Moved Permanently
nginx
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
[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