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.]


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.

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com

# Where will the mail seem to come from?

# The full hostname
AuthUser=YOUR_GMAIL_USER_NAME  #Without the @gmail.com
# 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

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.


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" = "" ]
notify-send "Grades are out."
echo $changes | mail -s "Grades Updated" EMAIL-ID@DOMAIN.com

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. ;)]


Node for a novice

So, this is my first blog and in this blog I would attempt to put down some stuff that I have understood or learnt or am learning or seem to have understood and putting together different pieces I have learnt from various sources also.

Having an understanding of JavaScript to some extent would be good to get started. If not you could read up more about it here. You might also reconsider reading the Wikipedia article on JavaScript before you read further.

So let’s get started! Okay firstly a quick intro to what this Node JS is all about. And then I started to see how useful this could be. Node JS or simply Node is used to provide an awesome way to build network programs and those that could be scaled. Now you might ask why not PHP? And how is this Node going to be of any use?

This is one very satisfying explanation you’ll find –

 What’s the issue with current server programs?

Let’s do the math. In languages like Java™ and PHP, each connection spawns a new thread that potentially has an accompanying 2 MB of memory with it. On a system that has 8 GB of RAM, that puts the theoretical maximum number of concurrent connections at about 4,000 users. As your client-base grew, if you wanted your web application to support more users, you had to add more and more servers. Of course, this adds to a business’s server costs, traffic costs, labor costs, and more. Adding to those costs are the potential technical issues — a user can be using different servers for each request, so any shared resources have to be shared across all the servers. For all these reasons, the bottleneck in the entire web application architecture (including traffic throughput, processor speed, and memory speed) was the maximum number of concurrent connections a server could handle.

So what does Node do and how is it any different? Instead of spawning a new thread for each connection  each connection fires an event, as node is event-run. Node will never deadlock, and I/O calls are non blocking and asynchronous. Awesome ain’t it? For a fact, Facebook still uses PHP majorly and not Node!In case you’re wondering why, Node is a recent technology and changing the technology stack is not a risk that companies would probably be taking!

Okay, so if you’re still reading this. You might be interested in knowing more! Here’s the official site : http://nodejs.org/

There is documentation available on the site but you might not find the documentation quite useful if you didn’t know what to do with Node and what it’s meant for!

So let’s get to the point now! Simply put, Node is used as JavaScript on the server-side. Yes, I did not make a mistake, that’s right, on the server-side, outside your browser!!

Installation :

Okay cool! Let us check if you have node installed first. Type this on your command line :

node -v

You should now be seeing the version of node installed. Don’t have it installed? Follow the instructions  given here for the installation and we’re set!

Cross check if it works?

Open the Node Shell from the command line :

$ node 
> console.log('Hello World');  
  Hello World 

And now I suggest you go and read this. If you were a beginner like me, this is one perfect guide to make you an advanced novice in Node as the author says. 🙂 After this you should be able to do some cool stuff like writing an application that streams live twitter feed, maybe live feed about the location of visitors on your website if you own one!

And then once you are done, come back to visit my next blog which I hope to write soon where I’ll tell you about how I went on to use Socket.io (Web Sockets) and MongoDB(which is a non-relational database) with Node and what they are all about , why I had to use MongoDB and not a relational database like MySQL and how I used these to build an application.

P.S : Since it’s my first blog, and me being a beginner, criticism in any form(although constructive would be preferred ) and comments suggesting any improvements would be highly appreciated.

Update (4th June 2012) : To convince yourself as to why Node is cool and a comparison as to how bad writing networking code in other languages is comparatively. Check this out.