Linux curl 命令

2017-09-19|Categories: External cmd, Linux|

下载文件

如果不指定任何选项,curl会把URL包含的文件内容打印到标准输出,如果要下载文件,必须使用某些选项:

curl -LO http://php.net/get/php-7.1.9.tar.gz/from/this/mirror

  • -L, --location:访问某些URL时会被服务器重定向,指定-L选项之后,curl会继续访问重定向之后的URL。
    • -I, --head选项可以查询URL是否会被重定向,这个选项会输出HTTP响应头,其中的Location就是重定向之后的URL。
  • -O, --remote-name:提取URL最后一部分(最后一个/之后的字符)作为下载文件的名称,在这里就是mirror

很明显,我要下载的文件是php-7.1.9.tar.gz,而-O选项自动把文件保存为mirror不符合我的要求,这时候就需要-o(小写字母o)指定文件名:

curl -L -o php-7.1.9.tar.gz http://php.net/get/php-7.1.9.tar.gz/from/this/mirror

有没有办法让下载的文件直接使用它在服务器上的名称呢?有,但有条件:-J, --remote-header-name可以使用服务器指定的Content-Disposition文件名,如果服务器没有指定或指定的格式不正确,-J选项就无能为力,例如:

# 成功,保存为`molokai.vim`
curl -JLO http://www.vim.org/scripts/download_script.php?src_id=9750

# 失败,保存为`mirror`
curl -JLO http://php.net/get/php-7.1.9.tar.gz/from/this/mirror

相比之下,wget解决这个问题的成功率更高一些:

# `wget --content-disposition`
# 成功,保存为`php-7.1.9.tar.gz`
wget --content-disposition http://php.net/get/php-7.1.9.tar.gz/from/this/mirror
# 成功,保存为`molokai.vim`
wget --content-disposition http://www.vim.org/scripts/download_script.php?src_id=9750

# `wget --trust-server-names`
# 成功,保存为`php-7.1.9.tar.gz`
wget --trust-server-names http://php.net/get/php-7.1.9.tar.gz/from/this/mirror
# 失败,保存为`download_script.php?src_id=9750`
wget --trust-server-names http://www.vim.org/scripts/download_script.php?src_id=9750

需要注意的是,curl -Jwget --content-disposition --trust-server-names都有安全风险,尤其是在Windows系统上:恶意服务器可以指定*.dll之类可被自动执行的文件名,使用时务必慎重。

用curl查字典

https://linuxcommando.blogspot.com/2007/10/dictionary-lookup-via-command-line.html

语法:

curl dict://dict.org/d:YourWord

To help you remember, d: stands for DEFINE.

For example, to look up the word "bash", (all output in this article are greatly abbreviated for clarity)

curl dict://dict.org/d:bash

151 "Bash" gcide "The Collaborative International Dictionary of English v.0.48"
Bash \Bash\, n.
1. a forceful blow, especially one that does damage to its
target.
[PJC]
2. a elaborate or lively social gathering or party.
[PJC]

The dict.org web site supports 77 dictionaries. To list them:

curl dict://dict.org/show:db

So, if you want to know the computer meaning of "bash", specify foldoc "The Free On-line Dictionary of Computing" like this:

curl dict://dict.org/d:bash:foldoc

151 "bash" foldoc "The Free On-line Dictionary of Computing (27 SEP 03)"
bash
Bourne Again SHell. {GNU}'s {command interpreter} for {Unix}.
Bash is a {Posix}-compatible {shell} with full {Bourne shell}
syntax, and some {C shell} commands built in. The Bourne
Again Shell supports {Emacs}-style command-line editing, job
control, functions, and on-line help. Written by Brian Fox of
{UCSB}.

To display results from all libraries, do this:

curl dict://dict.org/d:bash:*

Fuzzy matching is also possible. Replace the "d" or Define command with "m" which stands for MATCH.

curl dict://dict.org/m:bash

With the match command, you can also specify the strategy such as exact, prefix, suffix, or even soundex.

curl dict://dict.org/m:bash::prefix

gcide "Bash"
gcide "Bashed"
gcide "Bashful"
gcide "bashful Billy"

To list all supported strategies:

curl dict://dict.org/show:strat

查看HTTP请求头 2018-01-24

HTTP响应头(Response header)可以用-I选项查看,而请求头(Request header)可以用-v选项查看。

  • -s, --silent:安静模式,不显示进度条或错误信息。
  • > /dev/null:只显示-v选项的输出,避免重复。

大于号>后面就是请求头信息,小于号<后面是响应头信息:

$ curl -Ivs http://baidu.com > /dev/null
* Rebuilt URL to: http://baidu.com/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 6152 (#0)
> HEAD http://baidu.com/ HTTP/1.1
> Host: baidu.com
> User-Agent: curl/7.54.0
> Accept: */*
> Referer:
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 200 OK
< Date: Wed, 24 Jan 2018 11:17:28 GMT
< Server: Apache
< Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
< ETag: "51-47cf7e6ee8400"
< Accept-Ranges: bytes
< Content-Length: 81
< Cache-Control: max-age=86400
< Expires: Thu, 25 Jan 2018 11:17:28 GMT
< Connection: Keep-Alive
< Content-Type: text/html
<
* Connection #0 to host 127.0.0.1 left intact

查看本机的公网IP 2018-02-21

curl ipinfo.io/ip

查看本机的私网(局域网)IP稍微麻烦一点:

# `eth0`是CentOS 6默认的网卡名称,
# CentOS 7默认的网卡名称是`ens33`
ifconfig eth0 | grep netmask | awk '{print $2}'

忽略系统代理设置 2018-04-06

某些时候测试走代理会报错,例如测试局域网地址,此时可以用--noproxy '*'忽略所有系统代理设置:

curl --noproxy '*' -vs 172.16.125.71 >/dev/null

Leave A Comment