10个wget命令示例帮助您进行故障排除- wget测试

系统管理员经常使用的实用工具之一是wget。在与网络相关的链接中,它非常方便。

wget命令是什么?

wget命令是一种流行的Unix/Linux命令行实用程序,用于从网络获取内容。它是免费使用的,并提供一种非交互方式从网络下载文件的方法。wget命令默认支持HTTPS、HTTP和FTP协议。此外,您还可以使用HTTP代理。

wget如何帮助您进行故障排除?

有很多方法。

作为一个sysadmin,大部分时间您都会在终端上工作,当您需要排除与Web应用程序相关的问题时,您可能不想检查整个页面,而只是检查连接性。或者,您想验证内部网站。或者,您想下载某个页面以验证内容。

wget是非交互式的,这意味着即使在注销登录时,您也可以在后台运行它。有很多情况下,您在进行从网络检索文件时需要与系统断开连接,这对您来说是非常重要的。在后台中,wget将运行并完成其分配的任务。

它还可以用于在本地机器上获取整个网站。它可以遵循XHTML和HTML页面中的链接以创建本地版本。为此,它必须递归下载页面。这非常有用,因为您可以使用它下载离线查看的重要页面或站点。

让我们看看它们的实际效果。wget的语法如下所示。

wget [选项] [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 URL3

您只需确保在URL之间添加空格。

限制下载速度

当您想要检查文件在不同带宽下下载所需的时间时,这将非常有用。

使用--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
正在解析主机 nodejs.org (nodejs.org)… 104.20.23.46, 104.20.22.46, 2606:4700:10::6814:162e, …
正在连接 nodejs.org (nodejs.org)|104.20.23.46|:443… 已连接。
已发送 HTTP 请求,正在等待回应… 200 OK
长度:14591852 (14M) [application/x-xz] 正在保存至: “node-v12.16.1-linux-x64.tar.xz”

node-v12.16.1-linux-x64.tar.xz 100%[===========================================================================================>] 13.92M –.-KB/s 用时 0.05s

2020-02-23 10:59:58 (272 MB/s) – 已保存 “node-v12.16.1-linux-x64.tar.xz” [14591852/14591852]

It took 0.05 seconds to download 13.92 MB files. Now, let’s try to limit the speed to 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
正在解析主机 nodejs.org (nodejs.org)… 104.20.23.46, 104.20.22.46, 2606:4700:10::6814:162e, …
正在连接 nodejs.org (nodejs.org)|104.20.23.46|:443… 已连接。
已发送 HTTP 请求,正在等待回应… 200 OK
长度:14591852 (14M) [application/x-xz] 正在保存至: “node-v12.16.1-linux-x64.tar.xz.1”

node-v12.16.1-linux-x64.tar.xz.1 100%[===========================================================================================>] 13.92M 501KB/s 用时 28s

2020-02-23 11:00:46 (500 KB/s) – 已保存 “node-v12.16.1-linux-x64.tar.xz.1” [14591852/14591852]

Reducing the bandwidth took longer to download – 28 seconds. Imagine, your users are complaining about slow download, and you know their network bandwidth is low. You can quickly try –limit-rate to simulate the issue.
Download in the background
Downloading large files can take the time or the above example where you want to set the rate limit as well. This is expected, but what if you don’t want to stare at your terminal?
Well, you can use -b argument to start the wget in the background.
root@trends:~# wget -b https://slack.com
Continuing in background, pid 25430.
Output will be written to ‘wget-log.1’.
root@trends:~#
Ignore Certificate Error
This is handy when you need to check intranet web applications that don’t have the proper certificate. By default, wget will throw an error when a certificate is not valid.
root@trends:~# wget https://expired.badssl.com/
–2020-02-23 11:24:59– https://expired.badssl.com/
正在解析主机 expired.badssl.com (expired.badssl.com)… 104.154.89.105
正在连接 expired.badssl.com (expired.badssl.com)|104.154.89.105|:443… 已连接。
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'.
The above example is for the URL where cert is expired. As you can see it has suggested using –no-check-certificate which will ignore any cert validation.
根据给定的站点,在终端打印出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参数将非常有用。

我们以主机头为application.com的http://10.10.10.1测试为例。

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的想法。

类似文章