常用系统管理员工具:wget
在处理与网络相关的故障时,wget
是一款系统管理员经常使用的极其有用的工具。
什么是 wget
命令?
wget
是一款在 Unix/Linux 系统上广泛应用的命令行实用工具,它的主要功能是从网络上下载内容。 这款免费的工具提供了一种非交互式的方式来获取网络上的文件。wget
原生支持 HTTPS、HTTP 和 FTP 协议,并且可以配置使用 HTTP 代理。
wget
如何协助故障排除?
wget
在故障排除方面有多种应用方式。
作为系统管理员,大部分工作时间您可能都在终端上度过。当您在解决与 Web 应用程序相关的问题时,可能并不需要浏览整个网页,而是仅仅想检查网络连接状况。或者,您可能需要验证内部网网站,或下载某个网页以检查其内容。
wget
是非交互式的,这意味着即使您已经退出登录,它仍然可以在后台运行。 在许多情况下,您可能需要从网络上检索文件,并且必须断开与系统的连接。这时,wget
可以在后台继续运行并完成它的任务。
此外,wget
还可以用来将整个网站下载到本地计算机。它可以递归地跟踪 HTML 和 XHTML 页面中的链接,从而创建网站的本地版本。这项功能对于下载重要的网页或站点以便离线查看非常有用。
下面让我们来了解一下它的实际应用。wget
的基本语法如下:
wget [选项] [URL]
下载网页
让我们尝试下载一个网页,例如:github.com
wget github.com
如果网络连接正常,它将下载该网页的主页,并显示如下类似的输出:
[email protected]:~# wget github.com
URL transformed to HTTPS due to an HSTS policy
--2020-02-23 10:45:52-- https://github.com/
Resolving github.com (github.com)... 140.82.118.3
Connecting to github.com (github.com)|140.82.118.3|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html’
index.html [ <=> ] 131.96K --.-KB/s in 0.04s
2020-02-23 10:45:52 (2.89 MB/s) - ‘index.html’ saved [135126]
[email protected]:~#
下载多个文件
当需要一次性下载多个文件时,wget
非常方便。 它允许您使用脚本自动执行文件下载。
让我们尝试下载 Python 3.8.1 和 3.5.1 的安装文件:
wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz
由此可见,其语法如下:
wget URL1 URL2 URL3
只需确保在 URL 之间留有空格即可。
限制下载速度
当您需要测试不同带宽下下载文件所花费的时间时,这个功能非常有用。
通过 --limit-rate
选项,您可以限制下载速度。
以下是下载 Node.js 文件的输出:
[email protected]:~# wget https://nodejs.org/dist/v12.16.1/node-v12.16.1-linux-x64.tar.xz
--2020-02-23 10:59:58-- https://nodejs.org/dist/v12.16.1/node-v12.16.1-linux-x64.tar.xz
Resolving nodejs.org (nodejs.org)... 104.20.23.46, 104.20.22.46, 2606:4700:10::6814:162e, ...
Connecting to nodejs.org (nodejs.org)|104.20.23.46|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 14591852 (14M) [application/x-xz]
Saving to: ‘node-v12.16.1-linux-x64.tar.xz’
node-v12.16.1-linux-x64.tar.xz 100%[===========================================================================================>] 13.92M --.-KB/s in 0.05s
2020-02-23 10:59:58 (272 MB/s) - ‘node-v12.16.1-linux-x64.tar.xz’ saved [14591852/14591852]
下载 13.92MB 的文件耗时 0.05 秒。现在,让我们尝试将速度限制为 500K:
[email protected]:~# wget --limit-rate=500k https://nodejs.org/dist/v12.16.1/node-v12.16.1-linux-x64.tar.xz
--2020-02-23 11:00:18-- https://nodejs.org/dist/v12.16.1/node-v12.16.1-linux-x64.tar.xz
Resolving nodejs.org (nodejs.org)... 104.20.23.46, 104.20.22.46, 2606:4700:10::6814:162e, ...
Connecting to nodejs.org (nodejs.org)|104.20.23.46|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 14591852 (14M) [application/x-xz]
Saving to: ‘node-v12.16.1-linux-x64.tar.xz.1’
node-v12.16.1-linux-x64.tar.xz.1 100%[===========================================================================================>] 13.92M 501KB/s in 28s
2020-02-23 11:00:46 (500 KB/s) - ‘node-v12.16.1-linux-x64.tar.xz.1’ saved [14591852/14591852]
降低带宽会导致下载时间增加,这里耗时 28 秒。 想象一下,如果您的用户抱怨下载速度慢,而您知道他们的网络带宽较低。您可以使用 --limit-rate
选项快速模拟这个问题。
在后台下载
下载大文件或使用速度限制选项,通常需要较长时间。 这很常见,但是如果您不想一直盯着终端屏幕该怎么办呢?
您可以使用 -b
参数在后台启动 wget
。
[email protected]:~# wget -b https://slack.com
Continuing in background, pid 25430.
Output will be written to ‘wget-log.1’.
[email protected]:~#
忽略证书错误
当您需要检查没有正确证书的内部网 Web 应用程序时,这个功能非常实用。默认情况下,如果证书无效,wget
会报错。
[email protected]:~# wget https://expired.badssl.com/
--2020-02-23 11:24:59-- https://expired.badssl.com/
Resolving expired.badssl.com (expired.badssl.com)... 104.154.89.105
Connecting to expired.badssl.com (expired.badssl.com)|104.154.89.105|:443... connected.
ERROR: cannot verify expired.badssl.com's certificate, issued by ‘CN=COMODO RSA Domain Validation Secure Server CA,O=COMODO CA Limited,L=Salford,ST=Greater Manchester,C=GB’:
Issued certificate has expired.
To connect to expired.badssl.com insecurely, use `--no-check-certificate'.
上述示例使用了证书过期的 URL。 正如您所见,它建议使用 --no-check-certificate
来忽略证书验证。
[email protected]:~# wget https://untrusted-root.badssl.com/ --no-check-certificate
--2020-02-23 11:33:45-- https://untrusted-root.badssl.com/
Resolving untrusted-root.badssl.com (untrusted-root.badssl.com)... 104.154.89.105
Connecting to untrusted-root.badssl.com (untrusted-root.badssl.com)|104.154.89.105|:443... connected.
WARNING: cannot verify untrusted-root.badssl.com's certificate, issued by ‘CN=BadSSL Untrusted Root Certificate Authority,O=BadSSL,L=San Francisco,ST=California,C=US’:
Self-signed certificate encountered.
HTTP request sent, awaiting response... 200 OK
Length: 600 [text/html]
Saving to: ‘index.html.6’
index.html.6 100%[===========================================================================================>] 600 --.-KB/s in 0s
2020-02-23 11:33:45 (122 MB/s) - ‘index.html.6’ saved [600/600]
[email protected]:~#
是不是很方便?
现在,我们来了解如何在终端上查看指定网站的 HTTP 响应头。
使用 -S
选项可以打印响应头,如下面的 Coursera 网站所示:
[email protected]:~# wget https://www.coursera.org -S
--2020-02-23 11:47:01-- https://www.coursera.org/
Resolving www.coursera.org (www.coursera.org)... 13.224.241.48, 13.224.241.124, 13.224.241.82, ...
Connecting to www.coursera.org (www.coursera.org)|13.224.241.48|:443... connected.
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 511551
Connection: keep-alive
Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0
Date: Sun, 23 Feb 2020 11:47:01 GMT
etag: W/"7156d-WcZHnHFl4b4aDOL4ZSrXP0iBX3o"
Server: envoy
Set-Cookie: CSRF3-Token=1583322421.s1b4QL6OXSUGHnRI; Max-Age=864000; Expires=Wed, 04 Mar 2020 11:47:02 GMT; Path=/; Domain=.coursera.org
Set-Cookie: __204u=9205355775-1582458421174; Max-Age=31536000; Expires=Mon, 22 Feb 2021 11:47:02 GMT; Path=/; Domain=.coursera.org
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
X-Content-Type-Options: nosniff
x-coursera-render-mode: html
x-coursera-render-version: v2
X-Coursera-Request-Id: NCnPPlYyEeqfcxIHPk5Gqw
X-Coursera-Trace-Id-Hex: a5ef7028d77ae8f8
x-envoy-upstream-service-time: 1090
X-Frame-Options: SAMEORIGIN
x-powered-by: Express
X-XSS-Protection: 1; mode=block
X-Cache: Miss from cloudfront
Via: 1.1 884d101a3faeefd4fb32a5d2a8a076b7.cloudfront.net (CloudFront)
X-Amz-Cf-Pop: LHR62-C3
X-Amz-Cf-Id: vqvX6ZUQgtZAde62t7qjafIAqHXQ8BLAv8UhkPHwyTMpvH617yeIbQ==
Length: 511551 (500K) [text/html]
操纵用户代理
有时您可能需要使用自定义的用户代理来连接网站,或者是特定的浏览器用户代理。 这可以通过指定 --user-agent
选项来实现。 以下示例使用了 MyCustomUserAgent
用户代理:
[email protected]:~# wget https://gf.dev --user-agent="MyCustomUserAgent"
当应用程序仍在开发中时,您可能没有可用于测试的正确 URL。 或者,您可能需要使用 IP 地址测试单个 HTTP 实例,但需要提供主机头才能使应用程序正常工作。 在这种情况下,--header
选项非常有用。
我们以测试 http://10.10.10.1
为例,主机头为 application.com
:
wget --header="Host: application.com" http://10.10.10.1
不仅限于主机头,您还可以注入任何您需要的头部。
使用代理连接
如果您在 DMZ 环境中工作,可能无法直接访问 Internet 站点,但您可以通过代理服务器连接。
wget -e use_proxy=yes http_proxy=$PROXYHOST:PORT http://externalsite.com
请确保将 $PROXYHOST:PORT
变量替换为实际的代理服务器地址和端口。
使用特定的 TLS 协议进行连接
通常,我建议使用 OpenSSL 来测试 TLS 协议。但是,您也可以使用 wget
。
wget --secure-protocol=TLSv1_2 https://example.com
上述命令将强制 wget
通过 TLS 1.2 连接。
总结
掌握必要的命令能够有效提升工作效率。我希望以上内容能让您更好地了解如何使用 wget
。