系统管理员经常使用的一种实用程序是 wget。这在与 Web 相关的故障排除过程中非常有用。
wget 命令是什么?
wget 命令是一个流行的 Unix/Linux 命令行实用程序,用于从 Web 检索内容。它可以免费使用,并提供一种从网络下载文件的非交互式方式。 wget 命令开箱即用地支持 HTTPS、HTTP 和 FTP 协议。此外,您还可以使用 HTTP 代理。
wget 如何帮助排除故障?
有很多方法。
作为系统管理员,您大部分时间都会在终端中工作,因此在排除与 Web 应用程序相关的问题时,您可能只想检查连接而不是整个页面。或者假设您想检查您的 Intranet 网站。或者,如果您想下载特定页面并检查其内容。
wget 是非交互式的。这意味着即使您已注销,它也可以在后台运行。即使从网络检索文件,您也经常需要断开与系统的连接。 wget在后台运行并完成其指定的工作。
您还可以使用它将整个网站加载到您的本地计算机上。您可以点击XHTML 和 HTML页面中的链接来创建本地版本。为此,您需要递归下载页面。这非常有用,因为它允许您下载重要的页面和网站并离线查看它们。
让我们看看它的实际效果。 wget 的语法如下。
wget [option] [URL] 
下载网页
让我们下载该页面。示例:github.com
wget github.com如果没有连接问题,将下载主页,您将看到类似于以下内容的输出。
root@trends:~# 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] root@trends:~#

下载多个文件
当您需要一次下载多个文件时很有用。这将使您了解如何使用某些脚本自动下载文件。
让我们下载 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 URL3URL 之间必须有空格。

限制下载速度
如果您想查看在不同带宽下下载文件需要多长时间,这很有用。
您可以使用--limit-rate选项限制下载速度。
Nodejs文件下载的输出如下:
root@trends:~# 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.92 MB 的文件花了 0.05 秒。现在我们将速度限制为 500K。
root@trends:~# 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。
root@trends:~# wget -b https://slack.com
Continuing in background, pid 25430.
Output will be written to ‘wget-log.1’.
root@trends:~# 
忽略证书错误
如果您需要检查没有适当证书的 Intranet Web 应用程序,这非常有用。默认情况下,如果证书无效,wget 会抛出错误。
root@trends:~# 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忽略证书验证。
root@trends:~# 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]
root@trends:~#这很酷。
HTTP 响应头
检查设备上特定站点的 HTTP 响应标头。
使用-S打印标题,如下面的 Coursera 中所示。
root@trends:~# 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。
root@trends:~# 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可以做什么。




![2021 年如何设置 Raspberry Pi Web 服务器 [指南]](https://i0.wp.com/pcmanabu.com/wp-content/uploads/2019/10/web-server-02-309x198.png?w=1200&resize=1200,0&ssl=1)

