Checking Remote File for ‘ 404 ‘ Pattern, Send Email If Found (Not Optimized yet)


for (( ; ; ))

result =””

echo “Resetting Variables”

sec_latest=”$(ls /home/max/Desktop/SSH\ Watchdog/ -Art | tail -n 1)”

echo “‘”$sec_latest”‘”” was last file modified”

lines_old=”$(grep -c “.*” /home/max/Desktop/SSH\ Watchdog/${sec_latest})”

echo $lines_old

# tocopy=”$(ssh root@ ‘ls /home/max/Desktop -Art | tail -n 1’)”

# Edit: The previous command doesn’t work with Absolute Path so use this one instead

tocopy=”$(ssh root@ ‘ls /home/max/Desktop/SSH\ Watchdog/’20130619’* -Art | tail -n 1 | sed ‘s/[^\/]*\///g”)”

echo $tocopy

scp root@${tocopy} /home/max/Desktop/SSH\ Watchdog/

echo “SCP Command Executed”

DATE=$(date +%Y%m%d%H%M%S)

cp /home/max/Desktop/SSH\ Watchdog/${tocopy} /home/max/Desktop/SSH\ Watchdog/$DATE.txt

echo “File Renamed”

lines_new=”$(grep -c “.*” /home/max/Desktop/SSH\ Watchdog/${tocopy})”

echo $lines_new

tocheck=”$(sed “$lines_old,$lines_new!d” /home/max/Desktop/SSH\ Watchdog/${tocopy})”

echo “Data to be checked retrieved”


echo “$tocheck” > “$datachunk”

echo “datachunk saved”

result=”$(grep -A 0 ” 404 ” ${datachunk})”

echo “$result” > “/home/max/Desktop/logs/${DATE}.chunk”

if [[ “$result” =~ ” 404 ” ]]


echo $result

echo “$result” | mutt -s “ERROR” —


echo “All Is Well”


sleep 1s



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.