Skip to content

Instantly share code, notes, and snippets.

@takuya
Last active October 19, 2023 16:33
Show Gist options
  • Save takuya/a3e30ff04a23132420d03db1e71e6c02 to your computer and use it in GitHub Desktop.
Save takuya/a3e30ff04a23132420d03db1e71e6c02 to your computer and use it in GitHub Desktop.
check-web-site.sh
#!/usr/bin/env bash
function check_site(){
url=$1
regex=$2
if [ -z $url ] ; then
echo "{
\"result\": 1,
\"error\" : \"no uri.\"
}"
exit 1
fi
[ -z $regex ] && regex='<title>.+</title>'
tmp_body=$(mktemp -u)
tmp_header=$(mktemp -u)
curl -L -s -o $tmp_body $url -w '{
"http_code" : "%{http_code}",
"time_total" : "%{time_total}",
"time_namelookup" : "%{time_namelookup}",
"time_connect" : "%{time_connect}",
"time_redirect" : "%{time_redirect}",
"size_download" : "%{size_download}",
"size_request" : "%{size_request}",
"speed_download" : "%{speed_download}",
"content_type" : "%{content_type}",
"num_connects" : "%{num_connects}",
"num_redirects" : "%{num_redirects}",
"redirect_url" : "%{redirect_url}"
}' > $tmp_header;
headers=$(cat $tmp_header);
rm $tmp_header
if [ ! -e $tmp_body ] ; then
echo "{
\"result\": 7,
\"headers\": $headers,
\"error\" : \"tcp connection failed.\"
}"
exit 7
fi
body=$(cat $tmp_body | grep -P "$regex" | sed -e 's|"|\\"|g' -e 's/^/ "/g' -e 's/$/",/');
rm $tmp_body;
if [ -z $body ] ; then
echo "{
\"result\": 8,
\"headers\": $headers,
\"error\" : \"regex not matched.\"
}"
exit 8
else
echo "{
\"headers\": $headers,
\"result\": 0,
\"regex\": [
$body
\"\"
]
}"
exit 0
fi
}
check_site $1 $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment