Linux commands (136) - curl command

curl command is a rule in the Shell terminal using the URL file transfer command line tool to work under; it supports upload and download files, and so is integrated transmission tool, but traditionally, used to refer to curl to download the tool.

As a powerful tool, curl support including HTTP, HTTPS, FTP, and other protocols, also supports POST, cookies, authentication, wherein part of the file download, the user agent string, the speed limit, the file size, the progress bar at a specified offset from the other ; do web automation processes and data retrieval.

2. Command Format

curl [OPTIONS] [URL...]

3. Option Description

-#, --progress-bar
	显示进度条
-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 NAME
	选择用于密码操作的OpenSSL加密引擎
--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[-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 FILE
	把输出写到指定文件中
-O, --remote-name
	把输出写到与远程文件同名的本地文件中
-p, --proxytunnel
	使用 HTTP 代理
--proxy-anyauth
	选择任一代理身份验证方法
--proxy-basic
	在代理上使用基本身份验证
--proxy-digest
	在代理上使用数字身份验证
 --proxy-ntlm
 	在代理上使用 ntlm 身份验证
-P, --ftp-port ADDRESS
	使用端口地址,而不是使用PASV
-q, --disable
	作为第一个参数,关闭 .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
	将对 stderr 的所有写入重定向到指定的文件
-t, --telnet-option OPT=VAL
	Telnet 选项设置
--trace FILE
	对指定文件进行 debug
--trace-ascii FILE
	启用对所有传入和传出数据(包括描述性信息)的完整跟踪转储到给定文件。使用“-”作为文件名将输出发送到 stdout
--trace-time
	跟踪详细输出时,添加时间戳
-T, --upload-file FILE
	上传文件
--url URL
	指定要提取的 URL
-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
	停止传输速度的限制,速度时间

4. Common examples

(1) obtaining data given page.

curl https://github.com/dablelv/CEasyLocalLog/archive/master.zip
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

(2) write the output to a file with the same name as a local remote file.

curl -O  https://github.com/jindeng/sz14.git
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   162  100   162    0     0    303      0 --:--:-- --:--:-- --:--:--   303

#查看文件
ls
sz14.git

(3) Silent Mode download stuff.

curl -s -O  https://github.com/jindeng/sz14.git

(4) require authorization to access a page that provides a user name and password are authorized by the -u option.

curl -uroot  https://github.com/jindeng/sz14.git
Enter host password for user 'root':

(5) to display a progress bar.

curl --progress-bar  -O  https://github.com/jindeng/sz14.git
######################################################################## 100.0%

(6) only in response to print header information.

curl -I https://github.com/jindeng/sz14.git

It can be printed out only by --head -I or HTTP header information.

(7) to authenticate with a curl.

curl -u user:pwd  https://github.com/jindeng/sz14.git

(8) to limit the download speed of curl.

curl URL --limit-rate 50k

Use --limit-rate limiting download speeds the curl, with the command k (kilobytes), and m (megabytes) to download the specified speed limit.

(9) specifies the maximum file size can be downloaded.

curl URL --max-filesize bytes

Can be downloaded using the specified maximum file size --max-filesize, if the file size exceeds the limit, then the command returns a non-zero exit code, it returns 0 if the command is normal.

(10) the user agent is provided with a curl.

curl URL -A "Mozilla/5.0"

Some sites visited will be prompted only use IE browser to access, because these sites set up to check the user agent, the user agent can use the curl is set to IE, so that you can visit. Use -A or --user-agent option.

(11) custom header information to the server. HTTP header of other information may also be sent using curl, use -H "header information" is transmitted a plurality of header information.

curl -H "Host:man.linuxde.net" -H "accept-language:zh-cn" URL

(12) curl setting cookies. Use --cookie option to specify a cookie, more than one cookie separated by a semicolon.

curl URL --cookie "user=root;pass=123456"

The cookie is saved as a file, use --cookie-jar option.

curl URL --cookie-jar cookie_file

(13) curl setting page reference string. Refer to page option to specify the use of --referer string.

curl --referer URL1 URL2

Refer to page is located in a string HTTP header, used to indicate which page the user is arriving from the current page, if the user clicks on a web page connected to A, then the user will jump to page B, page B head Refer to page contains the string portion a of the URL of the webpage.

(14) HTTP. curl can continue to download a specific file offset, which can specify a shift amount will be part of a file to download.

curl URL/File -C OFFSET

Offset in bytes integer, if let curl automatically infer the correct position using the resume:

curl -C - URL

(15) to fetch page contents to a file.

curl -o git.html https://github.com/jindeng/sz14.git

Http error is not displayed when (16) connection fails.

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>

(17) via ftp upload.

curl -T test.sql ftp://用户名:密码@ip:port/demo/curtain/bbstudy_files/ 

(18) via ftp download.

curl -u 用户名:密码 -O URL

references

[1] curl (1) Manual
[2] . CSDN [Linux] step by step learning Linux - curl command (193)

Published 519 original articles · won praise 1567 · Views 2.02 million +

Guess you like

Origin blog.csdn.net/K346K346/article/details/104743660