wgetコマンドでレスポンスヘッダを表示する方法

目次

スポンサードリンク

wgetコマンドの基本構文

wgetコマンドはインターネットから、htmlファイルや画像などのコンテンツをダウンロードする際に使用するコマンドです。

基本構文は以下です。コマンドの後にオプション、ダウンロード対象のURLと続けます。

wget [オプション] [URL]

レスポンスヘッダを表示するオプション –server-response

–server-responseオプションを指定すると標準エラー出力にレスポンスヘッダが表示されます。

[root@hostname tmp]# wget --server-response https://uchy.me/blog/ 
--2024-05-08 18:53:25--  https://uchy.me/blog/
Resolving uchy.me (uchy.me)... 49.212.235.185
Connecting to uchy.me (uchy.me)|49.212.235.185|:443... connected.
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 08 May 2024 09:53:25 GMT
Content-Type: text/html
Content-Length: 20738
Connection: keep-alive
Last-Modified: Wed, 08 May 2024 09:23:37 GMT
ETag: "5102-617eddb779840"
Accept-Ranges: bytes
Length: 20738 (20K) [text/html]
Saving to: ‘index.html.35’

100%[===========================================================================>] 20,738      --.-K/s   in 0.008s

2024-05-08 18:53:25 (2.60 MB/s) - ‘index.html.35’ saved [20738/20738]

より詳細な結果を確認したい場合は、–debugオプションを指定する方法もあります。レスポンスヘッダだけでなく、リクエストヘッダやSSL証明書の情報など詳細な情報を表示することができます。どこからどこまでがレスポンスヘッダかも区切りがあり見やすくなっています。

[root@hostname tmp]# wget --debug https://uchy.me/blog/ 
Setting --check-certificate (checkcertificate) to 0
DEBUG output created by Wget 1.14 on linux-gnu.

URI encoding = ‘UTF-8’
Converted file name 'index.html' (UTF-8) -> 'index.html' (UTF-8)
Converted file name 'index.html' (UTF-8) -> 'index.html' (UTF-8)
--2024-05-08 18:55:24--  https://uchy.me/blog/
Resolving uchy.me (uchy.me)... 49.212.235.185
Caching uchy.me => 49.212.235.185
Connecting to uchy.me (uchy.me)|49.212.235.185|:443... connected.
Created socket 3.
Releasing 0x00000000019b1820 (new refcount 1).
Initiating SSL handshake.
Handshake successful; connected socket 3 to SSL handle 0x00000000019ba280
certificate:
subject: /CN=uchy.me
issuer:  /C=US/O=Let's Encrypt/CN=R3

---request begin---
GET /blog/ HTTP/1.1
User-Agent: Wget/1.14 (linux-gnu)
Accept: */*
Host: uchy.me
Connection: Keep-Alive

---request end---
HTTP request sent, awaiting response...
---response begin---
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 08 May 2024 09:55:24 GMT
Content-Type: text/html
Content-Length: 20738
Connection: keep-alive
Last-Modified: Wed, 08 May 2024 09:23:37 GMT
ETag: "5102-617eddb779840"
Accept-Ranges: bytes

---response end---
200 OK
Registered socket 3 for persistent reuse.
Length: 20738 (20K) [text/html]
Saving to: ‘index.html.37’

100%[===========================================================================>] 20,738      --.-K/s   in 0.008s

2024-05-08 18:55:24 (2.62 MB/s) - ‘index.html.37’ saved [20738/20738]



Linuxコマンドについて学べるおすすめ書籍
Linuxコマンドの知識は、プログラマにとって長く役立つ知識です。 私はこちらの書籍で一通り知識を抑えました。基本から丁寧に解説されています。

Linux教科書 図解でパッとわかる LPIC/LinuC
はじめてLPICを受ける方向け、手を動かしながらLinuxについて学びたい方におすすめ。30日間の無料体験もできる『Kindle Unlimited』でも読むことができます。

Linuxコマンドについて徹底的に学べるUdemy講座
もう絶対に忘れない Linux コマンド【Linux 100本ノック+名前の由来+丁寧な解説で、長期記憶に焼き付けろ!】 icon