curl命令 是一个利用URL规则在命令行下工作的文件传输工具。它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具。作为一款强力工具,curl支持包括HTTP、HTTPS、ftp等众多协议,还支持POST、cookies、认证、从指定偏移处下载部分文件、用户代理字符串、限速、文件大小、进度条等特征。
命令语法
命令选项 - -A/--user-agent 设置用户代理发送给服务器
- -b/--cookie cookie字符串或文件读取位置
- -c/--cookie-jar 操作结束后把cookie写入到这个文件中
- -C/--continue-at 断点续转
- -D/--dump-header 把header信息写入到该文件中
- -e/--referer 来源网址
- -f/--fail 连接失败时不显示http错误
- -o/--output 把输出写到该文件中
- -O/--remote-name 把输出写到该文件中,保留远程文件的文件名
- -r/--range 检索来自HTTP/1.1或FTP服务器字节范围
- -s/--silent 静音模式。不输出任何东西
- -T/--upload-file 上传文件
- -u/--user 设置服务器的用户和密码
- -w/--write-out [format] 什么输出完成后
- -x/--proxy 在给定的端口上使用HTTP代理
- -#/--progress-bar 进度条显示当前的传送状态
复制代码
文件下载
- curl命令可以用来执行下载、发送各种HTTP请求,指定HTTP头部等操作
- curl是将下载文件输出到stdout,将进度信息输出到stderr,不显示进度信息使用–silent选项。
- > curl https://rumenz.com --silent
复制代码
下载文件到指定的文件小写-o,大写 -O 保存文件和它的原始文件名 - > curl https://rumenz.com/1.html -o 2.html
复制代码
大写 -O 保存文件和它的原始文件名
- > curl https://rumenz.com/1.html -O
复制代码
--progress显示进度条
- > curl https://rumenz.com/2.html -o 2.html --progress
复制代码
断点续传 - > curl -O -u 'rumenz':'test' ftp://rumenz.com/jdk.tar.gz
复制代码
然后你的连接突然断开,你可以用以下命令继续下载
- > curl -C - -O -u 'rumenz':'test' ftp://rumenz.com/jdk.tar.gz
复制代码
注意断点续传的参数是-C, 要自动续传的话要使用 -C -, 否则需要手工指定断点的字节位置.
伪造请求来源 - > curl -e https://json.im https://rumenz.com
复制代码
参照页是位于HTTP头部中的一个字符串,用来表示用户是从哪个页面到达当前页面的,如果用户点击网页A中的某个连接,那么用户就会跳转到B网页,网页B头部的参照页字符串就包含网页A的URL。也可以使用--referer选项指定参照页字符串.
-H参数可以通过直接添加标头 Referer,达到同样效果
- > curl -H 'Referer: https://json.im' https://rumenz.com
复制代码
设置请求header - > curl -H "Host:rumenz.com" -H "accept-language:zh-cn" URL
复制代码
curl的带宽控制 - > curl --limit-rate 200k https://rumenz.com/1.html
复制代码
用curl进行认证
使用curl选项 -u 可以完成HTTP或者FTP的认证,可以指定密码,也可以不指定密码在后续操作中输入密码:
- > curl -u user:pwd https://rumenz.com
- > curl -u user https://rumenz.com
复制代码
只打印响应头 - > curl -I https://rumenz.com
- HTTP/1.1 200 OK
- Server: openresty/1.19.3.1
- Date: Wed, 02 Jun 2021 13:37:41 GMT
- Content-Type: text/html; charset=utf-8
- Connection: keep-alive
复制代码
使用curl模拟get请求
直接显示网页内容
- > curl https://json.im/1.txt
- 123
- 456
复制代码
显示请求头和网页内容
- > curl -i https://json.im/1.txt
- HTTP/1.1 200 OK
- Server: openresty
- Date: Wed, 02 Jun 2021 14:02:30 GMT
- Content-Type: text/plain
- Content-Length: 8
- Last-Modified: Wed, 02 Jun 2021 14:00:57 GMT
- Connection: keep-alive
- ETag: "60b78f19-8"
- Accept-Ranges: bytes
- 123
- 456
复制代码
直接显示网页内容
- > curl -l https://json.im/1.txt
复制代码
显示get请求全过程解析
- > curl -v https://json.im/1.txt
复制代码
·
使用curl模拟post请求 - > curl -d "param1=value1¶m2=value2" https://json.im/login
- > curl -d'login=rumenz&password=123' -X POST https://json.im/login
- > curl -d 'login=rumenz' -d 'password=123' -X POST https://json.im/login
复制代码
--data-urlencode 参数等同于-d,发送 POST请求的数据体,区别在于会自动将发送的数据进行 URL 编码.
- > curl --data-urlencode 'comment=hello world' https://json.im/login
复制代码
上传文本文件 - > curl -d '@data.txt' https://json.im/upload
复制代码
post json格式的数据 - > curl -l -H 'Content-type: application/json' -X POST -d '{"rumenz":"123"}' https://json.im/123.json
复制代码
向服务器发送 Cookie - > curl https://json.im --cookie "user=rumenz&pass=123456"
复制代码
Cookie写入到一个文件 - > curl -c cookies.txt https://json.im
复制代码
上传二进制文件
-F 参数用来向服务器上传二进制文件。
- > curl -F "file=@123.png" https://json.im/uploadfile
复制代码
上面命令会给 HTTP 请求加上标头 Content-Type: multipart/form-data ,然后将文件123…png作为file字段上传。
-F 参数可以指定MIME 类型。
- > curl -F 'file=@123.png;type=image/png' https://json.im/uploadfile
复制代码
上面命令指定 MIME 类型为image/png,否则 curl 会把 MIME 类型设为 application/octet-stream。
-F参数也可以指定文件名。
- > curl -F 'file=@123.png;filename=rumenz.png' https://json.im/uploadfile
复制代码
上面命令中,原始文件名为123.png,但是服务器接收到的文件名为rumenz.png。
请求跟随服务器的重定向
-L参数会让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。
- > curl -L -d 'rumenz=123' https://json.im/
复制代码
调试参数
-v 参数输出通信的整个过程,用于调试。
- > curl -v https://json.im/1.txt
- * Trying 150.109.147.28...
- * TCP_NODELAY set
- * Connected to json.im (150.109.147.28) port 443 (#0)
- * ALPN, offering h2
- * ALPN, offering http/1.1
- * Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
- * successfully set certificate verify locations:
- * CAfile: /etc/ssl/cert.pem
- CApath: none
- * TLSv1.2 (OUT), TLS handshake, Client hello (1):
- * TLSv1.2 (IN), TLS handshake, Server hello (2):
- * TLSv1.2 (IN), TLS handshake, Certificate (11):
- * TLSv1.2 (IN), TLS handshake, Server key exchange (12):
- * TLSv1.2 (IN), TLS handshake, Server finished (14):
- * TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
- * TLSv1.2 (OUT), TLS change cipher, Client hello (1):
- * TLSv1.2 (OUT), TLS handshake, Finished (20):
- * TLSv1.2 (IN), TLS change cipher, Client hello (1):
- * TLSv1.2 (IN), TLS handshake, Finished (20):
- * SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
- * ALPN, server accepted to use http/1.1
- * Server certificate:
- * subject: CN=json.im
- * start date: Apr 27 14:50:23 2021 GMT
- * expire date: Jul 26 14:50:23 2021 GMT
- * subjectAltName: host "json.im" matched cert's "json.im"
- * issuer: C=US; O=Let's Encrypt; CN=R3
- * SSL certificate verify ok.
- > GET /1.txt HTTP/1.1
- > Host: json.im
- > User-Agent: curl/7.54.0
- > Accept: */*
- >
- < HTTP/1.1 200 OK
- < Server: openresty
- < Date: Wed, 02 Jun 2021 14:31:36 GMT
- < Content-Type: text/plain
- < Content-Length: 8
- < Last-Modified: Wed, 02 Jun 2021 14:00:57 GMT
- < Connection: keep-alive
- < ETag: "60b78f19-8"
- < Accept-Ranges: bytes
- <
- 123
- 456
- * Connection #0 to host json.im left intact
复制代码
--trace参数也可以用于调试,还会输出原始的二进制数据。
- > curl --trace - https://json.im
复制代码
原文链接:https://rumenz.com/rumenbiji/linux-curl.html 微信公众号:入门小站
- 回复【1001】获取 linux常用命令速查手册
- 回复【10010】获取 阿里云ECS运维Linux系统诊断
- 回复【10012】获取 Linux学习笔记【强悍总结值得一看】
- 回复【10013】获取 shell简明教程
|