curl命令

Curl是一种支持多种协议(http、https,ftp)等,利用url规则在命令行下工作的文件传输工具,还支持POST、cookies、认证、从指定偏移处下载部分文件、用户代理字符串、限速、文件大小、进度条等特征

1.语法:

curl [options] [URL...]

2.参数:

-a/--append 用于上传文件时,指定附加到目标文件,文件存在则覆盖,不存在则创建

-A/--user-agent 指定客户端agent,即浏览器类型

-anyauth   可以使用“任何”身份验证方法

-b/--cookie <name=string/file>  cookie字符串或文件读取位置

--basic    使用HTTP基本验证

-B/--use-ascii    使用ASCII /文本传输

-c/--cookie-jar <file>   操作结束后把cookie写入到这个文件中

-C/--continue-at <offset>   断点续转

-d/--data <data>  HTTP POST方式传送数据

-D/--dump-header <file>  把header信息写入到该文件中

-e/--referer  来源网址

--create-dirs建立本地目录的目录层次结构

-F/--form <name=content>模拟http表单提交数据

-G/--get   以get的方式来发送数据

-H/--header <line>   自定义头信息传递给服务器

--ignore-content-length  忽略的HTTP头信息的长度

-i/--include  输出时包括protocol头信息

-I/--head  只显示请求头信息

--interface <interface>  使用指定网络接口/地址

-K/--config   指定的配置文件读取

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

--limit-rate <rate>  设置传输速度

-r/ --range 用于分段下载使用 单位为bytes

--local-port<NUM>强制使用本地端口号

-m/--max-time <seconds>  设置最大传输时间

-o/--output   把输出写到该文件中

-O/--remote-name  把输出写到该文件中,保留远程文件的文件名

-p/--proxytunnel  使用HTTP代理

-Q/--quote <cmd>  文件传输前,发送命令到服务器

-R/--remote-time  在本地生成文件时,保留远程文件时间

--retry <num>传输出现问题时,重试的次数

--retry-delay <seconds>  传输出现问题时,设置重试间隔时间

--retry-max-time <seconds>  传输出现问题时,设置最大重试时间

-s/--silent   静默模式。不输出任何东西

-S/--show-error   显示错误

--trace <file>    对指定文件进行debug

-T/--upload-file <file>  上传文件

--url <URL>   Spet URL to work with

-u/--user <user[:password]>设置服务器的用户和密码

-U/--proxy-user <user[:password]>  设置代理用户名和密码

-x/--proxy <host[:port]>在给定的端口上使用HTTP代理,默认1080

-X/--request <command>   指定什么命令

-w/--write-out [format]  指定输出变量格式,变量需按%{变量}的格式,如果要输出自己定义的名字,直接使用%%

 

3.具体用法

1)查看url的请求头信息

curl -I  http://club.xywy.com

 

HTTP/1.1 200 OK

Server: XT-server/0.0

Date: Mon, 28 Sep 2015 02:51:38 GMT

Content-Type: text/html

Connection: close

Vary: Accept-Encoding

XT_ID: cweb-4

2)自定义请求头信息,返回请求状态

curl -I -H "HOST:club.xywy.com"  http://115.182.211.132

 

HTTP/1.1 200 OK

Server: XT-server/0.0

Date: Mon, 28 Sep 2015 02:54:53 GMT

Content-Type: text/html

Connection: close

Vary: Accept-Encoding

XT_ID: cweb-4

 

-H 指定请求头信息,

curl -H "Connection:keep-alive \n User-Agent: Mozilla/5.0 \n Host:bbs.beyond.com"   http://192.168.1.1/index.php  -I

3)查看url的各类响应时间

curl -o /dev/null -s -w http_code:%{http_code}\\ntime_namelookup:%{time_namelookup}\\ntime_redirect:%{time_redirect}\\ntime_pretransfer:%{time_pretransfer}\\ntime_connect:%{time_connect}\\ntime_starttransfer:%{time_starttransfer}\\ntime_total:%{time_total}\\nspeed_download:%{speed_download}\\n  http://club.xywy.com  

 

http_code:200         //返回的状态吗

time_namelookup:0.032     //dns解析域名的时间

time_redirect:0.000       //重定向时间 

time_pretransfer:0.037        //从开始到准备传输的时间

time_connect:0.037            //建立连接的总时间

time_starttransfer:0.044       //从发出请求之后,web返回第一个字节所用时间

time_total:0.463                  //client发出请求;到web的server发送会所有的相应数据的时间

speed_download:617565.000         //下载速度,单位 byte/s

 

 

 

size_upload          //上传文件大小

size_header          //响应头

size_request          //发送请求参数大小

speed_download      //传输速度

speed_upload         //平均上传速度

content_type 

 

 

 

建立tcp连接的时间:time_connect - time_namelookup

 

4)指定来源地址,指定浏览器类型

curl -I -A "Mozilla/5.0 Firefox/21.0"  -e "http://www.baidu.com" -H "Host:bbs.beyond.com"   http://192.168.1.1/index.php

 

5)保存输出到文件

curl -o baidu.html http://www.baidu.com

curl -O https://www.baidu.com/img/bd_logo1.png

 

-o 指定保存文件的位置,把输出重定向到一个文件

-O 下载url中文件,所以url中需指明到某个文件

6)分段下载,并限速

目标文件大小:900M

现在分三次下载,每次300M,并限速1M/s

[root@test01 ~]#  curl -r  0-300000000 -o test.sh.part1 --limit-rate 1024000 -H "HOST:bbs.beyond.com"  http://127.0.0.1/test.sh  -w speed_download:%{speed_download}\\ntime:%{time_total}\\n 

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

100  286M  100  286M    0     0   998k      0  0:04:53  0:04:53 --:--:-- 1142k

speed_download:1022549.000

time:293.384

[root@test01 ~]# du -sh test.sh.part1 

287M    test.sh.part1

 

断点续传,使用#显示进度

curl -C -   -# -r 0-1024000000 -o test.sh.part1 --limit-rate 10M -H "HOST:bbs.beyond.com"  http://127.0.0.1/test.sh  -w speed_download:%{speed_download}\\ntime:%{time_total}\\n

7)使用代理及用户名密码

curl -x 1.1.1.1:80 http://www.baidu.com

curl -x 1.1.1.2:80 -U user:password http://www.baidu.com

 

当网站设置用户身份验证时,不加用户名密码是不行的

[root@test01 ~]#  curl  -I -H "HOST:bbs.beyond.com"  http://127.0.0.1/test.sh

HTTP/1.1 401 Unauthorized

Server: nginx

Date: Mon, 28 Sep 2015 07:05:52 GMT

Content-Type: text/html

Content-Length: 188

Connection: keep-alive

WWW-Authenticate: Basic realm="plese input your name and password"

现在使用-u参数指定用户名和密码

[root@test01 ~]#  curl  -I -H "HOST:bbs.beyond.com" -u fuzj:fuzj123  http://127.0.0.1/test.sh 

HTTP/1.1 200 OK

Server: nginx

Date: Mon, 28 Sep 2015 07:06:27 GMT

Content-Type: application/octet-stream

Content-Length: 943718400

Last-Modified: Mon, 28 Sep 2015 06:25:42 GMT

Connection: keep-alive

ETag: "5608dd66-38400000"

Accept-Ranges: bytes

 

注意:-u是指定的网站授权的用户名和密码,-U指定的是代理服务器授权的用户名密码

8)保留cookie信息到本地,并使用

curl  -c cookie.txt  -I -H "HOST:bbs.beyond.com" -u fuzj:fuzj123  http://127.0.0.1/forum.php

curl  -b cookie.txt  -I -H "HOST:bbs.beyond.com" -u fuzj:fuzj123  http://127.0.0.1/forum.php

 

9)curl 访问 302跳转后的页面

 

curl -s -L -w '%{url_effective}\n' dns.ip.cn

 

10)忽略url中的特殊符号:

抱错:curl: (3) [globbing] bad range specification in column 120"

Escape [ and ] or use -g or --globoff

An example URL that will cause the error is this:

curl "http://www.example.com/?test[]=123"

Either escape the square brackets like this:

curl "http://www.example.com/?test\[\]=123"

or use --globoff like this:

curl --globoff "http://www.example.com/?test[]=123"

or the shorter -g like this:

curl --g "http://www.example.com/?test[]=123"

 

posted @ 2018-04-07 19:00  Fuzengjie  阅读(736)  评论(0编辑  收藏  举报