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 [option] [url]

常见参数:

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

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


保存访问的网页

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

-O/--remote-name    把输出写到该文件中,保留远程文件的文件名,这里后面的url要具体到某个文件,不然抓不下来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[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    静音模式。不输出任何东西

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

1
2
3
4
5
6
[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代理

1
[root@localhost src] # 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字符串或文件读取位置

1
2
3
[root@localhost src] # curl -c cookiec.txt    #保存http的response里面的cookie信息
[root@localhost src] # curl -D cookied.txt http://www.baidu.com #保存http的response里面的header信息
[root@localhost src] # curl -b cookiec.txt http://www.baidu.com


模仿浏览器

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

1
[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地址不是首页的地址,就断定那是个盗连了

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

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


下载文件

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

循环下载

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

justin1.png-justin5.png全部下载下来


下载重命名

1
2
[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服务器字节范围

有时候下载的东西会比较大,这个时候我们可以分段下载

1
2
3
4
[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 采用证书认证

1
2
3
[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    进度条显示当前的传送状态

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

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

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


断点续传

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

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


上传文件

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

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


显示抓取错误

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

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


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

-d/--data <data>    HTTP POST方式传送数据,当提交的参数值中有特殊字符就需要先转义,如空格时,就需要转义成%20;--data-urlencode可以自动转义特殊字符,无需人工事先转义

1
[root@localhost src] # /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的方式来发送数据

1
/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)




本文转自 justin_peng 51CTO博客,原文链接:http://blog.51cto.com/ityunwei2017/1977513,如需转载请自行联系原作者

相关文章
|
11天前
|
Web App开发 Linux 网络安全
工作中常用到的Linux命令
工作中常用到的Linux命令
|
8天前
|
NoSQL Linux Shell
常用的 Linux 命令
常用的 Linux 命令
30 9
|
1天前
|
安全 Linux 开发工具
Linux中可引起文件时间戳改变的相关命令
【4月更文挑战第12天】Linux中可引起文件时间戳改变的相关命令
9 0
|
3天前
|
域名解析 网络协议 Linux
Linux 中的 Nslookup 命令怎么使用?
【4月更文挑战第12天】
22 6
Linux 中的 Nslookup 命令怎么使用?
|
3天前
|
运维 网络协议 Unix
18.系统知识-Linux常用命令
18.系统知识-Linux常用命令
|
4天前
|
网络协议 Ubuntu Linux
Linux 下 TFTP 服务搭建及 U-Boot 中使用 tftp 命令实现文件下载
Linux 下 TFTP 服务搭建及 U-Boot 中使用 tftp 命令实现文件下载
|
4天前
|
Linux Go
Linux命令Top 100驱动人生! 面试必备
探索Linux命令不再迷茫!本文分10部分详解20个基础命令,带你由浅入深掌握文件、目录管理和文本处理。 [1]: <https://cloud.tencent.com/developer/article/2396114> [2]: <https://pan.quark.cn/s/865a0bbd5720> [3]: <https://yv4kfv1n3j.feishu.cn/docx/MRyxdaqz8ow5RjxyL1ucrvOYnnH>
48 0
|
7天前
|
缓存 运维 监控
Linux系统监控利器:探索常用命令及数据保存技巧
Linux系统监控利器:探索常用命令及数据保存技巧
26 4
Linux系统监控利器:探索常用命令及数据保存技巧
|
10天前
|
Linux 索引
linux 文件查找 和文件管理常用命令
linux 文件查找 和文件管理常用命令
19 0
|
10天前
|
安全 Unix Linux
一、linux 常用命令之 linux版本信息 系统管理与设置 持续更新******
一、linux 常用命令之 linux版本信息 系统管理与设置 持续更新******
14 0