Created
August 26, 2018 23:28
-
-
Save caiosba/e885d2d416bbd054fba0aaff41ec19ef to your computer and use it in GitHub Desktop.
Script to monitor a list of links and e-mail the links that are offline... the links are read from a CSV file with the following columns in the following order: title, module, unit, URL (only URL is mandatory)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
offline=$(mktemp) | |
cat all.csv | while read line | |
do | |
title=$(echo "$line" | cut -d ',' -f 1) | |
module=$(echo "$line" | cut -d ',' -f 2) | |
unit=$(echo "$line" | cut -d ',' -f 3) | |
url=$(echo "$line" | cut -d ',' -f 4) | |
echo "Checking if $url is online..." | |
agent="User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36" | |
status=$(curl -XGET -LIk -m 10 -H "$agent" "$url" 2>/dev/null | grep HTTP | tail -1 | sed 's/^[^ ]\+ \([0-9]\+\).*/\1/g') | |
if [ -z $status ] | |
then | |
status="000" | |
fi | |
echo "Response code is: $status" | |
if [ $status -ne "200" ] | |
then | |
echo "Retrying after a few seconds..." | |
sleep 5 | |
status=$(curl -XGET -LIk -m 10 -H "$agent" "$url" 2>/dev/null | grep HTTP | tail -1 | sed 's/^[^ ]\+ \([0-9]\+\).*/\1/g') | |
if [ -z $status ] | |
then | |
status="000" | |
fi | |
echo "After trying again, response code is: $status" | |
if [ $status -ne "200" ] | |
then | |
echo "$url is not online!" | |
echo "URL: $url" >> $offline | |
echo "Title: $title" >> $offline | |
echo "Module: $module" >> $offline | |
echo "Unit: $unit" >> $offline | |
echo "" >> $offline | |
fi | |
fi | |
done | |
echo "Done... so, those URLs are offline:" | |
cat $offline | |
echo "Sending e-mail..." | |
cat $offline | mail -a 'FROM:[email protected]' -s 'Offline links' [email protected] | |
echo "Done!" | |
rm $offline |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment