安装 Wget
CentOS/RHEL/Fedora
[jack@onitroad ~]# yum install wget -y
Debian/Ubuntu
jack@onitroad:~# apt-get install wget -y
如何使用 wget - 命令示例
- 下载文件
让我们从从 Internet 下载简单文件的基本示例开始。我们只需在 wget 命令之后指定我们要下载的文件,如下所示。
- 下载文件
[jack@onitroad Downloads]# wget https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1511.iso --2014-08-31 11:30:54-- https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1511.iso Resolving mirror.aarnet.edu.au (mirror.aarnet.edu.au)... 202.158.214.106, 2001:388:30bc:cafe::beef Connecting to mirror.aarnet.edu.au (mirror.aarnet.edu.au)|202.158.214.106|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 394264576 (376M) [application/octet-stream] Saving to: ‘CentOS-7-x86_64-NetInstall-1511.iso’ 68% [=========================> ] 271,936,588 2.32MB/s eta 54s
输出为我们提供了完成百分比、下载速度和剩余时间估计。
这已将文件副本下载到当前工作目录,其文件名与 URL 中的文件名相同。
[jack@onitroad Downloads]# ls CentOS-7-x86_64-NetInstall-1511.iso
- 2.使用新名称下载到特定目录
我们可以使用 -O 选项来指定输出文件,允许我们告诉 wget 将文件下载到哪里,以及文件的名称。这使我们不必将其下载到当前工作目录,移动它,然后根据第一个示例可能需要重命名它。
[jack@onitroad Downloads]# wget -O /root/file.txt https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/md5sum.txt --2014-08-31 11:36:25-- https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/md5sum.txt Resolving mirror.aarnet.edu.au (mirror.aarnet.edu.au)... 202.158.214.106, 2001:388:30bc:cafe::beef Connecting to mirror.aarnet.edu.au (mirror.aarnet.edu.au)|202.158.214.106|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 406 [text/plain] Saving to: ‘/root/file.txt’ 100%[==============================================================================>] 406 --.-K/s in 0s 2014-08-31 11:36:25 (139 MB/s) - ‘/root/file.txt’ saved [406/406]
这里我们下载了远程文件 md5sum.txt 并保存到 /root/file.txt。
- 限制下载速度
我们可以选择使用 --limit-rate 选项以特定速度限制它,而不是让 wget 以默认的最大速度下载。如果我们不希望 wget 使用所有可用的网络带宽,这可能很有用。
速度限制可以定义为字节数、k 后缀的千字节或者 m 后缀的兆字节。
在下面的示例中,我们开始以 500KB/s 的限制下载 .ISO 文件,这在 wget 输出中得到了确认。
- 限制下载速度
[jack@onitroad Downloads]# wget --limit-rate=500k https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1511.iso --2014-08-31 11:37:47-- https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1511.iso Resolving mirror.aarnet.edu.au (mirror.aarnet.edu.au)... 202.158.214.106, 2001:388:30bc:cafe::beef Connecting to mirror.aarnet.edu.au (mirror.aarnet.edu.au)|202.158.214.106|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 394264576 (376M) [application/octet-stream] Saving to: ‘CentOS-7-x86_64-NetInstall-1511.iso.1’ 2% [==> ] 11,726,532 500KB/s eta 12m 31s
- 不要下载多个版本
默认情况下,如果我们第二次将相同的文件下载到相同的目录,则会为其命名并在末尾添加 .1. 下一个实例将是 .2,然后是 .3,依此类推。
这在我们之前的示例中已经看到,当我们尝试再次下载相同的 .iso 文件时,我们可以看到它以 .1 扩展名保存。
- 不要下载多个版本
Saving to: ‘CentOS-7-x86_64-NetInstall-1511.iso.1’
如果这种行为是不受欢迎的,我们可以改为使用“no clobber”的 -nc 选项来阻止这种情况,并防止下载文件的较新副本(如果它们已经存在)。
[jack@onitroad Downloads]# wget -nc https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1511.iso File ‘CentOS-7-x86_64-NetInstall-1511.iso’ already there; not retrieving.
- 不要检查 SSL/TLS 证书
默认情况下,当通过 HTTPS 下载时,wget 会根据可用的 CA 来检查证书,但是如果证书不好或者可能是自签名的,传输将失败。
- 不要检查 SSL/TLS 证书
[jack@onitroad Downloads]# wget https://localhost/file.txt --2014-08-31 11:44:14-- https://localhost/file.txt Resolving localhost (localhost)... ::1, 127.0.0.1 Connecting to localhost (localhost)|::1|:443... connected. ERROR: cannot verify localhost's certificate, issued by ‘/C=--/ST=SomeState/ L=SomeCity/O=SomeOrganization/OU=SomeOrganizationalUnit/ CN=centos7.example.com/jack@onitroad’: Unable to locally verify the issuer's authority. ERROR: certificate common name ‘centos7.example.com’ doesn't match requested host name ‘localhost’. To connect to localhost insecurely, use `--no-check-certificate'.
我们可能想忽略此检查并下载文件。这是通过 --no-check-certificate 选项完成的,它强制使用不安全的操作模式,允许我们继续。通常这在传输机密数据时不是一个好的选择,但是它在通常使用自签名证书的测试环境中非常有用。
[jack@onitroad Downloads]# wget https://localhost/file.txt --no-check-certificate --2014-08-31 11:44:32-- https://localhost/file.txt Resolving localhost (localhost)... ::1, 127.0.0.1 Connecting to localhost (localhost)|::1|:443... connected. WARNING: cannot verify localhost's certificate, issued by ‘/C=--/ST=SomeState/ L=SomeCity/O=SomeOrganization/OU=SomeOrganizationalUnit/ CN=centos7.example.com/jack@onitroad’: Unable to locally verify the issuer's authority. WARNING: certificate common name ‘centos7.example.com’ doesn't match requested host name ‘localhost’. HTTP request sent, awaiting response... 200 OK Length: 406 [text/plain] Saving to: ‘file.txt’ 100%[============================================================>] 406 --.-K/s in 0s 2014-08-31 11:44:32 (52.1 MB/s) - ‘file.txt’ saved [406/406]
- 启用时间戳
默认情况下,本地下载文件的时间戳将与远程服务器上的文件相同。我们可以改为使用 --no-use-server-timestamps ,它不会将本地文件时间戳设置为服务器上文件提供的时间戳,而是设置为下载文件的时间,这可能更有用,因为它允许我们看看文件是什么时候下载的。
如图所示,我们有第一个示例中的原始 .iso 文件,日期为“2014 年 12 月 10 日”,但是当我们下载第二个副本时,指定 --no-use-server-timestamps 完成后,其时间戳被列为今天,“ 2014 年 8 月 31 日'。
- 启用时间戳
[jack@onitroad Downloads]# ls -la total 477668 -rw-r--r--. 1 root root 394264576 Dec 10 2014 CentOS-7-x86_64-NetInstall-1511.iso [jack@onitroad Downloads]# wget --no-use-server-timestamps https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1511.iso --2014-08-31 11:50:10-- https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1511.iso Resolving mirror.aarnet.edu.au (mirror.aarnet.edu.au)... 202.158.214.106, 2001:388:30bc:cafe::beef Connecting to mirror.aarnet.edu.au (mirror.aarnet.edu.au)|202.158.214.106|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 394264576 (376M) [application/octet-stream] Saving to: ‘CentOS-7-x86_64-NetInstall-1511.iso.1’ 100%[================================================================>] 394,264,576 1.21MB/s in 3m 44s 2014-08-31 11:53:55 (1.68 MB/s) - ‘CentOS-7-x86_64-NetInstall-1511.iso.1’ saved [394264576/394264576] [jack@onitroad Downloads]# ls -la total 770060 -rw-r--r--. 1 root root 394264576 Dec 10 2014 CentOS-7-x86_64-NetInstall-1511.iso -rw-r--r--. 1 root root 394264576 Aug 31 11:53 CentOS-7-x86_64-NetInstall-1511.iso.1
- 7.更改进度条
默认情况下,wget 会以“=====>”的形式显示进度条以及完成百分比。我们可以使用 --progress 选项将其更改为“点”,如下所示。
[jack@onitroad Downloads]# wget --progress=dot https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1511.iso --2014-08-31 11:47:07-- https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1511.iso Resolving mirror.aarnet.edu.au (mirror.aarnet.edu.au)... 202.158.214.106, 2001:388:30bc:cafe::beef Connecting to mirror.aarnet.edu.au (mirror.aarnet.edu.au)|202.158.214.106|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 394264576 (376M) [application/octet-stream] Saving to: ‘CentOS-7-x86_64-NetInstall-1511.iso.2’ 0K .......... .......... .......... .......... .......... 0% 374K 17m8s 50K .......... .......... .......... .......... .......... 0% 1.33M 10m55s 100K .......... .......... .......... .......... .......... 0% 744K 10m9s 150K .......... .......... .......... .......... .......... 0% 740K 9m47s 200K .......... .......... .......... .......... .......... 0% 1.37M 8m44s 250K .......... .......... .......... .......... .......... 0% 753K 8m42s 300K .......... .......... .......... .......... .......... 0% 1.39M 8m6s 350K .......... .......... .......... .......... .......... 0% 752K 8m9s 400K .......... .......... .......... .......... .......... 0% 1.38M 7m45s 450K .......... .......... .......... .......... .......... 0% 760K 7m49s 500K .......... .......... .......... .......... .......... 0% 735K 7m54s ...
这只是一个简单的替代方法,我们可以用来在下载文件时以不同的方式显示输出。
- 继续部分下载的文件
如果我们有一个部分下载的文件没有完全完成,我们可以使用 -c 选项继续从 wget 或者其他程序的先前实例下载文件。
本质上,如果我们使用 -c 运行 wget 并且我们在本地系统上已经有一个同名的文件,那么 wget 将假定我们已经拥有的文件部分是正确的,并在请求其余数据时将其用作偏移量.这样服务器就只需要传输剩余的部分。
在下面的示例中,我们开始对 .iso 文件执行普通的 wget,然后按“ctrl+c”取消传输,留下部分完整的文件。在这里,我们再次使用 -c 运行 wget 以继续传输,我们可以在输出中看到我们已经拥有的文件部分由“+”字符表示。
- 继续部分下载的文件
[jack@onitroad Downloads]# wget -c https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1511.iso --2014-08-31 11:33:26-- https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1511.iso Resolving mirror.aarnet.edu.au (mirror.aarnet.edu.au)... 202.158.214.106, 2001:388:30bc:cafe::beef Connecting to mirror.aarnet.edu.au (mirror.aarnet.edu.au)|202.158.214.106|:443... connected. HTTP request sent, awaiting response... 206 Partial Content Length: 394264576 (376M), 74101684 (71M) remaining [application/octet-stream] Saving to: ‘CentOS-7-x86_64-NetInstall-1511.iso’ 94% [+++++++++++++++++++++++++++++++====> ] 372,277,944 2.12MB/s eta 8s
这应该只需要恢复在 wget 的先前实例中启动的下载我们仍然部分完成的本地文件。如果没有 -c,下载将简单地以新的 .1 文件重新开始,保留现有的不完整文件。
- 9.重试下载失败
我们可以使用 -t 选项指定文件传输应重试的次数,然后是失败时尝试的重试次数。默认值为 20 次,除非遇到诸如连接被拒绝或者 404 未找到等致命错误。
在下面的例子中,网络连接被故意中断,我们可以看到重试是自动发生的,wget 会继续尝试。
[jack@onitroad Downloads]# wget -t 10 https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1511.iso --2014-08-31 11:37:47-- https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1511.iso Resolving mirror.aarnet.edu.au (mirror.aarnet.edu.au)... 202.158.214.106, 2001:388:30bc:cafe::beef Connecting to mirror.aarnet.edu.au (mirror.aarnet.edu.au)|202.158.214.106|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 394264576 (376M) [application/octet-stream] Saving to: ‘CentOS-7-x86_64-NetInstall-1511.iso.1’ 22% [===============================> ] 86,849,288 504KB/s in 2m 50s 2014-08-31 11:40:37 (500 KB/s) - Connection closed at byte 86849288. Retrying. --2014-08-31 11:40:38-- (try: 2) https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1511.iso Connecting to mirror.aarnet.edu.au (mirror.aarnet.edu.au)|202.158.214.106|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 394264576 (376M) [application/octet-stream] Saving to: ‘CentOS-7-x86_64-NetInstall-1511.iso.1’ 0% [ ] 0 --.-K/s in 0.1s
与继续使用 -c 不同,这仅适用于在当前运行的 wget 实例中重试,而不是从前一个实例恢复部分文件。
- 从 URL 文件列表下载
我们不必在每个单独的 wget 命令中指定 URL,而是可以使用 -i 后跟一个包含多个 URL 的文件(每行一个 URL),wget 将遍历并下载它们。
- 从 URL 文件列表下载
[jack@onitroad Downloads]# cat to-download.txt https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/0_README.txt https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/md5sum.txt [jack@onitroad Downloads]# wget -i to-download.txt --2014-08-31 12:03:15-- https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/0_README.txt Resolving mirror.aarnet.edu.au (mirror.aarnet.edu.au)... 202.158.214.106, 2001:388:30bc:cafe::beef Connecting to mirror.aarnet.edu.au (mirror.aarnet.edu.au)|202.158.214.106|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 2498 (2.4K) [text/plain] Saving to: ‘0_README.txt’ 100%[======================================================>] 2,498 --.-K/s in 0.004s 2014-08-31 12:03:15 (611 KB/s) - ‘0_README.txt’ saved [2498/2498] --2014-08-31 12:03:15-- https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/md5sum.txt Reusing existing connection to mirror.aarnet.edu.au:443. HTTP request sent, awaiting response... 200 OK Length: 406 [text/plain] Saving to: ‘md5sum.txt’ 100%[======================================================>] 406 --.-K/s in 0s 2014-08-31 12:03:16 (96.0 MB/s) - ‘md5sum.txt’ saved [406/406] FINISHED --2014-08-31 12:03:16- Total wall clock time: 0.6s Downloaded: 2 files, 3.4K in 0.02s (207 KB/s)
- 等待时间
默认情况下,下一次重试会立即发生,但是我们可以通过指定 -w 等待选项来延迟它,在下一次尝试之前需要等待几秒钟。
如果远程服务器很慢或者没有响应,这会很有用,因为我们可能不想通过尝试使其过载。
[jack@onitroad Downloads]# time wget -w 10 -i to-download.txt --2014-08-31 12:06:30-- https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/0_README.txt Resolving mirror.aarnet.edu.au (mirror.aarnet.edu.au)... 202.158.214.106, 2001:388:30bc:cafe::beef Connecting to mirror.aarnet.edu.au (mirror.aarnet.edu.au)|202.158.214.106|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 2498 (2.4K) [text/plain] Saving to: ‘0_README.txt’ 100%[===============================================================>] 2,498 --.-K/s in 0s 2014-08-31 12:06:30 (675 MB/s) - ‘0_README.txt’ saved [2498/2498] --2014-08-31 12:06:40-- https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/md5sum.txt Reusing existing connection to mirror.aarnet.edu.au:443. HTTP request sent, awaiting response... 200 OK Length: 406 [text/plain] Saving to: ‘md5sum.txt’ 100%[===============================================================>] 406 --.-K/s in 0s 2014-08-31 12:06:40 (143 MB/s) - ‘md5sum.txt’ saved [406/406] FINISHED --2014-08-31 12:06:40- Total wall clock time: 10s Downloaded: 2 files, 2.8K in 0s (443 MB/s) real 0m10.337s user 0m0.020s sys 0m0.006s
在这个例子中,我们下载两个文件,它们之间有 10 秒的等待时间。
我们可以看到,当我们在这里处理非常小的文件时,总命令执行时间按预期花费了 10 多秒。
输出还确认第一个文件是在 12:06:30 下载的,而第二个文件在 12:06:40 开始下载,也就是我们的 -w 选项中指定的 10 秒后。
我们可以指定仅在下载失败的重试之间等待,而不是在每个文件下载上等待时间。
这是通过 --waitretry 后跟要等待的秒数来完成的。
这有一个默认值 10 秒,将在第一次失败后等待 1 秒开始,然后是 2 秒,不断增加到指定的数字。
我们还可以使用 --random-wait 后跟几秒来指定随机等待时间。
这有助于防止 Web 服务器检测到明显的持续文件检索,因为现在访问将是随机的。
- 非交互式下载
到目前为止,我们一直在前台使用 wget 进行下载。 Wget 是一个非交互式下载器,它允许我们在没有用户会话的情况下在后台运行它。这是通过后台传输的 -b 选项完成的,并在更新发生时创建一个 wget-log 文件。
- 非交互式下载
[jack@onitroad Downloads]# wget -b https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1511.iso Continuing in background, pid 2789. Output will be written to ‘wget-log’. [jack@onitroad Downloads]# tailf wget-log 14950K .......... .......... .......... .......... .......... 3% 1.52M 2m12s 15000K .......... .......... .......... .......... .......... 3% 12.5M 2m12s 15050K .......... .......... .......... .......... .......... 3% 1.47M 2m12s 15100K .......... .......... .......... .......... .......... 3% 1.54M 2m12s 15150K .......... .......... .......... .......... .......... 3% 11.5M 2m12s
如图所示,我们的 wget 传输会立即发送到后台,返回给我们的 bash shell。然后我们可以查看 wget-log 文件,该文件记录有关传输状态的信息。
- 发送消息到日志文件
我们可以使用 -o 将所有消息记录到指定的文件中,而不是通过 stdout/stderr 将消息输出到屏幕。
- 发送消息到日志文件
[jack@onitroad Downloads]# wget -o /root/wget-log.txt https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/md5sum.txt [jack@onitroad Downloads]# cat /root/wget-log.txt --2014-08-31 12:18:26-- https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/md5sum.txt Resolving mirror.aarnet.edu.au (mirror.aarnet.edu.au)... 202.158.214.106, 2001:388:30bc:cafe::beef Connecting to mirror.aarnet.edu.au (mirror.aarnet.edu.au)|202.158.214.106|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 406 [text/plain] Saving to: ‘md5sum.txt.1’ 0K 100% 4.60M=0s 2014-08-31 12:18:27 (4.60 MB/s) - ‘md5sum.txt.1’ saved [406/406]
重要的是要注意 -o 将覆盖现有文件,我们可以改用 -a ,它的工作方式相同,但如果文件已经存在,则会添加到文件的末尾。
- 隐藏输出
相反,如果我们不想要输出,我们可以在安静模式下使用 -q 选项,这将隐藏所有输出。
- 隐藏输出
[jack@onitroad Downloads]# wget -q https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/md5sum.txt [jack@onitroad Downloads]#
- 15.调试信息
使用调试选项 -d 可以获得更多信息,这在排除任何问题时可能很有用。
[jack@onitroad Downloads]# wget -d https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/md5sum.txt DEBUG output created by Wget 1.14 on linux-gnu. URI encoding = ‘UTF-8’ --2014-08-31 13:31:24-- https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/md5sum.txt Resolving mirror.aarnet.edu.au (mirror.aarnet.edu.au)... 202.158.214.106, 2001:388:30bc:cafe::beef Caching mirror.aarnet.edu.au => 202.158.214.106 2001:388:30bc:cafe::beef Connecting to mirror.aarnet.edu.au (mirror.aarnet.edu.au)|202.158.214.106|:443... connected. Created socket 3. Releasing 0x0000000002465f50 (new refcount 1). Initiating SSL handshake. Handshake successful; connected socket 3 to SSL handle 0x000000000247adb0 certificate: subject: /1.3.6.1.4.1.311.60.2.1.3=AU/businessCategory=Non-Commercial Entity/ serialNumber=54 084 540 518/C=AU/ST=New South Wales/L=North Ryde/O=AARNET Pty Ltd/OU=Operations/CN=mirror.aarnet.edu.au issuer: /C=BM/O=QuoVadis Limited/CN=QuoVadis EV SSL ICA G1 X509 certificate successfully verified and matches host mirror.aarnet.edu.au ---request begin-- GET /pub/centos/7/isos/x86_64/md5sum.txt HTTP/1.1 User-Agent: Wget/1.14 (linux-gnu) Accept: */* Host: mirror.aarnet.edu.au Connection: Keep-Alive ---request end-- HTTP request sent, awaiting response... ---response begin-- HTTP/1.1 200 OK Date: Wed, 31 Aug 2014 03:30:16 GMT Server: ATS/5.2.0 Last-Modified: Thu, 10 Dec 2014 15:37:35 GMT Accept-Ranges: bytes Content-Length: 406 Content-Type: text/plain; charset=UTF-8 Age: 96 ---response end-- 200 OK Registered socket 3 for persistent reuse. URI content encoding = ‘UTF-8’ Length: 406 [text/plain] Saving to: ‘md5sum.txt.4’ 100%[=====================================================>] 406 --.-K/s in 0s 2014-08-31 13:31:24 (126 MB/s) - ‘md5sum.txt.4’ saved [406/406]
在这里我们可以看到关于证书和HTTP请求的各种信息。
默认情况下,所有 wget 请求都使用 -v 选项运行以获取详细输出,但是我们可以运行 -nv 以取消详细输出以减少输出并仅接收基本要素。
[jack@onitroad Downloads]# wget -nv https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/md5sum.txt 2014-08-31 13:33:43 URL:https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/md5sum.txt [406/406] -> "md5sum.txt.7" [1]
- 查看服务器响应
同样,我们也可以只打印服务器发送的 HTTP 标头或者 FTP 服务器发送的响应,并使用 -S 选项作为服务器响应,这在故障排除时可能很有用,因为它允许我们查看远程服务器在响应我们的响应时所说的内容。要求。此信息也出现在上面显示的更详细的调试日志中。
- 查看服务器响应
[jack@onitroad Downloads]# wget -S https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/md5sum.txt --2014-08-31 13:37:00-- https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/md5sum.txt Resolving mirror.aarnet.edu.au (mirror.aarnet.edu.au)... 202.158.214.106, 2001:388:30bc:cafe::beef Connecting to mirror.aarnet.edu.au (mirror.aarnet.edu.au)|202.158.214.106|:443... connected. HTTP request sent, awaiting response... HTTP/1.1 200 OK Date: Wed, 31 Aug 2014 03:37:29 GMT Server: ATS/5.2.0 Last-Modified: Thu, 10 Dec 2014 15:37:35 GMT Accept-Ranges: bytes Content-Length: 406 Content-Type: text/plain; charset=UTF-8 Age: 0 Length: 406 [text/plain] Saving to: ‘md5sum.txt.8’ 100%[===================================================================================================================================================>] 406 --.-K/s in 0s 2014-08-31 13:37:00 (83.8 MB/s) - ‘md5sum.txt.8’ saved [406/406]
- 超时
如果下载时间太长,我们可以在 -T 选项指定的总时间段后自动中止和取消它,后跟我们想要超时的秒数。
- 超时
[jack@onitroad Downloads]# wget -T 30 https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/md5sum.txt
默认的 900 秒空闲超时(在下载期间没有接收到数据)通常很好,但是可以根据要求进行调整。
- 使用凭证
如果 HTTP 或者 FTP 连接需要用户身份验证,我们可以使用 --user 选项指定用户名,并使用 --password 选项指定密码。一般来说,使用 --password 选项在安全性方面不是一个好主意,因为我们指定的密码将存储在 bash 历史记录中。我们可以改为使用 --ask-password ,它会提示输入密码,将其保留在我们的历史日志之外。
- 使用凭证
[jack@onitroad Downloads]# wget --user=username --ask-password http://localhost/file.txt Password for user ‘username’:
- 代理认证
虽然上面的示例用于远程主机上的凭据,但如果我们的网络中有一个代理服务器,我们需要在允许通过代理进行代理之前对其进行身份验证,我们也可以指定代理服务器和可选的代理凭据。
这是通过首先设置 http_proxy、https_proxy 或者 ftp_proxy 环境变量来完成的,以便 wget 知道可以找到代理服务器的位置。
- 代理认证
[jack@onitroad Downloads]# http_proxy=http://proxy.example.com:3128 [jack@onitroad Downloads]# https_proxy=http://proxy.example.com:3128 [jack@onitroad Downloads]# echo $http_proxy http://proxy.example.com:3128 [jack@onitroad Downloads]# echo $https_proxy http://proxy.example.com:3128
现在 wget 将知道指定的代理。
如果代理需要用户身份验证,我们可以指定 --proxy-user 和 --proxy-password 选项进行身份验证并继续。
- 20.下载非缓存文件
在 HTTP 请求中,我们可以指定 --no-cache 将“Pragma: no-cache”和“Cache-Control: no-cache, must-revalidate”选项发送到 Web 服务器,请求非缓存版本,这可能如果我们想下载文件的最新版本并跳过缓存,这将很有用。默认情况下允许接受缓存文件。
通过使用-d,我们可以看到我们的HTTP请求只有在添加--no-cache之后才包含'Pragma: no-cache'和'Cache-Control: no-cache, must-revalidate'。
[jack@onitroad Downloads]# wget -d https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/md5sum.txt ... ---request begin-- GET /pub/centos/7/isos/x86_64/md5sum.txt HTTP/1.1 User-Agent: Wget/1.14 (linux-gnu) Accept: */* Host: mirror.aarnet.edu.au Connection: Keep-Alive ---request end-- [jack@onitroad Downloads]# wget -d --no-cache https://mirror.aarnet.edu.au/pub/centos/7/isos/x86_64/md5sum.txt ... ---request begin-- GET /pub/centos/7/isos/x86_64/md5sum.txt HTTP/1.1 Cache-Control: no-cache, must-revalidate Pragma: no-cache User-Agent: Wget/1.14 (linux-gnu) Accept: */* Host: mirror.aarnet.edu.au Connection: Keep-Alive ---request end--
Wget 是一个非交互式网络下载器,可用于在 Unix/Linux 中下载文件。
它支持 HTTP、HTTPS 和 FTP 协议,并且还具有代理支持。
wget 命令非常强大并且有很多可用选项,在本教程中,我们将介绍 20 个最重要的 wget 示例,学习如何最好地使用它。