Friday, June 13, 2008

Quick Bash Script for Checking HTTP Headers

Whenever it comes to efficiently configure Web Servers and setting headers like ETAg, Cache-Control, or Expires and you have more than one server, you need to check them all since any user may hit different servers.

This script is meant to call wget on every IP returned by dig and display http response headers.
Usage is simple:


./script.sh http://www.example.com/ressource/with-long-cache-and-no-etag/big-image.jpg


Here's the code:

#!/bin/bash

BIN=${0##*/}
URL=${1?usage: $BIN url}
HOST=${URL#http://*}
REQ=${HOST#*/}
HOST=${HOST%%/*}

function check
{
wget -S --header="Host: $HOST" "http://$line/$REQ" -O/dev/null
}

dig +short $HOST | \
while read line
do
case $line in
[a-z]*)
;;
[0-9]*.[0-9]*.[0-9]*)
echo === Testing $HOST with IP $line
check $line
;;
*)
;;
esac
done

3 comments:

Stefan Naewe said...

Ever heard of HEAD ?
It's part of libwww-perl on Debian.

S.

rolphin said...

Hi, thanks for the tip.

I don't know if HEAD can handle a specific Host header. And I choose to not use perl.
And HEAD request sometimes are not handled like GET . Simulating a web browser with HEAD request is not accurate.

Jadu Kumar Saikia said...

Nice post.

//Jadu
http://unstableme.blogspot.com/

Sticky