【Linux】一步一步学Linux——curl命令(193)

00. 目录

01. 命令概述

curl命令是一个利用URL规则在shell终端命令行下工作的文件传输工具;它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具。

作为一款强力工具,curl支持包括HTTP、HTTPS、ftp等众多协议,还支持POST、cookies、认证、从指定偏移处下载部分文件、用户代理字符串、限速、文件大小、进度条等特征;做网页处理流程和数据检索自动化。

02. 命令格式

用法: curl [选项] [URL...]

03. 常用选项

-a/--append	上传文件时,附加到目标文件
-A/--user-agent <string>	设置用户代理发送给服务器
-anyauth	可以使用“任何”身份验证方法
-b/--cookie <name=string/file>	cookie字符串或文件读取位置
     --basic	使用HTTP基本验证
-B/--use-ascii	使用ASCII /文本传输
-c/--cookie-jar <file>	操作结束后把cookie写入到这个文件中
-C/--continue-at <offset>	断点续转
-d/--data <data>	HTTP POST方式传送数据
     --data-ascii <data>	以ascii的方式post数据
     --data-binary <data>	以二进制的方式post数据
     --negotiate	使用HTTP身份验证
     --digest	使用数字身份验证
     --disable-eprt	禁止使用EPRT或LPRT
     --disable-epsv	禁止使用EPSV
-D/--dump-header <file>	把header信息写入到该文件中
     --egd-file <file>	为随机数据(SSL)设置EGD socket路径
     --tcp-nodelay	使用TCP_NODELAY选项
-e/--referer	来源网址
-E/--cert <cert[:passwd]>	客户端证书文件和密码 (SSL)
     --cert-type <type>	证书文件类型 (DER/PEM/ENG) (SSL)
     --key <key>	私钥文件名 (SSL)
     --key-type <type>	私钥文件类型 (DER/PEM/ENG) (SSL)
     --pass <pass>	私钥密码 (SSL)
     --engine <eng>	加密引擎使用 (SSL). "--engine list" for list
     --cacert <file>	CA证书 (SSL)
     --capath <directory>	CA目录 (made using c_rehash) to verify peer against (SSL)
     --ciphers <list>	SSL密码
     --compressed	要求返回是压缩的形势 (using deflate or gzip)
     --connect-timeout <seconds>	设置最大请求时间
     --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 <name=content>	模拟http表单提交数据
     --form-string <name=string>	模拟http表单提交数据
-g/--globoff	禁用网址序列和范围使用{}[]
-G/--get	以get的方式来发送数据
-H/--header <line>	自定义头信息传递给服务器
     --ignore-content-length	忽略的HTTP头信息的长度
-i/--include	输出时包括protocol头信息
-I/--head	只显示请求头信息
-j/--junk-session-cookies	读取文件进忽略session cookie
     --interface <interface>	使用指定网络接口/地址
     --krb4 <level>	使用指定安全级别的krb4
-k/--insecure	允许不使用证书到SSL站点
-K/--config	指定的配置文件读取
-l/--list-only	列出ftp目录下的文件名称
     --limit-rate <rate>	设置传输速度
     --local-port<NUM>	强制使用本地端口号
-m/--max-time <seconds>	设置最大传输时间
     --max-redirs <num>	设置最大读取的目录数
     --max-filesize <bytes>	设置最大下载的文件总量
-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 <address>	使用端口地址,而不是使用PASV
-q	作为第一个参数,关闭 .curlrc
-Q/--quote <cmd>	文件传输前,发送命令到服务器
-r/--range <range>	检索来自HTTP/1.1或FTP服务器字节范围
--range-file	读取(SSL)的随机文件
-R/--remote-time	在本地生成文件时,保留远程文件时间
     --retry <num>	传输出现问题时,重试的次数
     --retry-delay <seconds>	传输出现问题时,设置重试间隔时间
     --retry-max-time <seconds>	传输出现问题时,设置最大重试时间
-s/--silent	静默模式。不输出任何东西
-S/--show-error	显示错误
     --socks4 <host[:port]>	用socks4代理给定主机和端口
     --socks5 <host[:port]>	用socks5代理给定主机和端口
     --stderr <file>	
-t/--telnet-option <OPT=val>	Telnet选项设置
     --trace <file>	对指定文件进行debug
     --trace-ascii <file>	Like --跟踪但没有hex输出
     --trace-time	跟踪/详细输出时,添加时间戳
-T/--upload-file <file>	上传文件
     --url <URL>	Spet URL to work with
-u/--user <user[:password]>	设置服务器的用户和密码
-U/--proxy-user <user[:password]>	设置代理用户名和密码
-w/--write-out [format]	什么输出完成后
-x/--proxy <host[:port]>	在给定的端口上使用HTTP代理
-X/--request <command>	指定什么命令
-y/--speed-time	放弃限速所要的时间,默认为30
-Y/--speed-limit	停止传输速度的限制,速度时间

04. 参考示例

4.1 获取指定网页的数据

[root@localhost test]# curl https://github.com/jindeng/sz14.git
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

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  Speed
100   178  100   178    0     0    326      0 --:--:-- --:--:-- --:--:--   326
[root@localhost test]# ls
sz14.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.git
Enter 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 Permanently
Date: Fri, 30 Aug 2019 03:27:56 GMT
Content-Type: text/html
Content-Length: 178
Server: GitHub.com
Location: https://github.com/jindeng/sz14
Vary: Accept-Encoding
X-Frame-Options: DENY
Vary: Accept-Encoding
X-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]# ls
git.html
[root@localhost test]# 

4.16 显示下载错误信息

[root@localhost test]# curl -f https://github.com/jindeng/sz14.git
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
[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. 附录

参考:【Linux】一步一步学Linux系列教程汇总

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值