Dec 122013
 

I have been using this script for a long time (maybe 13 years) with only very slight changes.  It was probably one of the first cool ideas I had for a way to track laptops issued to employees that might possibly be stolen.  Granted, today, we use full disk encryption and other cool things that almost makes this script obsolete….but in the event something does get stolen, we can always track it.

The script only requires a crontab entry and a way to send mail (I use ssmtp btw).

You often hear about people who have cell phones stolen, laptops, etc and I know there are services that does this….but this is pretty minimal.

Without further wait….here is the script:

#!/bin/bash
EMAILS=yourname@yourdomain.com
tracker=/usr/local/bin/whereibeen.txt
CURDATE=`date`
CURIP=$(curl -s http://www.edwiget.name/ip.php)
if [ -f $tracker ]; then
		echo $CURDATE >> $tracker
		echo $(ifconfig eth0 | awk -F: '/inet addr:/ {print $2}' | awk '{ print $1 }') >> $tracker
		echo $(ifconfig wlan0 | awk -F: '/inet addr:/ {print $2}' | awk '{ print $1 }') >> $tracker
		echo $CURIP >> $tracker
		tar czfvp /usr/local/bin/whereibeen.txt.tar.gz /usr/local/bin/whereibeen.txt > /dev/null 2>&1
		mutt -s "tracker attached" -a /usr/local/bin/whereibeen.txt.tar.gz -- ${EMAILS} < /dev/null
	else
		touch $tracker
                echo $CURDATE >> $tracker
                echo $(ifconfig eth0 | awk -F: '/inet addr:/ {print $2}' | awk '{ print $1 }') >> $tracker
                echo $(ifconfig wlan0 | awk -F: '/inet addr:/ {print $2}' | awk '{ print $1 }') >> $tracker
                echo $CURIP >> $tracker
		tar czfvp /usr/local/bin/whereibeen.txt.tar.gz /usr/local/bin/whereibeen.txt > /dev/null 2>&1
                mutt -s "tracker attached" -a /usr/local/bin/whereibeen.txt.tar.gz -- ${EMAILS} < /dev/null
fi
 
# send the mail
sendmail -oi ${EMAILS} << EOF
From: [email protected]
To: [email protected]
Subject: COMPUTERNAME Booted: Booted on $CURDATE $CURIP
 
Your COMPUTERNAME has just been booted on $CURDATE with IP $CURIP
 
EOF

So basically what I always did was placed an entry in /etc/rc.local like:

/usr/local/bin/mailme.sh > /dev/null 2>&1

But sometimes, especially with wireless, you may not always have a connection established when the system boots up.  But the tracker file will at least have previous ip addresses.  So what I did was made a secondary entry in crontab which then sends the email every 6 hours.  You can always create a rule to file 13 the emails, until you really need them:

* */6 * * * /usr/local/bin/mailme.sh > /dev/null 2>&1