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设计为无用户交互下完成工作;curl提供了一大堆非常有用的功能,包括代理访问、用户认证、ftp上传下载、HTTP POST、SSL连接、cookie支持、断点续传...
语法:curl l]
常见参数:
# curl
# curl www.baidu.com|iconv -fgb2312 执行后,www.b.combaidu.com 的html就会显示在屏幕上了,这个用法经常用于测试一台服务器是否可以到达一个网站,如发现乱码,可以使用iconv转码
保存访问的网页
-o/--output 把输出写到该文件中
-O/--remote-name 把输出写到该文件中,保留远程文件的文件名,这里后面的url要具体到某个文件,不然抓不下来
# curl www.baidu.com >> baidu.com
% Total % Received % XferdAverage Speed Time Time TimeCurrent
DloadUpload Total Spent LeftSpeed
10023811002381 0 0 100k 0 --:--:-- --:--:-- --:--:--105k
# curl -o baidu.com www.baidu.com
% Total % Received % XferdAverage Speed Time Time TimeCurrent
DloadUpload Total Spent LeftSpeed
10023811002381 0 081602 0 --:--:-- --:--:-- --:--:-- 82103
# curl -O www.baidu.com
curl: Remote file name has no length!
curl: try 'curl --help' or 'curl --manual' for more information
# curl -O www.baidu.com/index.html
% Total % Received % XferdAverage Speed Time Time TimeCurrent
DloadUpload Total Spent LeftSpeed
10023811002381 0 0 102k 0 --:--:-- --:--:-- --:--:--105k
#
测试网页返回值
-s/--silent 静音模式。不输出任何东西
-w/--write-out 什么输出完成后
# curl -o /dev/null -s -w "%{http_code}" www.baidu.com
200# curl -o /dev/null -s -w "%{http_code}\n" www.baidu.com
200
# curl -o /dev/null -s -w "time_total: %{time_total}\n" "http://www.baidu.com"
time_total: 0.024
# 在脚本中,这是很常见的测试网站是否正常的用法
指定proxy服务器以及其端口
-x/--proxy <host[:port]> 在给定的端口上使用HTTP代理
# curl -x 192.168.100.198:8888 http://www.baidu.com
cookie
-c/--cookie-jar <file> 把cookie写入到这个文件中
-D/--dump-header <file> 把header信息写入到该文件中
-b/--cookie <name=string/file> cookie字符串或文件读取位置
# curl -c cookiec.txt #保存http的response里面的cookie信息
# curl -D cookied.txt http://www.baidu.com #保存http的response里面的header信息
# curl -b cookiec.txt http://www.baidu.com
模仿浏览器
-A/--user-agent <string> 设置用户代理发送给服务器
# 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地址不是首页的地址,就断定那是个盗连了
# curl -e "www.baidu.com" http://image.baiud.com 这样就会让服务器其以为你是从www.baidu.com点击某个链接访问image.baidu.com的
下载文件
# curl -o 51.png http://s1.51cto.com/wyfs02/M00/8D/49/wKioL1iV9-6wk8YsAACLSADynaw310.png
# curl -O http://s1.51cto.com/wyfs02/M00/8D/49/wKioL1iV9-6wk8YsAACLSADynaw310.png 循环下载
# curl -O http://www.51cto.com/justin.png justin1.png-justin5.png全部下载下来
下载重命名
# curl -O http://www.51cto.com/{justin,peng}/justin.png
# curl -o #1_#2.png http://www.51cto.com/{justin,peng}/justin.png 第一条命令会先去下载justin下的justin1-justin5.png文件,然后再下载peng下的justin1-justin5.png文件,这样就会把之前下载的文件覆盖,
第二条命令下载时候重命名成justin_justin1.png justin_justin2.png的形式
分块下载
-r/--range <range> 检索来自HTTP/1.1或FTP服务器字节范围
有时候下载的东西会比较大,这个时候我们可以分段下载
# curl -r 0-100 -o justin_part1.png http://www.51cto.com/justin.png
# curl -r 1000-200 -o justin_part2.png http://www.51cto.com/justin.png
# curl -r 200- -o justin_part3.png http://www.51cto.com/justin.png
# cat justin_part* > justin.png
通过ftp下载文件
-u/--user <user[:password]> 设置服务器的用户和密码
-E 采用证书认证
# curl -O -u justin:peng ftp://www.51cto.com/justin.png
# curl -O ftp://justin:peng@www.51cto.com/justin.png
# curl -E cert.pem https://www.51cto.com/justin.png
下载进度条
-#/--progress-bar 进度条显示当前的传送状态
# curl -# -O http://www.baidu.com/justin.png -s 不会显示下载进度信息
# curl -s -O http://www.baidu.com/justin.png
断点续传
-C/--continue-at <offset> 断点续转
# curl -C -O http://www.baidu.com/justin.png
上传文件
-T/--upload-file <file> 上传文件
# curl -T justin.png -u justin:peng ftp://www.baidu.com/img/
显示抓取错误
-f/--fail 连接失败时不显示http错误
# curl -f http://www.baidu.com/error
-B/--use-ascii 使用ASCII文本传输
-d/--data <data> HTTP POST方式传送数据,当提交的参数值中有特殊字符就需要先转义,如空格时,就需要转义成%20;--data-urlencode可以自动转义特殊字符,无需人工事先转义
# /usr/bin/curl -d "username=justin&pwd=123456" "http://192.168.15.250/webAuth/" --data-ascii <data> 以ascii的方式post数据
--data-binary <data> 以二进制的方式post数据
--connect-timeout <seconds> 设置最大请求时间
--create-dirs 建立本地目录的目录层次结构
-G/--get 以get的方式来发送数据
/usr/bin/curl -G -d "username=justin&pwd=123456" "http://192.168.15.250/webAuth/" --interface <interface> 使用指定网络接口/地址
-l/--list-only 列出ftp目录下的文件名称
--limit-rate <rate> 设置传输速度
--retry <num> 传输出现问题时,重试的次数
--retry-delay <seconds> 传输出现问题时,设置重试间隔时间
--retry-max-time <seconds> 传输出现问题时,设置最大重试时间
-S/--show-error 显示错误
-y/--speed-time 放弃限速所要的时间。默认为30
-Y/--speed-limit 停止传输速度的限制,速度时间'秒
-z/--time-cond 传送时间设置
-0/--http1.0 使用HTTP 1.0
-1/--tlsv1 使用TLSv1(SSL)
-2/--sslv2 使用SSLv2的(SSL)
-3/--sslv3 使用的SSLv3(SSL)
页:
[1]