神器之CURL

什么是CURL

curl是利用URL语法在命令行方式下工作的开源文件传输工具.它被广泛应用在Unix\多种Linux发行版中,并且有DOS和Win32\Win64下的移植版本.

常见用法

1.获取页面内容

root@amu:/Data/apps# curl 127.0.0.1/login.php
<!DOCTYPE html>
<html>
<head>
    <title>login</title>
    <meta name="viewport" content="width=device-width,initial-scale=1" />
</head>
<body>
    欢迎注册,请输入用户名和密码  
    <form method="post" action="register.php">  
        <p>姓名:<input type="text" name="name" size="10"></p>  
        <p>密码:<input type="password" name="password" size="10"></p>  
        <p>
            <input type="submit" value="确定">  
            <input type="reset" value="取消">
         </p>  
    </form> 
    <a href='http://10.42.0.1/weixin/weixin.html'>微信连Wi-Fi</a>
</body>
</html>

当我们不加任何选修使用curl时,默认会发送GET请求来获取链接内容到标准输出

2.显示HTTP头

root@amu:/Data/apps# curl -I 127.0.0.1
HTTP/1.1 302 Found
Server: nginx/1.13.8
Date: Thu, 10 May 2018 03:47:56 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.1.13
Location: http://127.0.0.1/login.php

也可以同时显示HTTP头和文件内容,使用-i选项
curl -i 127.0.0.1

3.将内容保存到文件

root@amu:/Data/apps# curl -o index.html 127.0.0.1
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   969    0   969    0     0   429k      0 --:--:-- --:--:-- --:--:--  473k
root@amu:/Data/apps# ls
index.html  nginx1.13.8  openresty  php7.1.13
root@amu:/Data/apps# curl -O 127.0.0.1/index.php
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   969    0   969    0     0   441k      0 --:--:-- --:--:-- --:--:--  473k
root@amu:/Data/apps# ls
index.html  index.php  nginx1.13.8  openresty  php7.1.13

两种用法:-o 和 -O
-o:结果会被保存到命令行中提供的文件名
-O:URL中的文件名会被用作保存输出的文件名

4.同时下载多个文件

curl -O 127.0.0.1/index.php -O 127.0.0.1/login.php
当然用-o也可以

5.使用 -L 跟踪重定向

从第二个例子中我们可以看到网站给我们返回的状态码是302,说明网站跳转了,在这种情况下就无法获取我们想要的网页内容,这是我们就可以用-L选项了.

root@amu:/Data/apps# curl -L 127.0.0.1
<!DOCTYPE html>
<html>
<head>
    <title>login</title>
    <meta name="viewport" content="width=device-width,initial-scale=1" />
</head>
<body>
    欢迎注册,请输入用户名和密码  
    <form method="post" action="register.php">  
        <p>姓名:<input type="text" name="name" size="10"></p>  
        <p>密码:<input type="password" name="password" size="10"></p>  
        <p>
            <input type="submit" value="确定">  
            <input type="reset" value="取消">
         </p>  
    </form> 
    <a href='http://10.42.0.1/weixin/weixin.html'>微信连Wi-Fi</a>
</body>
</html>

6.使用 -A 自定义 USER-Agent

curl --user-agent "[User Agent]" [URL]
curl --user-agent "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7" www.baidu.com

7.使用 -H 自定义 header

curl -H 'Host: 192.168.199.4' -H 'Accept-Language:es' http://127.0.0.1

8.使用 Cookie

#保存http的responses里面的Cookie信息
curl -c cookiec.txt http://www.baidu.com
#保存http的response里面的header信息
curl -D cookied.txt http://www.baidu.com
#使用cookie
curl -b cookiec.txt http://www.baidu.com

9.传递数据

http协议有各种请求的方法,如:GET\POST\HEADD\PUT等.这些方法curl都支持.使用-X指定请求方法,没有指定的情况下,默认使用GET请求方法.POSTPUT方法提交的数据用-d参数指定

//GET方法
curl http://127.0.0.1?age=2
//HEAD方法,获取响应的头部信息
curl -I http://127.0.0.1
// POST 方法
curl -X POST http://127.0.0.1 -d "name=admin&pwd=123"
// PUT 方法
curl -X PUT http://127.0.0.1 -d '{"name":"admin"}'

10.查看详细请求和响应过程

root@amu:/Data/apps# curl -v 127.0.0.1
* Rebuilt URL to: 127.0.0.1/
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 302 Found
< Server: nginx/1.13.8
< Date: Thu, 10 May 2018 04:41:13 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Powered-By: PHP/7.1.13
< Location: http://127.0.0.1/login.php
< 
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: HTTP_REFERER in /Data/apps/nginx1.13.8/html/index.php on line <i>66</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0002</td><td bgcolor='#eeeeec' align='right'>373888</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='/Data/apps/nginx1.13.8/html/index.php' bgcolor='#eeeeec'>.../index.php<b>:</b>0</td></tr>
</table></font>
* Connection #0 to host 127.0.0.1 left intact

11.查看请求或响应的具体某一指标

root@amu:/Data/apps# curl -s -w "%{time_total}\n%{size_download}\n" https://github.com -o /dev/null
7.514
54697
root@amu:/Data/apps# curl -w %{http_code} https://github.com -o /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 54697    0 54697    0     0  11389      0 --:--:--  0:00:04 --:--:-- 13515

12.完成认证

curl --basic --user 'admin:123' http://example.com/

13文件上传

curl --form upload=@localfilename --form press=OK [URL]
curl http://api.example.com/profile -F "image=@profile.jpg"
curl http://api.example.com/profile -F "image=@profile.jpg" -F "phone=123456789"

其他一些用法

curl --trace output.txt www.sina.com
curl --referer http://www.example.com http://www.example.com

欢迎关注个人微信公众号:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。