linux之curl使用技巧

获取页面内容

> curl https://json.im

显示 HTTP 头
> curl -I https://json.im HTTP/1.1 200 OK Server: openresty Date: Fri, 04 Jun 2021 07:38:32 GMT Content-Type: text/html Content-Length: 12864 Last-Modified: Thu, 29 Apr 2021 01:39:01 GMT Connection: keep-alive Vary: Accept-Encoding ETag: "608a0e35-3240" Expires: Fri, 04 Jun 2021 08:38:32 GMT Cache-Control: max-age=3600 Accept-Ranges: bytes

同时显示 HTTP 头和文件内容,使用 -i 选项
> curl -i https://json.im

将链接保存到文件
我们可以使用 > 符号将输出重定向到本地文件中。
> curl https://json.im > index.html

也可以通过 curl 自带的 -o/-O 选项将内容保存到文件中。
  • -o(小写的 o):结果会被保存到命令行中提供的文件名
  • -O(大写的 O):URL 中的文件名会被用作保存输出的文件名
> curl -o index.html https://json.im > curl -O https://json.im/index.html

注意:使用 -O 选项时,必须确保链接末尾包含文件名,否则 curl 无法正确保存文件。如果遇到链接中无文件名的情况,应该使用 -o 选项手动指定文件名,或使用重定向符号
同时下载多个文件
我们可以使用 -o-O选项来同时指定多个链接,按照以下格式编写命令:
> curl -O html https://json.im/1.html-O html https://json.im/2.html > curl -o p1.html https://json.im/1.html-o p2.html https://json.im/2.html

使用 -L 跟随链接重定向
如果直接使用 curl 打开某些被重定向后的链接,这种情况下就无法获取我们想要的网页内容。例如:
> curl http://json.im301 Moved Permanently 301 Moved Permanently openresty

而当我们通过浏览器打开该链接时,会自动跳转到 https://json.im 。此时我们想要 curl 做的,就是像浏览器一样跟随链接的跳转,获取最终的网页内容。我们可以在命令中添加 -L 选项来跟随链接重定向:
> curl -L http://json.im

使用 -A 自定义 User-Agent
我们可以使用 -A 来自定义用户 *,例如下面的命令将伪装成安卓火狐浏览器对网页进行请求:
> curl -A "Mozilla/5.0 (Android; Mobile; rv:35.0) Gecko/35.0 Firefox/35.0" https://json.im

使用 -H 自定义 header
当我们需要传递特定的 header 的时候,可以仿照以下命令来写:
> curl -H "Referer: https://rumenz.com" -H "User-Agent: Custom-User-Agent" https://json.im

header 中传递 Cookie
> curl -H "Cookie: JSESSIONID=xxx" https://json.im

使用-c保存Cookie
使用 cURL 访问页面的时候,默认是不会保存 Cookie 的
> curl -c "cookie.txt" https://json.im

使用 -b 读取 Cookie
> curl -b "cookie.txt" https://json.im

使用-d发送POST请求
有一个登录页面 https://json.im/login,只需要提交用户名和密码便可登录。我们可以使用 cURL 来完成这一 POST 请求,-d 用于指定发送的数据,-X 用于指定发送数据的方式
> curl -d "userName=rumenz&passwd=123456" -X POST https://json.im/login

在使用 -d 的情况下,如果省略 -X,则默认为 POST 方式:
> curl -d "userName=rumenz&passwd=123456" https://json.im/login

从文件中读取 data.txt文本
> curl -d "@data.txt" https://json.im/upload

恢复中断下载
> curl -C - -O https://json.im/jdk.tar.gz

从文件下载URL
如果将curl与xargs结合使用,则可以从文件中的URL列表中下载文件。
> xargs -n 1 curl -O < urls.txt

CURL设置代理
> curl https://json.im/-U user:password-x 127.0.0.1:3128

【linux之curl使用技巧】如果您的代理不需要身份验证,您可以跳过-U user:password 。
比如Nginx配置代理
server { listen3128; resolver8.8.8.8; proxy_connect; proxy_connect_allow443 563; proxy_connect_connect_timeout10s; proxy_connect_read_timeout10s; proxy_connect_send_timeout10s; location / { proxy_pass http://$host; proxy_set_header Host $host; } }

上传文件到ftp
> curl -u username:password -T jdk.tar.gz ftp://json.im

修改名称解析
> curl --resolve json.im:443:127.0.0.1 https://json.im:443/

https://json.im的查询将告诉 curl127.0.0.1请求该站点,而不是使用DNS或 /etc/hosts文件。
限制下载率
> curl --limit-rate 100K https://json.im/jdk.tar.gz -O

HTTP认证
有些网域需要HTTP认证,这时curl需要用到--user参数。
> curl --user name:passwd https://json.im

原文链接:https://rumenz.com/rumenbiji/...
微信公众号:入门小站

    推荐阅读