本文共 6919 字,大约阅读时间需要 23 分钟。
00. 目录
文章目录
01. 命令概述
curl命令是一个利用URL规则在shell终端命令行下工作的文件传输工具;它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具。
作为一款强力工具,curl支持包括HTTP、HTTPS、ftp等众多协议,还支持POST、cookies、认证、从指定偏移处下载部分文件、用户代理字符串、限速、文件大小、进度条等特征;做网页处理流程和数据检索自动化。
02. 命令格式
用法: curl [选项] [URL...]
03. 常用选项
-a/--append 上传文件时,附加到目标文件-A/--user-agent设置用户代理发送给服务器-anyauth 可以使用“任何”身份验证方法-b/--cookie cookie字符串或文件读取位置 --basic 使用HTTP基本验证-B/--use-ascii 使用ASCII /文本传输-c/--cookie-jar 操作结束后把cookie写入到这个文件中-C/--continue-at 断点续转-d/--data HTTP POST方式传送数据 --data-ascii 以ascii的方式post数据 --data-binary 以二进制的方式post数据 --negotiate 使用HTTP身份验证 --digest 使用数字身份验证 --disable-eprt 禁止使用EPRT或LPRT --disable-epsv 禁止使用EPSV-D/--dump-header 把header信息写入到该文件中 --egd-file 为随机数据(SSL)设置EGD socket路径 --tcp-nodelay 使用TCP_NODELAY选项-e/--referer 来源网址-E/--cert 客户端证书文件和密码 (SSL) --cert-type 证书文件类型 (DER/PEM/ENG) (SSL) --key 私钥文件名 (SSL) --key-type 私钥文件类型 (DER/PEM/ENG) (SSL) --pass 私钥密码 (SSL) --engine 加密引擎使用 (SSL). "--engine list" for list --cacert CA证书 (SSL) --capath CA目录 (made using c_rehash) to verify peer against (SSL) --ciphers SSL密码 --compressed 要求返回是压缩的形势 (using deflate or gzip) --connect-timeout
设置最大请求时间 --create-dirs 建立本地目录的目录层次结构 --crlf 上传是把LF转变成CRLF-f/--fail 连接失败时不显示http错误 --ftp-create-dirs 如果远程目录不存在,创建远程目录 --ftp-method [multicwd/nocwd/singlecwd] 控制CWD的使用 --ftp-pasv 使用 PASV/EPSV 代替端口 --ftp-skip-pasv-ip 使用PASV的时候,忽略该IP地址 --ftp-ssl 尝试用 SSL/TLS 来进行ftp数据传输 --ftp-ssl-reqd 要求用 SSL/TLS 来进行ftp数据传输-F/--form 模拟http表单提交数据 --form-string 模拟http表单提交数据-g/--globoff 禁用网址序列和范围使用{ }和[]-G/--get 以get的方式来发送数据-H/--header 自定义头信息传递给服务器 --ignore-content-length 忽略的HTTP头信息的长度-i/--include 输出时包括protocol头信息-I/--head 只显示请求头信息-j/--junk-session-cookies 读取文件进忽略session cookie --interface 使用指定网络接口/地址 --krb4 使用指定安全级别的krb4-k/--insecure 允许不使用证书到SSL站点-K/--config 指定的配置文件读取-l/--list-only 列出ftp目录下的文件名称 --limit-rate 设置传输速度 --local-port 强制使用本地端口号-m/--max-time 设置最大传输时间 --max-redirs 设置最大读取的目录数 --max-filesize 设置最大下载的文件总量-M/--manual 显示全手动-n/--netrc 从netrc文件中读取用户名和密码 --netrc-optional 使用 .netrc 或者 URL来覆盖-n --ntlm 使用 HTTP NTLM 身份验证-N/--no-buffer 禁用缓冲输出-o/--output 把输出写到该文件中-O/--remote-name 把输出写到该文件中,保留远程文件的文件名-p/--proxytunnel 使用HTTP代理 --proxy-anyauth 选择任一代理身份验证方法 --proxy-basic 在代理上使用基本身份验证 --proxy-digest 在代理上使用数字身份验证 --proxy-ntlm 在代理上使用ntlm身份验证-P/--ftp-port 使用端口地址,而不是使用PASV-q 作为第一个参数,关闭 .curlrc-Q/--quote 文件传输前,发送命令到服务器-r/--range 检索来自HTTP/1.1或FTP服务器字节范围--range-file 读取(SSL)的随机文件-R/--remote-time 在本地生成文件时,保留远程文件时间 --retry 传输出现问题时,重试的次数 --retry-delay 传输出现问题时,设置重试间隔时间 --retry-max-time 传输出现问题时,设置最大重试时间-s/--silent 静默模式。不输出任何东西-S/--show-error 显示错误 --socks4 用socks4代理给定主机和端口 --socks5 用socks5代理给定主机和端口 --stderr -t/--telnet-option Telnet选项设置 --trace 对指定文件进行debug --trace-ascii Like --跟踪但没有hex输出 --trace-time 跟踪/详细输出时,添加时间戳-T/--upload-file 上传文件 --url Spet URL to work with-u/--user 设置服务器的用户和密码-U/--proxy-user 设置代理用户名和密码-w/--write-out [format] 什么输出完成后-x/--proxy 在给定的端口上使用HTTP代理-X/--request 指定什么命令-y/--speed-time 放弃限速所要的时间,默认为30-Y/--speed-limit 停止传输速度的限制,速度时间
04. 参考示例
4.1 获取指定网页的数据
[root@localhost test]# curl https://github.com/jindeng/sz14.git301 Moved Permanently 301 Moved Permanently
nginx
4.2 把输出写到该文件中,保留远程文件的文件名
[root@localhost test]# curl -O https://github.com/jindeng/sz14.git % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed100 178 100 178 0 0 326 0 --:--:-- --:--:-- --:--:-- 326[root@localhost test]# lssz14.git[root@localhost test]#
4.3 静默模式下载东西
[root@localhost test]# curl --silent -O https://github.com/jindeng/sz14.git[root@localhost test]#
4.4 访问需要授权的页面时,可通过-u选项提供用户名和密码进行授权
[root@localhost test]# curl -uroot https://github.com/jindeng/sz14.gitEnter host password for user 'root':
4.5 显示进度条
[root@localhost test]# curl --progress -O https://github.com/jindeng/sz14.git######################################################################## 100.0%[root@localhost test]#
4.6 只打印响应头部信息
[root@localhost test]# curl -I https://github.com/jindeng/sz14.git HTTP/1.1 301 Moved PermanentlyDate: Fri, 30 Aug 2019 03:27:56 GMTContent-Type: text/htmlContent-Length: 178Server: GitHub.comLocation: https://github.com/jindeng/sz14Vary: Accept-EncodingX-Frame-Options: DENYVary: Accept-EncodingX-GitHub-Request-Id: 38DA:0255:A54A7D:E96FA2:5D6897BB[root@localhost test]#
通过-I
或者--head
可以只打印出HTTP头部信息。
4.7 用curl进行认证
[root@localhost test]# curl -u user:pwd https://github.com/jindeng/sz14.git[root@localhost test]#
4.8 限制curl的下载速度
[root@localhost test]# curl URL --limit-rate 50k[root@localhost test]#
使用--limit-rate
限制curl的下载速度,命令中用k(千字节)和m(兆字节)指定下载速度限制。
4.9 指定可下载的最大文件大小
[root@localhost test]# curl URL --max-filesize bytes[root@localhost test]#
使用--max-filesize
指定可下载的最大文件大小,如果文件大小超出限制,命令则返回一个非0退出码,如果命令正常则返回0。
4.10 用curl设置用户代理
[root@localhost test]# curl URL --user-agent "Mozilla/5.0"#或者[root@localhost test]# curl URL -A "Mozilla/5.0"
有些网站访问会提示只能使用IE浏览器来访问,这是因为这些网站设置了检查用户代理,可以使用curl把用户代理设置为IE,这样就可以访问了。使用--user-agent
或者-A
选项。
4.11 自定义头信息传递给服务器
其他HTTP头部信息也可以使用curl来发送,使用-H
"头部信息" 传递多个头部信息。
[root@localhost test]# curl -H "Host:man.linuxde.net" -H "accept-language:zh-cn" URL[root@localhost test]#
4.12 curl设置cookies
使用--cookie "COKKIES"
选项来指定cookie,多个cookie使用分号分隔。
[root@localhost test]# curl URL --cookie "user=root;pass=123456"[root@localhost test]#
将cookie另存为一个文件,使用--cookie-jar
选项
[root@localhost test]# curl URL --cookie-jar cookie_file
4.13 curl设置参照页字符串
使用--referer
选项指定参照页字符串
[root@localhost test]# curl --referer URL1 URL2
参照页是位于HTTP头部中的一个字符串,用来表示用户是从哪个页面到达当前页面的,如果用户点击网页A中的某个连接,那么用户就会跳转到B网页,网页B头部的参照页字符串就包含网页A的URL。
4.14 断点续传
curl能够从特定的文件偏移处继续下载,它可以通过指定一个便移量来下载部分文件。
[root@localhost test]# curl URL/File -C 偏移量
偏移量是以字节为单位的整数,如果让curl自动推断出正确的续传位置使用-C -
[root@localhost test]# curl -C -URL
4.15 抓取页面内容到一个文件中
[root@localhost test]# curl -o git.html https://github.com/jindeng/sz14.git % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 100 178 100 178 0 0 322 0 --:--:-- --:--:-- --:--:-- 323[root@localhost test]# lsgit.html[root@localhost test]#
4.16 显示下载错误信息
[root@localhost test]# curl -f https://github.com/jindeng/sz14.git301 Moved Permanently 301 Moved Permanently
nginx [root@localhost test]#
4.17 通过ftp上传
[root@localhost test]# curl -T test.sql ftp://用户名:密码@ip:port/demo/curtain/bbstudy_files/
4.18 通过ftp下载
[root@localhost test]# curl -u 用户名:密码 -O URL
4.19 显示下载进度
[root@localhost test]# curl -# -O https://github.com/jindeng/sz14.git######################################################################## 100.0%[root@localhost test]#
4.20 不显示下载进度
[root@localhost test]# curl -s -o test.git https://github.com/jindeng/sz14.git[root@localhost test]#
05. 附录
参考:
转载地址:https://dengjin.blog.csdn.net/article/details/100145300 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!