设为首页 收藏本站
查看: 523|回复: 0

[经验分享] Linux curl命令简介

[复制链接]

尚未签到

发表于 2018-5-17 06:45:36 | 显示全部楼层 |阅读模式
  在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]

  常见参数:
[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要具体到某个文件,不然抓不下来
[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]    什么输出完成后
[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写入到这个文件中

  -D/--dump-header <file>   把header信息写入到该文件中
  -b/--cookie <name=string/file>    cookie字符串或文件读取位置
[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>    设置用户代理发送给服务器
[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 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  循环下载
[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  

  -B/--use-ascii    使用ASCII文本传输
  -d/--data <data>    HTTP POST方式传送数据,当提交的参数值中有特殊字符就需要先转义,如空格时,就需要转义成%20;--data-urlencode可以自动转义特殊字符,无需人工事先转义
[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的方式来发送数据
/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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-461095-1-1.html 上篇帖子: Linux上的mysql集群 下篇帖子: Linux基础04
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表