前言
CURL是Linux中使用比较多的软件,我也有所耳闻,我也曾经使用过,不过使用的时间间隔的比较长了,也不知道这篇文章会写多长时间全当记录吧。
内容
首先看看其帮助的内容,一般cmd帮助中都有其全部命令的信息(不然怎么用啊
1# curl --help
2Usage: curl [options...] <url>
3Options: (H) means HTTP/HTTPS only, (F) means FTP only
4 --anyauth Pick "any" authentication method (H)
5 -a, --append Append to target file when uploading (F/SFTP)
6 --basic Use HTTP Basic Authentication (H)
7 --cacert FILE CA certificate to verify peer against (SSL)
8 --capath DIR CA directory to verify peer against (SSL)
9 -E, --cert CERT[:PASSWD] Client certificate file and password (SSL)
10 --cert-type TYPE Certificate file type (DER/PEM/ENG) (SSL)
11 --ciphers LIST SSL ciphers to use (SSL)
12 --compressed Request compressed response (using deflate or gzip)
13 -K, --config FILE Specify which config file to read
14 --connect-timeout SECONDS Maximum time allowed for connection
15 -C, --continue-at OFFSET Resumed transfer offset
16 -b, --cookie STRING/FILE String or file to read cookies from (H)
17 -c, --cookie-jar FILE Write cookies to this file after operation (H)
18 --create-dirs Create necessary local directory hierarchy
19 --crlf Convert LF to CRLF in upload
20 --crlfile FILE Get a CRL list in PEM format from the given file
21 -d, --data DATA HTTP POST data (H)
22 --data-ascii DATA HTTP POST ASCII data (H)
23 --data-binary DATA HTTP POST binary data (H)
24 --data-urlencode DATA HTTP POST data url encoded (H)
25 --delegation STRING GSS-API delegation permission
26 --digest Use HTTP Digest Authentication (H)
27 --disable-eprt Inhibit using EPRT or LPRT (F)
28 --disable-epsv Inhibit using EPSV (F)
29 -D, --dump-header FILE Write the headers to this file
30 --egd-file FILE EGD socket path for random data (SSL)
31 --engine ENGINGE Crypto engine (SSL). "--engine list" for list
32 -f, --fail Fail silently (no output at all) on HTTP errors (H)
33 -F, --form CONTENT Specify HTTP multipart POST data (H)
34 --form-string STRING Specify HTTP multipart POST data (H)
35 --ftp-account DATA Account data string (F)
36 --ftp-alternative-to-user COMMAND String to replace "USER [name]" (F)
37 --ftp-create-dirs Create the remote dirs if not present (F)
38 --ftp-method [MULTICWD/NOCWD/SINGLECWD] Control CWD usage (F)
39 --ftp-pasv Use PASV/EPSV instead of PORT (F)
40 -P, --ftp-port ADR Use PORT with given address instead of PASV (F)
41 --ftp-skip-pasv-ip Skip the IP address for PASV (F)
42 --ftp-pret Send PRET before PASV (for drftpd) (F)
43 --ftp-ssl-ccc Send CCC after authenticating (F)
44 --ftp-ssl-ccc-mode ACTIVE/PASSIVE Set CCC mode (F)
45 --ftp-ssl-control Require SSL/TLS for ftp login, clear for transfer (F)
46 -G, --get Send the -d data with a HTTP GET (H)
47 -g, --globoff Disable URL sequences and ranges using {} and []
48 -H, --header LINE Custom header to pass to server (H)
49 -I, --head Show document info only
50 -h, --help This help text
51 --hostpubmd5 MD5 Hex encoded MD5 string of the host public key. (SSH)
52 -0, --http1.0 Use HTTP 1.0 (H)
53 --ignore-content-length Ignore the HTTP Content-Length header
54 -i, --include Include protocol headers in the output (H/F)
55 -k, --insecure Allow connections to SSL sites without certs (H)
56 --interface INTERFACE Specify network interface/address to use
57 -4, --ipv4 Resolve name to IPv4 address
58 -6, --ipv6 Resolve name to IPv6 address
59 -j, --junk-session-cookies Ignore session cookies read from file (H)
60 --keepalive-time SECONDS Interval between keepalive probes
61 --key KEY Private key file name (SSL/SSH)
62 --key-type TYPE Private key file type (DER/PEM/ENG) (SSL)
63 --krb LEVEL Enable Kerberos with specified security level (F)
64 --libcurl FILE Dump libcurl equivalent code of this command line
65 --limit-rate RATE Limit transfer speed to this rate
66 -l, --list-only List only names of an FTP directory (F)
67 --local-port RANGE Force use of these local port numbers
68 -L, --location Follow redirects (H)
69 --location-trusted like --location and send auth to other hosts (H)
70 -M, --manual Display the full manual
71 --mail-from FROM Mail from this address
72 --mail-rcpt TO Mail to this receiver(s)
73 --mail-auth AUTH Originator address of the original email
74 --max-filesize BYTES Maximum file size to download (H/F)
75 --max-redirs NUM Maximum number of redirects allowed (H)
76 -m, --max-time SECONDS Maximum time allowed for the transfer
77 --metalink Process given URLs as metalink XML file
78 --negotiate Use HTTP Negotiate Authentication (H)
79 -n, --netrc Must read .netrc for user name and password
80 --netrc-optional Use either .netrc or URL; overrides -n
81 --netrc-file FILE Set up the netrc filename to use
82 -N, --no-buffer Disable buffering of the output stream
83 --no-keepalive Disable keepalive use on the connection
84 --no-sessionid Disable SSL session-ID reusing (SSL)
85 --noproxy List of hosts which do not use proxy
86 --ntlm Use HTTP NTLM authentication (H)
87 -o, --output FILE Write output to <file> instead of stdout
88 --pass PASS Pass phrase for the private key (SSL/SSH)
89 --post301 Do not switch to GET after following a 301 redirect (H)
90 --post302 Do not switch to GET after following a 302 redirect (H)
91 --post303 Do not switch to GET after following a 303 redirect (H)
92 -#, --progress-bar Display transfer progress as a progress bar
93 --proto PROTOCOLS Enable/disable specified protocols
94 --proto-redir PROTOCOLS Enable/disable specified protocols on redirect
95 -x, --proxy [PROTOCOL://]HOST[:PORT] Use proxy on given port
96 --proxy-anyauth Pick "any" proxy authentication method (H)
97 --proxy-basic Use Basic authentication on the proxy (H)
98 --proxy-digest Use Digest authentication on the proxy (H)
99 --proxy-negotiate Use Negotiate authentication on the proxy (H)
100 --proxy-ntlm Use NTLM authentication on the proxy (H)
101 -U, --proxy-user USER[:PASSWORD] Proxy user and password
102 --proxy1.0 HOST[:PORT] Use HTTP/1.0 proxy on given port
103 -p, --proxytunnel Operate through a HTTP proxy tunnel (using CONNECT)
104 --pubkey KEY Public key file name (SSH)
105 -Q, --quote CMD Send command(s) to server before transfer (F/SFTP)
106 --random-file FILE File for reading random data from (SSL)
107 -r, --range RANGE Retrieve only the bytes within a range
108 --raw Do HTTP "raw", without any transfer decoding (H)
109 -e, --referer Referer URL (H)
110 -J, --remote-header-name Use the header-provided filename (H)
111 -O, --remote-name Write output to a file named as the remote file
112 --remote-name-all Use the remote file name for all URLs
113 -R, --remote-time Set the remote file's time on the local output
114 -X, --request COMMAND Specify request command to use
115 --resolve HOST:PORT:ADDRESS Force resolve of HOST:PORT to ADDRESS
116 --retry NUM Retry request NUM times if transient problems occur
117 --retry-delay SECONDS When retrying, wait this many seconds between each
118 --retry-max-time SECONDS Retry only within this period
119 -S, --show-error Show error. With -s, make curl show errors when they occur
120 -s, --silent Silent mode. Don't output anything
121 --socks4 HOST[:PORT] SOCKS4 proxy on given host + port
122 --socks4a HOST[:PORT] SOCKS4a proxy on given host + port
123 --socks5 HOST[:PORT] SOCKS5 proxy on given host + port
124 --socks5-basic Enable username/password auth for SOCKS5 proxies
125 --socks5-gssapi Enable GSS-API auth for SOCKS5 proxies
126 --socks5-hostname HOST[:PORT] SOCKS5 proxy, pass host name to proxy
127 --socks5-gssapi-service NAME SOCKS5 proxy service name for gssapi
128 --socks5-gssapi-nec Compatibility with NEC SOCKS5 server
129 -Y, --speed-limit RATE Stop transfers below speed-limit for 'speed-time' secs
130 -y, --speed-time SECONDS Time for trig speed-limit abort. Defaults to 30
131 --ssl Try SSL/TLS (FTP, IMAP, POP3, SMTP)
132 --ssl-reqd Require SSL/TLS (FTP, IMAP, POP3, SMTP)
133 -2, --sslv2 Use SSLv2 (SSL)
134 -3, --sslv3 Use SSLv3 (SSL)
135 --ssl-allow-beast Allow security flaw to improve interop (SSL)
136 --stderr FILE Where to redirect stderr. - means stdout
137 --tcp-nodelay Use the TCP_NODELAY option
138 -t, --telnet-option OPT=VAL Set telnet option
139 --tftp-blksize VALUE Set TFTP BLKSIZE option (must be >512)
140 -z, --time-cond TIME Transfer based on a time condition
141 -1, --tlsv1 Use => TLSv1 (SSL)
142 --tlsv1.0 Use TLSv1.0 (SSL)
143 --tlsv1.1 Use TLSv1.1 (SSL)
144 --tlsv1.2 Use TLSv1.2 (SSL)
145 --tlsv1.3 Use TLSv1.3 (SSL)
146 --tls-max VERSION Use TLS up to VERSION (SSL)
147 --trace FILE Write a debug trace to the given file
148 --trace-ascii FILE Like --trace but without the hex output
149 --trace-time Add time stamps to trace/verbose output
150 --tr-encoding Request compressed transfer encoding (H)
151 -T, --upload-file FILE Transfer FILE to destination
152 --url URL URL to work with
153 -B, --use-ascii Use ASCII/text transfer
154 -u, --user USER[:PASSWORD] Server user and password
155 --tlsuser USER TLS username
156 --tlspassword STRING TLS password
157 --tlsauthtype STRING TLS authentication type (default SRP)
158 --unix-socket FILE Connect through this UNIX domain socket
159 -A, --user-agent STRING User-Agent to send to server (H)
160 -v, --verbose Make the operation more talkative
161 -V, --version Show version number and quit
162 -w, --write-out FORMAT What to output after completion
163 --xattr Store metadata in extended file attributes
164 -q If used as the first parameter disables .curlrc
其中有全部的内容 本文章中挑选常用的命令做测试。
直接请求 Get
如果我们不给其指定任何参数 ,其将使用Get请求请求,如果访问某一网址 则返回这个网页
命令行:
示例
返回结果:(因为太多了只是截取了一小部分
指定用户代理
何为用户代理 当我们使用浏览器等发起网络请求中,就自动的带上浏览器的用户代理,比如我们使用浏览器请求https://lihui.net的时候,使用chrome请求的时候如下图
最下面的一行,即是用户代理
在curl加入-A 参数即可以指定用户的user-agent
那问题来了 curl 不指定user-agent的时候是什么呢 当不指定user-agent的时候默认的为curl/{{version}}
如何去除呢 可以使用下面的命令可以将user-agent去除
携带用户cookie
使用参数 -b 可以使得curl 在交互中携带用户的cookie
命令行如下图所示 下面的命令会向example.com 发送名为A 值为B的cookie 如果要发送多个cookie 要以;分割(都在“”内)
示例
返回结果
因为我的网站没有设置相关的cookie认证 等操作,因此返回的页面是和上面的一样的。
我们可以发送cookie文件到远端的网站
发送cookie文件
命令行
既然有发送文件了,可不可以接受cookie呢
答案是肯定的,保存返回的cookie到文件到命令行如下图所示
总结
curl命令行的作用很大,需要学习的内容还有很多,熟悉命令行的作用也有很大。