Linux curl命令简介

在Linux中curl是一个利用URL规则在命令行下工做的文件传输工具,支持的协议包括 (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP),curl设计为无用户交互下完成工做;php


  curl提供了一大堆很是有用的功能,包括代理访问、用户认证、ftp上传下载、HTTP POST、SSL链接、cookie支持、断点续传...html


语法:curl [option] [url]
json

常见参数:浏览器

[root@localhost src]# curl www.baidu.com|iconv -fgb2312

执行后,www.baidu.com 的html就会显示在屏幕上了,这个用法常常用于测试一台服务器是否能够到达一个网站,如发现乱码,能够使用iconv转码安全


-I/--head 仅返回头部信息,使用HEAD请求:curl -I http://www.baidu.combash


保存访问的网页服务器

-o:将文件保存为命令行中指定的文件名的文件中
-O:使用URL中默认的文件名保存文件到本地,这里
后面的url要具体到某个文件,否则抓不下来cookie

[root@localhost src]# curl www.baidu.com >> baidu.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2381  100  2381    0     0   100k      0 --:--:-- --:--:-- --:--:--  105k
[root@localhost src]# curl -o baidu.com www.baidu.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2381  100  2381    0     0  81602      0 --:--:-- --:--:-- --:--:-- 82103
[root@localhost src]# curl -O www.baidu.com
curl: Remote file name has no length!
curl: try 'curl --help' or 'curl --manual' for more information
[root@localhost src]# curl -O www.baidu.com/index.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2381  100  2381    0     0   102k      0 --:--:-- --:--:-- --:--:--  105k
[root@localhost src]#


测试网页返回值网络

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

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

[root@localhost src]# curl -o /dev/null -s -w "%{http_code}" www.baidu.com
200[root@localhost src]# curl -o /dev/null -s -w "%{http_code}\n" www.baidu.com
200
[root@localhost src]# curl -o /dev/null -s -w "time_total: %{time_total}\n" "http://www.baidu.com"
time_total: 0.024
[root@localhost src]#

在脚本中,这是很常见的测试网站是否正常的用法


指定proxy服务器以及其端口

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

[root@localhost src]# curl -x 192.168.100.198:8888 http://www.baidu.com


cookie

  • -c/--cookie-jar <file>    把cookie写入到这个文件中

[root@Super ~]# curl -d"name=123&password=456" http://192.168.100.182/index.php -v -c ./cookie

使用用户名和密码登陆系统,并将cookie信息存储在当前目录的cookie文件中

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

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

[root@Super ~]# curl http://192.168.100.182/index.php -v -b ./cookie
  • ‘-cookie’直接指定cookie

[root@Super ~]# curl --cookie "name=123" http://192.168.100.182/index.php -v


模仿浏览器

-A/--user-agent <string>    设置用户代理发送给服务器

[root@localhost src]# curl -A "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0)" http://www.baidu.com

服务器端就会认为是使用IE8.0去访问的


伪造referer(盗链)

-e/--referer    来源网址

不少服务器会检查http访问的referer从而来控制访问。好比:你是先访问首页,而后再访问首页中的邮箱页面,这里访问邮箱的referer地址就是访问首页成功后的页面地址,若是服务器发现对邮箱页面访问的referer地址不是首页的地址,就判定那是个盗连了

[root@localhost src]# curl -e "www.baidu.com" http://image.baiud.com

这样就会让服务器其觉得你是从www.baidu.com点击某个连接访问image.baidu.com的


下载文件

[root@localhost src]# curl -o 51.png https://s1.51cto.com/wyfs02/M00/8D/49/wKioL1iV9-6wk8YsAACLSADynaw310.png
[root@localhost src]# curl -O https://s1.51cto.com/wyfs02/M00/8D/49/wKioL1iV9-6wk8YsAACLSADynaw310.png

循环下载

[root@localhost src]# curl -O http://www.51cto.com/justin[1-5].png

justin1.png-justin5.png所有下载下来


下载重命名

[root@localhost src]# curl -O http://www.51cto.com/{justin,peng}/justin[1-5].png
[root@localhost src]# curl -o #1_#2.png http://www.51cto.com/{justin,peng}/justin[1-5].png

第一条命令会先去下载justin下的justin1-justin5.png文件,而后再下载peng下的justin1-justin5.png文件,这样就会把以前下载的文件覆盖,

第二条命令下载时候重命名成justin_justin1.png justin_justin2.png的形式


分块下载

-r/--range <range>    检索来自HTTP/1.1或FTP服务器字节范围

有时候下载的东西会比较大,这个时候咱们能够分段下载

[root@localhost src]# curl -r 0-100 -o justin_part1.png http://www.51cto.com/justin.png 
[root@localhost src]# curl -r 1000-200 -o justin_part2.png http://www.51cto.com/justin.png 
[root@localhost src]# curl -r 200- -o justin_part3.png http://www.51cto.com/justin.png 
[root@localhost src]# cat justin_part* > justin.png


经过ftp下载文件

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

-E 采用证书认证

[root@localhost src]# curl -O -u justin:peng ftp://www.51cto.com/justin.png
[root@localhost src]# curl -O ftp://justin:peng@www.51cto.com/justin.png
[root@localhost src]# curl -E cert.pem https://www.51cto.com/justin.png


下载进度条

-#/--progress-bar    进度条显示当前的传送状态

[root@localhost src]# curl -# -O http://www.baidu.com/justin.png

-s 不会显示下载进度信息

[root@localhost src]# curl -s -O http://www.baidu.com/justin.png


断点续传

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

[root@localhost src]# curl -C -O http://www.baidu.com/justin.png


上传文件

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

[root@localhost src]# curl -T justin.png -u justin:peng ftp://www.baidu.com/img/


显示抓取错误

-f/--fail    链接失败时不显示http错误

[root@localhost src]# curl -f http://www.baidu.com/error


-F表单提交操做

    -F命令以Content-Type:multipart/form-data的形式向serverpost数据,该命令容许提交二进制文件等。
    能够使用@前缀来制定提交的内容为一个文件,也能够使用<符号来提交文件中的内容

curl -F prefile=@portrait.jpg https://example.com/upload.cgi

向服务器上传一个图片,图片的表单域名为profile,内容为protrait.jpg的二进制


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

-X 指定请求方式

在浏览器输入一个网址访问网站都是GET请求,在FORM表单中,能够经过设置Method指定提交方式为GET或者POST提交方式,默认为GET提交方式。    
HTTP定义了与服务器交互的不一样方法,其中最基本的四种:GET,POST,PUT,DELETE,HEAD
GET和HEAD被称为安全方法,由于使用GET和HEAD的HTTP请求不会产生什么动做。不会产生动做意味着GET和HEAD的HTTP请求不会在服务器上产生任何结果。可是安全方法并非什么动做都不产生,这里的安全方法仅仅指不会修改信息。
GET请求,请求的数据会附加在URL以后,以?分割URL和传输数据,多个参数用&链接。URL的编码格式采用的是ASCII编码,而不是uniclde,便是说全部的非ASCII字符都要编码以后再传输。
POST请求会把请求的数据放置在HTTP请求包的包体中

POST请求

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

[root@Super ~]# curl -X POST -d"name=123&password=456" http://192.168.100.182/index.php -v

-d选项为使用POST方式向server发送数据,所以在使用-d的时候,能够省略-X POST。使用-d时,将使用Content-type:application/x-www-form-urlencoded方式发送数据。使用JSON形式post数据,能够使用-H指定头部类型:

[root@Super ~]# curl -X POST -H "Content-Type:application/json" -d '{"data":"123","key":"456"}' http://192.168.100.182/index.php -v

当提交的参数值中有特殊字符就须要先转义,如空格时,就须要转义成%20;--data-urlencode能够自动转义特殊字符,无需人工事先转义

[root@Super ~]# curl --data-urlencode "value 1" http://192.168.100.182/index.php -v

--data-urlencode参数会自动转义特殊字符,如上面的空格

请求的时候带上Cookie:

[root@Super ~]# curl -X POST -H "Cookie:username=123"  http://192.168.100.182/index.php -v


    --data-ascii <data>    以ascii的方式post数据

    --data-binary <data>    以二进制的方式post数据

    --connect-timeout <seconds> 设置最大请求时间

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

GET请求

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

[root@Super ~]# /usr/bin/curl -G -d "username=justin&pwd=123456" "
[root@Super ~]# curl -X GET http://www.baidu.com

在访问须要受权的页面时,可经过-u选项提供用户名和密码进行受权;curl -u username:password URL

[root@Super ~]# curl -u ywbz http://192.168.100.127
Enter host password for user 'ywbz':


DELETE请求

[root@Super ~]# curl -I -X DELETE http://192.168.100.182/index.php -v


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

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

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

curl --limit-rate 1000B -O http://192.168.100.182/index.php


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

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

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


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

-y/--speed-time    放弃限速所要的时间。默认为30

-Y/--speed-limit    中止传输速度的限制,速度时间'秒

-z/--time-cond    传送时间设置

curl -z 11-Oct-18 http://192.168.100.182/index.php

若文件index.php在2018-10-11以后游过更新才会下载

-0/--http1.0    使用HTTP 1.0

-1/--tlsv1    使用TLSv1(SSL)

-2/--sslv2    使用SSLv2的(SSL)

-3/--sslv3    使用的SSLv3(SSL)

相关文章
相关标签/搜索