linux之curl命令

【linux之curl命令】宝剑锋从磨砺出,梅花香自苦寒来。这篇文章主要讲述linux之curl命令相关的知识,希望能为你提供帮助。
命令语法

> curl (选项)(参数)

命令选项
-A/--user-agent < string> 设置用户代理发送给服务器 -b/--cookie < name=string/file> cookie字符串或文件读取位置 -c/--cookie-jar < file> 操作结束后把cookie写入到这个文件中 -C/--continue-at < offset> 断点续转 -D/--dump-header < file> 把header信息写入到该文件中 -e/--referer来源网址 -f/--fail连接失败时不显示http错误 -o/--output把输出写到该文件中 -O/--remote-name把输出写到该文件中,保留远程文件的文件名 -r/--range < range> 检索来自HTTP/1.1或FTP服务器字节范围 -s/--silent静音模式。不输出任何东西 -T/--upload-file < file> 上传文件 -u/--user < user[:password]> 设置服务器的用户和密码 -w/--write-out [format]什么输出完成后 -x/--proxy < host[:port]> 在给定的端口上使用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

> curl https://rumenz.com/1.html -O

> curl https://rumenz.com/2.html -o 2.html --progress

断点续传
> curl -O -u \'rumenz\':\'test\' ftp://rumenz.com/jdk.tar.gz

伪造请求来源
> curl -e https://json.im https://rumenz.com

> 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 200khttps://rumenz.com/1.html

用curl进行认证
> 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: bytes123 456

> curl -l https://json.im/1.txt

> curl -v https://json.im/1.txt

·
使用curl模拟post请求
> curl -d "param1=value1& param2=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 POSThttps://json.im/login

> 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

上传二进制文件
> curl -F "file=@123.png" https://json.im/uploadfile

> curl -F \'file=@123.png; type=image/png\'https://json.im/uploadfile

> curl -F \'file=@123.png; filename=rumenz.png\' https://json.im/uploadfile

请求跟随服务器的重定向
> curl -L -d \'rumenz=123\' https://json.im/

调试参数
> 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

> curl --trace - https://json.im

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

    推荐阅读