首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

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

  • 23-11-18 13:21
  • 3301
  • 11410
blog.csdn.net

00. 目录

文章目录

    • 00. 目录
    • 01. 命令概述
    • 02. 命令格式
    • 03. 常用选项
    • 04. 参考示例
    • 05. 附录

01. 命令概述

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

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

02. 命令格式

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

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	停止传输速度的限制,速度时间
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4.3 静默模式下载东西

[root@localhost test]# curl --silent -O  https://github.com/jindeng/sz14.git
[root@localhost test]# 
  • 1
  • 2

4.4 访问需要授权的页面时,可通过-u选项提供用户名和密码进行授权

[root@localhost test]# curl -uroot  https://github.com/jindeng/sz14.git
Enter host password for user 'root':
  • 1
  • 2

4.5 显示进度条

[root@localhost test]# curl --progress  -O  https://github.com/jindeng/sz14.git
######################################################################## 100.0%
[root@localhost test]# 
  • 1
  • 2
  • 3

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]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

通过-I或者--head可以只打印出HTTP头部信息。

4.7 用curl进行认证

[root@localhost test]# curl -u user:pwd  https://github.com/jindeng/sz14.git
[root@localhost test]#
  • 1
  • 2

4.8 限制curl的下载速度

[root@localhost test]# curl URL --limit-rate 50k
[root@localhost test]#
  • 1
  • 2

使用--limit-rate限制curl的下载速度,命令中用k(千字节)和m(兆字节)指定下载速度限制。

4.9 指定可下载的最大文件大小

[root@localhost test]# curl URL --max-filesize bytes
[root@localhost test]#
  • 1
  • 2

使用--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"
  • 1
  • 2
  • 3
  • 4

有些网站访问会提示只能使用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]#
  • 1
  • 2

4.12 curl设置cookies

使用--cookie "COKKIES"选项来指定cookie,多个cookie使用分号分隔。

[root@localhost test]# curl URL --cookie "user=root;pass=123456"
[root@localhost test]#
  • 1
  • 2

将cookie另存为一个文件,使用--cookie-jar选项

[root@localhost test]# curl URL --cookie-jar cookie_file
  • 1

4.13 curl设置参照页字符串

使用--referer选项指定参照页字符串

[root@localhost test]# curl --referer URL1 URL2
  • 1

参照页是位于HTTP头部中的一个字符串,用来表示用户是从哪个页面到达当前页面的,如果用户点击网页A中的某个连接,那么用户就会跳转到B网页,网页B头部的参照页字符串就包含网页A的URL。

4.14 断点续传

curl能够从特定的文件偏移处继续下载,它可以通过指定一个便移量来下载部分文件。

[root@localhost test]# curl URL/File -C 偏移量
  • 1

偏移量是以字节为单位的整数,如果让curl自动推断出正确的续传位置使用-C -

[root@localhost test]# curl -C -URL
  • 1

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]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

4.17 通过ftp上传

[root@localhost test]# curl -T test.sql ftp://用户名:密码@ip:port/demo/curtain/bbstudy_files/ 
  • 1

4.18 通过ftp下载

[root@localhost test]# curl -u 用户名:密码 -O URL
  • 1

4.19 显示下载进度

[root@localhost test]# curl -# -O  https://github.com/jindeng/sz14.git
######################################################################## 100.0%
[root@localhost test]# 
  • 1
  • 2
  • 3

4.20 不显示下载进度

[root@localhost test]# curl -s -o test.git https://github.com/jindeng/sz14.git
[root@localhost test]# 
  • 1
  • 2

05. 附录

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

注:本文转载自blog.csdn.net的沧海一笑-dj的文章"https://blog.csdn.net/dengjin20104042056/article/details/100145300"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top