Think before you speak, read before you think.

curl实战

by

in

取得头信息

curl --head 127.0.0.1
curl -I 127.0.0.1

取得http状态码(加-I只加载头信息,速度更快)
-w后加变量,获取很多与请求相关的额外信息显示在屏幕上,如%{http_code}显示状态码,%{time_total} 响应时间, %{size_download}页面大小, %{content_type} 页面类型

curl -s -o /dev/null -w "%{http_code}" http://jpuyy.com
curl -s -o /dev/null -I -w "%{http_code}" http://jpuyy.com

如果有一批url需要判断状态码,写入url_http_status.txt中,脚本如下:

#!/bin/bash
for i in `cat url_http_status.txt`
do
STATUS_CODE=`curl -o /dev/null -s -w %{http_code} $i`
echo -e "$i:\t$STATUS_CODE"
done

使用安静模式

--silent

curl 跟踪 301跳转,使用参数 -L

➜  ~  curl -I http://www.jpuyy.com -L
HTTP/1.1 301 Moved Permanently
Server: nginx/1.0.15
Date: Sun, 09 Nov 2014 10:40:15 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.3.3
X-CF-Powered-By: WP 1.3.14
X-Pingback: http://jpuyy.com/xmlrpc.php
Location: http://jpuyy.com/

HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 09 Nov 2014 10:40:16 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.3.3
X-CF-Powered-By: WP 1.3.14
X-Pingback: http://jpuyy.com/xmlrpc.php

取得多个子域名信息

curl http://site.{one,two,three}.com
如:curl http://{bbs,ftp,nic}.cczu.edu.cn

下载file1.txt-file100.txt

ftp://ftp.numericals.com/file[1-100].txt
ftp://ftp.numericals.com/file[001-100].txt (with leading zeros)
ftp://ftp.letters.com/file[a-z].txt
http://any.org/archive[1996-1999]/vol[1-4]/part{a,b,c}.html
http://www.numericals.com/file[1-100:10].txt 10,20.....txt

关于cookies

The file specified with -b/–cookie is only used as input.No cookies will be stored in the file.

用于读入

To store cookies, use the -c/–cookie-jar option or you could even save the HTTP headers to a file using -D/–dump-header!

curl --cookie "desktop=1;key2=value2" -v -o /dev/null http://jpuyy.com/

curl伪造头信息

curl --header "X-Forwarded-For:8.8.8.8" --header "Accept-Language: en" http://www.jpuyy.com

curl模拟指定浏览器

curl -A "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/25.0" [URL]

curl模拟指定的referer

curl [URL] -e http://www.aavv.com
或使用 --referer

curl使用压缩

curl -I "http://www.hao123.com" --compressed
curl -I -H "Accept-Encoding: gzip, deflate" "http://www.hao123.com"

curl执行post提交

curl http://localhost:8000/wrap -d text=kadj+kdsj+kasdfj+kaslfdj+jkdasljf+skdfjal-yyyyksdjf-kasjd

curl 带账号,basic 认证

-u, --user <user:password>

curl -L -i --user myuser:mypass http://abc.example.com/manage

curl https出现错误

curl -sS https://getcomposer.org/installer
curl: (60) SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
More details here: http://curl.haxx.se/docs/sslcerts.html

解决办法:
http://curl.haxx.se/docs/caextract.html 下载 ca-bundle.crt到 /etc/pki/tls/certs/ca-bundle.crt

curl 指定代理服务器

curl http://jpuyy.com -x 211.61.47.19:80

指定 socks5 代理服务器

curl --socks5 127.0.0.1:1081 jpuyy.com/ip.php

更多案例:http://www.thegeekstuff.com/2012/04/curl-examples/

下载 github release tar.gz 包

curl -L -O https://github.com/ansible/ansible/archive/v2.1.0.0-0.2.rc2.tar.gz

获取 token,先准备一个认证文件 auth.json

{
"username":"jpuyy",
"password":"pass"
}

发送请求

curl -v -XPOST info.mysite.com/api/auth/token -d @auth.json -H 'Content-Type: application/json'

根据认证的 token 放在 header 访问

curl info.mysite.com/api/config/web.gui-H "Authorization: FuZzA2In0..."

curl 返回 000 状态码表示超时

curl 测试 websocket

curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: echo.websocket.org" -H "Origin: http://www.websocket.org" http://echo.websocket.org

curl 下载断点重连

curl -O -C - http://jpuyy.com/a.txt

curl 自己指定域名的解析

--resolve [DOMAIN]:[PORT]:[IP]
curl https://jpuyy.com --resolve jpuyy.com:443:192.168.1.66

curl 重复请求 100次

curl 'https://jpuyy.com/abc?[1-100]'

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *