curl

curl

  • -# 显示进度(具体显示啥进度测试不出来)
  • -0 指定使用http1.0协议发起请求
$curl -svI   ab.text.com/jdk-8u101-linux-x64.tar.gz 
......
> HEAD /big/SGWS5.PC6game.zip HTTP/1.1  #默认协议为1.1

$curl -0  -svI   ab.text.com/jdk-8u101-linux-x64.åtar.gz
......
> HEAD /big/SGWS5.PC6game.zip HTTP/1.0  #默认协议已经修改为http 1.0了
  • 进行https访问 -1 强制使用 TLS1.x版本访问(ssl 1版本),-2 ssl 2版本 -3 ssl 3版本 
  •  -4 将地址解析为ipv4地址(有ipv6的情况下)  -6 将地址解析为ipv6地址(有ipv6的情况下)(不知道怎么测试,先放着继续往下)
  •  -a 在进行ftp/sftp上传的时,使用-a参数可以将本地文件追加到目录下面 (测试出问题了)

  • -A 使用某个客户端访问,指定多个客户端的时候,以最后一个为主
$curl -A 'Mozilla/5.0' -I  ab.text.com/jdk-8u101-linux-x64.tar.gz

# tail -1  /data/logs/www/ab.text.com.log   #通过下面的日志就可以看出使用的是Mozilla进行访问,而不是通过curl访问的
10.168.1.102 | - | 22/Oct/2019:09:53:53 -0400 | HEAD /jdk-8u101-linux-x64.tar.gz HTTP/1.1 | 200 | 256 | 0 | ab.text.com | - | Mozilla/5.0 | - | - | - | 0.000 | -

  • -b 将客户端数据作为cookie发送给服务器,在浏览器访问某个网站的时候,浏览器会自动将本地cookie信息打包发送给服务器。
#发送单个cookie
$curl -b "test=test1" -vI  ab.text.com/jdk-8u101-linux-x64.tar.gz
*   Trying 10.28.88.199...
* TCP_NODELAY set
* Connected to ab.text.com (10.28.88.199) port 80 (#0)
> HEAD /jdk-8u101-linux-x64.tar.gz HTTP/1.1
> Host: ab.text.com
> User-Agent: curl/7.54.0
> Accept: */*
> Cookie: test=test1

#发送多个cookie 语法:“name1=value1;name2=value2” 中间用;隔开
$curl -b "test=test1;test2=test2" -vI  ab.text.com/jdk-8u101-linux-x64.tar.gz
*   Trying 10.28.88.199...
* TCP_NODELAY set
* Connected to ab.text.com (10.28.88.199) port 80 (#0)
> HEAD /jdk-8u101-linux-x64.tar.gz HTTP/1.1
> Host: ab.text.com
> User-Agent: curl/7.54.0
> Accept: */*
> Cookie: test=test1;test2=test2

#读取本地cookei 语法: -b filename(没有看到cookie信息)
$curl -b 1.txt -vI  ab.text.com
  • -c 把服务器发送过来的cookie存储到本地,如果没有返回任何cookie,则不会写入任何文件
$curl -c cookie.txt -vL  www.baidu.com  #需要加 -L 才能看到set-cookie信息
......
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/

$ls cookie.txt
cookie.txt

$cat cookie.txt
# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

.baidu.com	TRUE	/	FALSE	1571841604	BDORZ	27315

  • -X 指定发起请求所使用的请求方法(后面跟着各种请求方法,什么prot,put,HTTPS_PROXY等等) 默认是get
  •  -H  指定一个header(标头,指定请求头部分 )选项 值是key=value 需要指定其他header时,curl需要加 -H参数
  •   -d  发送post请求的数据体  --data-binary 上传二进制数据  --data-urlencode 自动将数据进行URL编码(这3个参数的功能大致一样)

注:将post请求体发送给服务器,使用-d参数后,header请求头会自动添加 application / x-www-form-urlencoded 标头,也会把请求方法自动转换为POST方法,不用 -X post   用法: -d '{具体数据}'

#用法基本是这样
$ curl -H "Content-Type:application/json" -XPUT 'http://10.28.88.199:9200/ac/book/1?pretty' -d '{"title":"创始道纪"}' 

  • -D 将header信息写入文件
$curl -D header.txt  -vI  www.baidu.com

s$ls header.txt
header.txt

$cat header.txt
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Tue, 22 Oct 2019 15:07:55 GMT
Etag: "575e1f72-115"
Last-Modified: Mon, 13 Jun 2016 02:50:26 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
  • --digest 启用http身份验证(防止密码以明文方式传输) 一般配合-u参数使用。
  • -e 设置referer信息,referer主要表示当前来源页面,在我的header信息中,这个referer有详细介绍
$curl -e "ab.text.com/a.html" http://www.baidu.com

$curl -H  "referer:ab.text.com/a.html" http://www.baidu.com # -H "referer:" 与 -e效果一样

#为什么这么做,那我现在也测试不出来
  • -E  通过curl进行https,ftps基于ssl协议访问时候需要指定客户端证书文件,并且证书必须为PEM格式,语法: -E, --cert <certificate[:password]>

 --cert-type <type> 指定curl证书支持类型,支持PEM,DER,ENG  --cacert <CA certificate>  指定curl使用指定证书进行验证   --capath <CA certificate directory> 指定证书目录多个文件用;分开(path1;path2)


  • -F 用来向服务器上传二进制文件就是模拟表单提交
#一般用法
$curl -F 'file=@cookie.txt' ab.text.com/user

#指定文件类型 使用type指令
$curl -F 'file=@cookie.txt;type=text/plain' ab.text.com/user

#改名(将文件传送到服务器之后进行改名) 使用filename指令
$curl -F 'file=@cookie.txt;filename=cookie' ab.text.com/user

注:有个前提是你的服务器可以接收你所上传的数据,并且进行处理

  • -G 主要是将-d(-d 默认是post请求方法)指定的数据通过GET请求方法发送给服务器,简单来说:-G可以把-d默认的post请求方法改为get请求方法。
#默认访问方式
$curl -v -d 'c=wwert' -d 'count=30'  ab.text.com
* Rebuilt URL to: ab.text.com/
*   Trying 10.28.88.199...
* TCP_NODELAY set
* Connected to ab.text.com (10.28.88.199) port 80 (#0)
> POST / HTTP/1.1
> Host: ab.text.com
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Length: 16
> Content-Type: application/x-www-form-urlencoded


#加了 -G  请求方法立马变get,并且URI变成/?c=wwert&count=30
$curl -v  -G -d 'c=wwert' -d 'count=30'  ab.text.com
*   Trying 10.28.88.199...
* TCP_NODELAY set
* Connected to ab.text.com (10.28.88.199) port 80 (#0)
> GET /?c=wwert&count=30 HTTP/1.1
> Host: ab.text.com
> User-Agent: curl/7.54.0
> Accept: */*

#在加个 -I 请求方法立马变为HEAD
$curl -v -GI -d 'c=wwert' -d 'count=30'  ab.text.com
*   Trying 10.28.88.199...
* TCP_NODELAY set
* Connected to ab.text.com (10.28.88.199) port 80 (#0)
> HEAD /?c=wwert&count=30 HTTP/1.1
> Host: ab.text.com
> User-Agent: curl/7.54.0
> Accept: */*
>

  • -i 获取header信息及响应内容(默认不打印请求头信息)
$curl -i ab.text.com/a.html   # -i 只显示响应头信息及访问内容
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 23 Oct 2019 03:53:28 GMT
Content-Type: text/html; charset=utf8
Content-Length: 143
Last-Modified: Tue, 15 Oct 2019 03:59:21 GMT
Connection: keep-alive
ETag: "5da54419-8f"
Accept-Ranges: bytes

#这是a页面
是不是很意外?是不是很惊喜?这就是我的a页面。
    <!--引入b.html-->
    <!--# include file="b.html" -->
tieshan@ytsdeMacBook-Air:~$
  •  -I 只获取header信息(默认不打印请求头信息)
$curl -I ab.text.com/a.html
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 23 Oct 2019 03:55:32 GMT
Content-Type: text/html; charset=utf8
Content-Length: 143
Last-Modified: Tue, 15 Oct 2019 03:59:21 GMT
Connection: keep-alive
ETag: "5da54419-8f"
Accept-Ranges: bytes

 

  • --interface <name> 指定某个接口执行操作,接口名称可以是主机名或主机名 例如:  curl --interface eth0:1 http://www.netscape.com/(没看见怎么用过)
  • -k 跳过ssl检测进行访问(用于显示不安全ssl连接时)

-K 从某个配置文件中获取curl参数

文件内容示例:以Example开头,以Example结尾

# --- Example file ---
# this is a comment
url = "curl.haxx.se"
output = "curlhere.html"
user-agent = "superagent/1.0"

# and fetch another URL too
url = "curl.haxx.se/docs/manpage.html"
 -O
referer = "http://nowhereatall.com/"
# --- End of example file ---

  • --keepalive-time <seconds> 设置连接超时时间
  • --key 指定私钥,未指定私钥文件,默认以“〜/ .ssh / id_rsa”,“〜/ .ssh / id_dsa”,“ ./ id_rsa”,“ ./ id_dsa”方式读取私钥,存在多个,则使用最后一个。

  • --key-type <type> 指定私钥文件类型,默认是PEM,支持DER,PEM,ENG等文件类型

  • -l 列出FTP目录


  • -L 进行重定向页面跟踪,用于获取重定向的页面,curl默认不获取重定向页面
  • --limit-rate <speed> 进行传输限速,一般用于模拟网速慢的场景,支持k,m,g 例如:200k,3m.1g
  • -m 指定整个操作花费的最长时间,超过此时间,不管操作有没有完成,都会断开连接
  • --max-filesize <bytes> 指定最大下载文件大小,如果文件大于此值,则不进行传输
  • -N 禁止缓冲,默认情况下curl使用标准缓冲进行输出也就是分块传输数据

  • -o 将响应内容写到文件,文件名需自定义
$curl -o ssd ab.text.com/a.html  #-o 需要在后面加文件名 类似wget下载功能
$ls ssd
ssd
  • -O 将响应内容保存到本地,默认以URI为文件名
$curl -O  ab.text.com/a.html
$ls a.html
a.html

  • -p 使用http代理进行访问 等于 -x --proxy
  • -P +端口 以被动模式访问FTP
  • --post301 在进行301重定向时,curl不得将post请求转换为get请求
  • --post302 在进行302重定向时,curl不得将post请求转换为get请求
  • --post303 在进行303重定向时,curl不得将post请求转换为get请求
  • --proxy-anyauth 在代理上选择任一身份验证
  • --proxy-basic 在代理上选择基本身份验证
  • --proxy-digest 在代理上选择数字验证
  • --proxy-ntlm 在代理商选择ntlm验证
  • -proxy1.0  <代理主机[:端口]> 指定http1.0进行代理,默认是1080端口 这里是http1.0协议 -x proxy是默认使用http1.1协议
  • -r --range <range>  检索来自HTTP/1.1或FTP服务器字节范围,前提是服务器开启此功能。
  • -R 在本地生成文件时,保留文件在服务器最后修改的时间

  • –resolve HOST:PORT:ADDRESS  将 HOST:PORT 强制解析到 ADDRESS(一般是用于查询备用服务器上的东西,也就是没有做dns解析的网站,dns做了解析的通过curl +url方式可以获取页面内容,没有做解析的话,只能通过–resolve强制调整,或者在hosts里面添加对应的记录,指定对应的域名及ip也可以获取url结果,不建议后者,太麻烦了。
$curl --resolve m.2234sad.com:80:192.168.1.11 http://m.2234sad.com/games/jinbeihaolemenqipai/

#直接获取的是当前网页信息

  •   --retry <num> 指定在失败后的重试次数
  •  -s 静音模式 不显示进度表及错误信息
  • -S 显示错误  与 -s 一起使用时,发生错误的时候只显示为一条错误信息 
  • --socks4 <host[:port]> 指定socks4代理,不填写端口,默认端口为1080,同时也会覆盖 -x --proxy使用方法
  • --socks5-basic 使用socks5代理连接时,需要进行用户及密码验证

  • -T 进行文件上传,需要服务器支持此方法才行
语法:
curl -T "{file1,file2}" http://www.uploadtothissite.com

示例:
$curl -v -T header.txt ab.text.com/user

  • --unix-socket <path> 使用unix套接字进行连接
  •  -u  <user:password>  输入服务器身份验证的用户名及密码   curl -u 'ab:123456' ab.text.com/login
  • -U <user:password> 输入代理身份验证用户名及密码   curl -U 'ab:123456' ab.text.com/login
  • --url <URL> 指定获取URL

  • -v 输出整个通信过程,可以跟 -I 联合使用,只显示所有的header头部信息
$curl -v  ab.text.com/a.html
#显示通信信息
*   Trying 10.28.88.199...
* TCP_NODELAY set
* Connected to ab.text.com (10.28.88.199) port 80 (#0)

#显示请求头信息
> GET /a.html HTTP/1.1
> Host: ab.text.com
> User-Agent: curl/7.54.0
> Accept: */*

#显示响应信息
< HTTP/1.1 200 OK
< Server: nginx
< Date: Wed, 23 Oct 2019 06:16:13 GMT
< Content-Type: text/html; charset=utf8
< Content-Length: 143
< Last-Modified: Tue, 15 Oct 2019 03:59:21 GMT
< Connection: keep-alive
< ETag: "5da54419-8f"
< Accept-Ranges: bytes

#显示响应体信息
#这是a页面
是不是很意外?是不是很惊喜?这就是我的a页面。
    <!--引入b.html-->
    <!--# include file="b.html" -->
* Connection #0 to host ab.text.com left intact

  • -w 在命令完成后输出哪些信息,具体参数如下: 
-w 后面的可以使用的参数
url_effective
http_code 状态码
http_connect
time_total 请求总用时
time_namelookup DNS 域名解析的时候,就是把 https://baidu.com 转换成 ip 地址的过程
time_connect TCP 连接建立的时间,就是三次握手的时间
time_appconnect SSL/SSH 等上层协议建立连接的时间,比如 connect/handshake 的时间
time_redirect 从开始到最后一个请求事务的时间
time_pretransfer 从请求开始到响应开始传输的时间
time_starttransfer 从请求开始到第一个字节将要传输的时间
size_download
size_upload
size_header
size_request
speed_download
speed_upload
content_type
num_connects
num_redirects
ftp_entry_path

#示例 -w意思是将你想要获取的内容显示出来,如果不加-o,将获取整个标头信息
$ curl -sI -w "%{http_code}" -o /dev/null  http://www.pc6.com/az/403867.html
200
  • -x <[协议://] [user:password @] proxyhost [:port]>  指定http代理访问,如果没有指定协议默认使用http协议,端口默认为1080端口,还有个前提你访问的网站得支持代理才行 

       在7.21.7版本之后可以使用 protocol:// 这种方式代理,跟-x一样的用法,并且默认为http代理

        其他几个特殊代理:socks4://  socks4a://  socks5://(这3个是原来的意思)  socks5h://(这个特殊些,是指--socks5-hostname)

$ curl -v -x 192.168.1.11:80 http://m.2234sad.com/games/jinbeihaolemenqipai/

* About to connect() to proxy 175.6.2.179 port 80 (#0)
*   Trying 192.168.1.11...
* Connected to 192.168.1.11 (192.168.1.11) port 80 (#0)
> GET http://m.2234sad.com/games/jinbeihaolemenqipai/ HTTP/1.1
> User-Agent: curl/7.29.0
> Host: m.22234sad.com
> Accept: */*
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx
< Date: Sun, 29 Sep 2019 01:02:53 GMT
< Content-Type: text/html
< Content-Length: 178
< Connection: keep-alive
< Location: https://m.2234sad.com/games/alogdyan/
<
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Connection #0 to host 192.168.1.11 left intact
  • -X 指定请求方法,默认为GET
$ curl -H "Content-Type:application/json" -XPUT 'http://10.28.88.199:9200/ac/book/1?pretty' -d '{"title":"创始道纪"}' 

  • -z  <date expression>|<file> 获取晚于此最后修改时间最后的文件或者是在该时间之前修改的文件,比如我要获取3天之前文件,类似于find  ctime参数

  • -h 获取帮助
  • -M 打开man手册
  • -V 显示跟curl版本相关信息
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值