使用wget命令行排除故障

使用wget命令行排除故障

sysadmin常用的工具之一是wget。与Web相关的故障排除过程中非常方便。什么是wget命令?

技术开发 编程 技术框架 技术发展

 

使用wget命令行排除故障

sysadmin常用的工具之一是wget。与Web相关的故障排除过程中非常方便。什么是wget命令?

sysadmin常用的工具之一是wget。与Web相关的故障排除过程中非常方便。什么是wget命令?

wget命令是流行的Unix / Linux命令行实用程序,用于从Web上获取内容。它是免费使用的,并提供了一种非交互方式从Web下载文件。wget命令开箱即用地支持HTTPS,HTTP和FTP协议。此外,您还可以将HTTP代理与其一起使用。

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

有很多方法。

作为系统管理员,大多数时候,您将在终端上工作,并且在对与Web应用程序相关的问题进行故障排除时,您可能不想检查整个页面,而只是检查连接性。或者,您想验证Intranet网站。或者,您想下载某个页面以验证内容。

wget是非交互式的,这意味着即使您注销也可以在后台运行它。在许多情况下,即使从Web上进行文件检索,对于断开系统连接还是至关重要的。在后台,wget它将运行并完成其分配的作业。

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

让我们看看它们的作用。wget的语法如下。

wget [option] [URL]

1下载网页

让我们尝试下载一个页面。例如: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:~#复制

2下载多个文件

当您必须一次下载多个文件时非常方便。这可以为您提供有关通过某些脚本自动下载文件的想法。

让我们尝试下载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之间留出空格即可。

3限制下载速度

当您要检查文件以不同的带宽下载需要花费多少时间时,这将很有用。

使用该--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模拟问题。

4在后台下载

下载大文件可能要花一些时间,也可能需要上面的示例来设置速率限制。这是预期的,但是如果您不想凝视终端怎么办?

好了,您可以使用-b参数在后台启动wget。

root@trends:~# wget -b https://slack.com

Continuing in background, pid 25430.

Output will be written to ‘wget-log.1’.

root@trends:~#复制

5忽略证书错误

当您需要检查没有适当证书的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'.复制

上面的示例适用于cert过期的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:~#复制

不错,不是吗?

6 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]复制

7操纵用户代理

在某些情况下,您想使用自定义用户代理连接站点。或特定浏览器的用户代理。通过指定可以做到这一点--user-agent。下面的示例适用于作为MyCustomUserAgent的用户代理。

root@trends:~# wget https://gf.dev --user-agent="MyCustomUserAgent"复制

8主机头

当应用程序仍在开发中时,您可能没有正确的URL来对其进行测试。或者,您可能想使用IP测试单个HTTP实例,但是您需要提供主机标头才能使应用程序正常工作。在这种情况下,--header将很有用。

让我们举一个使用主机头作为application.com测试http://10.10.10.1的示例

wget --header="Host: application.com" http://10.10.10.1复制

不仅是主机,还可以注入任何您喜欢的标头。

9使用代理连接

如果您在DMZ环境中工作,则可能无法访问Internet站点。但是您可以利用代理进行连接。

wget -e use_proxy=yes http_proxy=$PROXYHOST:PORT http://externalsite.com复制

不要忘记用实际值更新$ PROXYHOST:PORT变量。

10使用特定的TLS协议进行连接

通常,我建议使用OpenSSL测试TLS协议。但是,您也可以使用wget。

wget --secure-protocol=TLSv1_2 https://example.com

以上将强制wget通过TLS 1.2连接。

结论

知道必要的命令可以帮助您工作。希望以上内容使您对可以做什么有所了解wget。

技术开发 编程 技术框架 技术发展