A script, cron, and ssmtp – Making life simpler

Being pained by having to check the grades site every now and then, and having learnt how easy scripts can make your life, this was one attempt to make use of this. And moreover, if you are one of those who are online most of the time, you can set this up so that you could receive a mail whenever the grades have been updated. (Ofcourse, you can use this to do whatever you want! May be some important updates, or may be logs of who logged into your account, if you have sneaky class mates like mine. 😉 )

Setting up the requirements first :

[I used gmail to send emails from the terminal. I’ll tell you how I did this.]

PART – I

Step 1 : Install ssmtp.

If you’re using Ubuntu , you know what to do. Just copy paste this on your terminal :

sudo apt-get install ssmtp

Step 2 : Edit the ssmtp config file. You’ll find the config file in /etc/ssmtp :

sudo gedit /etc/ssmtp/ssmtp.conf

Step 3 : Update the config file.
You’ll find a template ssmtp file. Change the stuff below.

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=YOUR_EMAIL_ID@gmail.com

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.gmail.com:465

# Where will the mail seem to come from?
rewriteDomain=gmail.com

# The full hostname
hostname=YOUR_HOST_NAME
AuthUser=YOUR_GMAIL_USER_NAME  #Without the @gmail.com
AuthPass=YOUR_GMAIL_PASSWORD
# Are users allowed to set their own From: address?
# YES – Allow the user to specify their own From: address
# NO – Use the system generated From: address
FromLineOverride=YES
UseTLS=YES

Step 4 : If you have sendmail already installed then stop the service and remove it.

Step 5 : Try if this actually works now.
In a general case you would do something like :
echo "email content" | mail -s "email subject" email_address_to_send_email_to@somedomain.com

If you have a long message and prefer to write it down in a file first :
ssmtp email_address_to_send_email_to@somedomain.com < message.txt

So now just to check if it’s working :
echo "Hello World!" | mail -s "Test Mail" TO_EMAIL_ADDRESS@SOME_DOMAIN.com

You should have got the mail to the TO address you specified.

P.S : You might not want to use your own email id if you are doing this on a public computer as the password is stored in the config file in clear text. So you might be better off creating a separate account for this purpose.

PART II

Write the script or copy paste the script written below here that would send you a mail when grades are updated.


wget --post-data='rollno=ROLLNO&pwd=PASSWORD' --save-cookies cookies.txt --keep-session-cookies http://www.iitm.ac.in/viewgrades/studentauth/login.php
wget --load-cookies cookies.txt http://www.iitm.ac.in/viewgrades/studentauth/studopts2.php
rm cookies.txt
rm login.php
changes=`diff studopts2.php studopts2.php.1`
rm studopts2.php
mv studopts2.php.1 studopts2.php
if [ ! "$changes" = "" ]
then
notify-send "Grades are out."
echo $changes | mail -s "Grades Updated" EMAIL-ID@DOMAIN.com
fi

Copy the above lines into some file. Let’s say gradecheck.sh and edit it with your own username and password.

Now, make sure you have notify-send installed on your computer.

On Ubuntu that would be :

sudo apt-get install notify-send

And you’re done! Also, make an empty file named studopts2.php.  And we’re set!
Run the script and check if its working. You should get a pop up saying “Grades are out” on your screen. And also, a mail to the address you specified, if Part-I was set up correctly! You could comment out the line that would send you a mail in case you do not want to get mail updates.

Part – III

Set up a cron job!

The simplest part is this. All you need to do is the following :

sudo crontab -e -u USERNAME

And you would do something similar to the following line in the file :

*/10 * * * * export DISPLAY=:0 && cd ~/Desktop/Acads/Grades/ && ./gradecheck.sh

This runs the script every 10 minutes on your computer.
If you want to explore more about crontab. Check this out or check the man page for crontab. And we’re set!

P.S : You might want to add MAILTO=”” in the crontab file, else you would be inundated with emails regarding the mail alert regarding the cron job.

P.P.S : This would work fine if you are connected to the internet always. The cronjob would run even when you are not connected to the internet, and hence you would have to manually disable the cron job otherwise you would get an update that grades are updated, next time you are connected to the internet. I’m looking for a work around for this. But, since I am always connected to the internet when I’m on my laptop, I haven’t bothered to figure it out as yet.

Credits for the grades script :  Chinmay. [Didn’t want to be dragged to NVK court, if you know what I mean. ;)]

Advertisements

3 thoughts on “A script, cron, and ssmtp – Making life simpler

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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