Thursday, May 28, 2015

Small bash script - Chennai Power Outage check

This is a small script which will check if there is power outage in your are and drop you an email a day before.


http://pastebin.com/6xZaB8nb

#!/bin/bash

if [ -f /var/scripts/powercut.txt ]
  then
    rm /var/scripts/powercut.txt
fi

if [ -f /var/scripts/Tomorrow.txt ]
  then
    rm /var/scripts/Tomorrow.txt
fi

wget http://livechennai.com/powercut_schedule.asp -O /var/scripts/powercut.txt

NextURL=`grep "Power shutdown areas" /var/scripts/powercut.txt  | head | grep --color "href" | head -n1 | grep -P -o href.+ | cut -d\" -f2 | sed 's/^/http:\/\/livechennai.com\//'`


EmailSubject=`grep "Power shutdown areas" /var/scripts/powercut.txt  | head | grep --color "href" | head -n1 | grep -P -o title.+ | cut -d\" -f2`


echo "Power shutdown areas in Chennai on 30-05-15" | sed -e 's/^/NO /' -e 's/areas in Chennai/in your area/'

echo $NextURL

echo $EmailSubject

wget $NextURL -O /var/scripts/Tomorrow.txt

powercutyes=`grep -i "Your Street name" /var/scripts/Tomorrow.txt | wc -l`

echo

echo "From: \"SenderFirstname Lastname\" " > mail.txt
echo "To: \"RecepientFirstname Lastname\" " >> mail.txt

if [ $powercutyes -gt 0 ]
  then
    echo "We have power cut tomorrow"
    EmailSubject=`echo $EmailSubject | sed -e 's/^/YES /' -e 's/areas in Chennai/in your area/'`
    echo "Subject: $EmailSubject" >> mail.txt
    grep -i "Your Street name" Tomorrow.txt >> mail.txt
    curl --url "smtps://smtp.gmail.com:465" --ssl-reqd --mail-from "sender@gmail.com" --mail-rcpt "recepient@gmail.com" --upload-file /var/scripts/mail.txt --user "sender@gmail.com:senderGmailPassword"
  else
    EmailSubject=`echo $EmailSubject | sed -e 's/^/NO /' -e 's/areas in Chennai/in your area/'`
    echo "Subject: $EmailSubject" >> mail.txt
    echo "We don't have power cut tomorrow"
    curl --url "smtps://smtp.gmail.com:465" --ssl-reqd --mail-from "sender@gmail.com" --mail-rcpt "recepient@gmail.com" --upload-file /var/scripts/mail.txt --user "sender@gmail.com:senderGmailPassword"
fi

No comments:

Post a Comment