linux curl是一个利用URL规则在命令行下工作的文件传输工具。它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称url为下载工具。
1,抓取页面内容到一个文件中
[[email protected] mytest]# curl -o home.html http://blog.51yip.com
[[email protected] mytest]# curl -o home.html http://blog.51yip.com2,用-O(大写的),后面的url要具体到某个文件,不然抓不下来。我们还可以用正则来抓取东西
[[email protected] mytest]# curl -O http://blog.51yip.com/wp-content/uploads/2010/09/compare_varnish.jpg
[[email protected] mytest]# curl -O http://blog.51yip.com/wp-content/uploads/2010/[0-9][0-9]/aaaaa.jpg
[[email protected] mytest]# curl -O http://blog.51yip.com/wp-content/uploads/2010/09/compare_varnish.jpg
[[email protected] mytest]# curl -O http://blog.51yip.com/wp-content/uploads/2010/[0-9][0-9]/aaaaa.jpg3,模拟表单信息,模拟登录,保存cookie信息
[[email protected] mytest]# curl -c ./cookie_c.txt -F log=aaaa -F pwd=****** http://blog.51yip.com/wp-login.php
[[email protected] mytest]# curl -c ./cookie_c.txt -F log=aaaa -F pwd=****** http://blog.51yip.com/wp-login.php4,模拟表单信息,模拟登录,保存头信息
[[email protected] mytest]# curl -D ./cookie_D.txt -F log=aaaa -F pwd=****** http://blog.51yip.com/wp-login.php
[[email protected] mytest]# curl -D ./cookie_D.txt -F log=aaaa -F pwd=****** http://blog.51yip.com/wp-login.php-c(小写)产生的cookie和-D里面的cookie是不一样的。
5,使用cookie文件
[[email protected] mytest]# curl -b ./cookie_c.txt http://blog.51yip.com/wp-admin
[[email protected] mytest]# curl -b ./cookie_c.txt http://blog.51yip.com/wp-admin6,断点续传,-C(大写的)
[[email protected] mytest]# curl -C -O http://blog.51yip.com/wp-content/uploads/2010/09/compare_varnish.jpg
[[email protected] mytest]# curl -C -O http://blog.51yip.com/wp-content/uploads/2010/09/compare_varnish.jpg7,传送数据,最好用登录页面测试,因为你传值过去后,curl回抓数据,你可以看到你传值有没有成功
[[email protected] mytest]# curl -d log=aaaa http://blog.51yip.com/wp-login.php
[[email protected] mytest]# curl -d log=aaaa http://blog.51yip.com/wp-login.php8,显示抓取错误,下面这个例子,很清楚的表明了。
[[email protected] mytest]# curl -f http://blog.51yip.com/asdf
curl: (22) The requested URL returned error: 404
[[email protected] mytest]# curl http://blog.51yip.com/asdf
<HTML><HEAD><TITLE>404,not found</TITLE>
。。。。。。。。。。。。
[[email protected] mytest]# curl -f http://blog.51yip.com/asdf
curl: (22) The requested URL returned error: 404
[[email protected] mytest]# curl http://blog.51yip.com/asdf
<HTML><HEAD><TITLE>404,not found</TITLE>
。。。。。。。。。。。。9,伪造来源地址,有的网站会判断,请求来源地址。
[[email protected] mytest]# curl -e http://localhost http://blog.51yip.com/wp-login.php
[[email protected] mytest]# curl -e http://localhost http://blog.51yip.com/wp-login.php10,当我们经常用curl去搞人家东西的时候,人家会把你的IP给屏蔽掉的,这个时候,我们可以用代理
[[email protected] mytest]# curl -x 24.10.28.84:32779 -o home.html http://blog.51yip.com
[[email protected] mytest]# curl -x 24.10.28.84:32779 -o home.html http://blog.51yip.com11,比较大的东西,我们可以分段下载
[[email protected] mytest]# curl -r 0-100 -o img.part1 http://blog.51yip.com/wp-
content/uploads/2010/09/compare_varnish.jpg
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 101 100 101 0 0 105 0 --:--:-- --:--:-- --:--:-- 0
[[email protected] mytest]# curl -r 100-200 -o img.part2 http://blog.51yip.com/wp-
content/uploads/2010/09/compare_varnish.jpg
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 101 100 101 0 0 57 0 0:00:01 0:00:01 --:--:-- 0
[[email protected] mytest]# curl -r 200- -o img.part3 http://blog.51yip.com/wp-
content/uploads/2010/09/compare_varnish.jpg
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 104k 100 104k 0 0 52793 0 0:00:02 0:00:02 --:--:-- 88961
[[email protected] mytest]# ls |grep part | xargs du -sh
4.0K one.part1
112K three.part3
4.0K two.part2
[[email protected] mytest]# curl -r 0-100 -o img.part1 http://blog.51yip.com/wp-
content/uploads/2010/09/compare_varnish.jpg
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 101 100 101 0 0 105 0 --:--:-- --:--:-- --:--:-- 0
[[email protected] mytest]# curl -r 100-200 -o img.part2 http://blog.51yip.com/wp-
content/uploads/2010/09/compare_varnish.jpg
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 101 100 101 0 0 57 0 0:00:01 0:00:01 --:--:-- 0
[[email protected] mytest]# curl -r 200- -o img.part3 http://blog.51yip.com/wp-
content/uploads/2010/09/compare_varnish.jpg
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 104k 100 104k 0 0 52793 0 0:00:02 0:00:02 --:--:-- 88961
[[email protected] mytest]# ls |grep part | xargs du -sh
4.0K one.part1
112K three.part3
4.0K two.part2用的时候,把他们cat一下就OK了,cat img.part* >img.jpg
12,不会显示下载进度信息
[[email protected] mytest]# curl -s -o aaa.jpg http://blog.51yip.com/wp-content/uploads/2010/09/compare_varnish.jpg
[[email protected] mytest]# curl -s -o aaa.jpg http://blog.51yip.com/wp-content/uploads/2010/09/compare_varnish.jpg13,显示下载进度条
[[email protected] mytest]# curl -# -O http://blog.51yip.com/wp-content/uploads/2010/09/compare_varnish.jpg
######################################################################## 100.0%
[[email protected] mytest]# curl -# -O http://blog.51yip.com/wp-content/uploads/2010/09/compare_varnish.jpg
######################################################################## 100.0%14,通过ftp下载文件
[[email protected] ~]$ curl -u 用户名:密码 -O http://blog.51yip.com/demo/curtain/bbstudy_files/style.css
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
101 1934 101 1934 0 0 3184 0 --:--:-- --:--:-- --:--:-- 7136
[[email protected] ~]$ curl -u 用户名:密码 -O http://blog.51yip.com/demo/curtain/bbstudy_files/style.css
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
101 1934 101 1934 0 0 3184 0 --:--:-- --:--:-- --:--:-- 7136
或者用下面的方式 查看复制打印?
[[email protected] ~]$ curl -O ftp://用户名:密码@ip:port/demo/curtain/bbstudy_files/style.css
[[email protected] ~]$ curl -O ftp://用户名:密码@ip:port/demo/curtain/bbstudy_files/style.css
15,通过ftp上传
[[email protected] ~]$ curl -T test.sql ftp://用户名:密码@ip:port/demo/curtain/bbstudy_files/
[[email protected] ~]$ curl -T test.sql ftp://用户名:密码@ip:port/demo/curtain/bbstudy_files/
3