Home > Utility > Locate Your Mac Laptop If It Is Missing

Locate Your Mac Laptop If It Is Missing

There are commercial services to help you locate lost laptops, but I wanted a feature that required less software to be installed and fewer Big Brothers helping me out along the way. Using a few free tools and shell scripts, I cobbled together my own LoJack-type system that notifies me whenever my laptop awakes (I can’t help you if your computer is turned off).

Tools Used:

  • SleepWatcher: Executes commands of your choice when computer wakes, sleeps, etc
  • bash: Shell scripting
  • LocateMe: Free command line tool (from me) using Apple’s geolocating API
  • Notifo: Lightweight notification for iPhones, etc
  • Google Maps: Displaying your laptop location

Downloads:

Concept of Operations

Scenario: Laptop is forgotten or stolen. Third party opens laptop to snoop or legitimately try to identify owner. Third party sees password prompt (you have that turned on, right?) and wonders. If laptop is still connected to a known wireless network, or if Third Party has plugged in a network cable, laptop determines its latitude and longitude and sends notification. If you don’t have a password prompt, then suspect helpfully connects your computer to a network in order to use it, and you get notified. (You might be imagining a possible problem here; discussed below)

Step 1: SleepWatcher

First we need the ability to run a script when the laptop wakes up, and the SleepWatcher tool is a great way to run commands when certain events happen. According to the man page, the following events can be used as triggers. When the computer…

  • is put to sleep (must run for less than 15 seconds)
  • wakes from sleep
  • display is dimmed
  • display is undimmed without having gone to sleep
  • display sleeps
  • display wakes up
  • is idle
  • is plugged in to a power supply (laptop)
  • is unplugged from a power supply (laptop).

I used the example files included in the distribution and made a file at /Library/LaunchAgents/de.bernhard-baehr.sleepwatcher-20compatibility.plist

?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>de.bernhard-baehr.sleepwatcher</string>
	<key>ProgramArguments</key>
	<array>
		<string>/usr/local/sbin/sleepwatcher</string>
		<string>-V</string>
		<string>-s /etc/sleepwatcher/rc.sleep</string>
		<string>-w /etc/sleepwatcher/rc.wakeup</string>
		<string>-D /etc/sleepwatcher/rc.displaydim</string>
		<string>-E /etc/sleepwatcher/rc.displayundim</string>
		<string>-S /etc/sleepwatcher/rc.displaysleep</string>
		<string>-W /etc/sleepwatcher/rc.displaywakeup</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
	<key>KeepAlive</key>
	<true/>
</dict>
</plist>

I used the sample rc.* files provided and added a line to rc.wakeup:

/usr/local/bin/log-location-to-notifo.sh

Step 2: Shell Script

The log-location-to-notifo.sh file looks like this (sorry about the long lines):

#!/bin/sh
# Author: Robert Harder, rob _ iharder.net
# Web: http://blog.iharder.net
# Date: November 2010
# License: Public Domain

# Example:
# <+37.19511210, -120.37831060> +/- 150.00m (speed 0.00 mps / course -1.00) @ 2010-10-22 11:10:09 -0700
PATH=/usr/local/bin:$PATH
LOCATION=$(LocateMe)   

# Parse Lat and Lon
LAT=$(echo $LOCATION | sed 's/<\([0-9+-\.]*\), \([0-9+-\.]*\)> \(+\/- [0-9a-zA-Z\.]*\) (.*/\1/' )
LON=$(echo $LOCATION | sed 's/<\([0-9+-\.]*\), \([0-9+-\.]*\)> \(+\/- [0-9a-zA-Z\.]*\) (.*/\2/' )
ERR=$(echo $LOCATION | sed 's/<\([0-9+-\.]*\), \([0-9+-\.]*\)> \(+\/- [0-9a-zA-Z\.]*\) (.*/\3/' )

# Log location right away 
logger -t "$(basename $0)" "$LOCATION"

# Network Up?
function netup(){
NETUP=$(osascript - << EOF
try
	do shell script "ping -c 1 api.notifo.com"
	do shell script "echo Y"
on error
	do shell script "echo N"
end try
EOF
)
echo $NETUP
}

# Wait for network to be up
while [ "$(netup)" == "N" ]; do
	logger -t $(basename $0) Waiting for network to come up to report location $LOCATION
	sleep 1
done

# Get my external IP address
MYIP="$(curl -s http://whatismyip.org/)"

# Build a URL that will display a Google Maps page with useful info
URL="http://maps.google.com/maps?q=${LAT},${LON}($(hostname),+${MYIP},+$(echo $ERR | urlencode),+$(date | urlencode))&ie=UTF8&ll=${LAT},${LON}&t=roadmap&z=14&iwloc=A&mrt=loc"

# Log locally as well as calling notifo (another script)
# and logging its response.
logger -t "$(basename $0)" "$URL"
logger -t "$(basename $0)" $(notifo.sh -l "$(hostname)" -t "Current Location" -u "$URL" $LOCATION)

Step 3: LocateMe

The log-location-to-notifo.sh script calls LocateMe to determine your computer’s location and report it using, in this case, Notifo.

The LocateMe tool itself returns data provide by Apple’s GeoLocation API:

$ LocateMe
<+37.12357770, -120.45605097> +/- 197.00m (speed 0.00 mps / course -1.00) @ 2011-01-19 09:36:20 -0800

The relevant part of the shell script is this:

LOCATION=$(LocateMe)   

# Parse Lat and Lon
LAT=$(echo $LOCATION | sed 's/<\([0-9+-\.]*\), \([0-9+-\.]*\)> \(+\/- [0-9a-zA-Z\.]*\) (.*/\1/' )
LON=$(echo $LOCATION | sed 's/<\([0-9+-\.]*\), \([0-9+-\.]*\)> \(+\/- [0-9a-zA-Z\.]*\) (.*/\2/' )
ERR=$(echo $LOCATION | sed 's/<\([0-9+-\.]*\), \([0-9+-\.]*\)> \(+\/- [0-9a-zA-Z\.]*\) (.*/\3/' )

Step 4: Notifo

Another handy script that’s used within log-location-to-notifo.sh is notifo.sh which posts notifications to your Notifo.com account.

You’ll need to sign up and insert your own account and key in the notify.sh script:

#!/bin/sh
# Author: Robert Harder
# Date: September 2010
# License: Public Domain
# For use with notifo.com

# Provide your username and API key here
# Example
NOTIFO_USER=johndoe
NOTIFO_API_KEY=baf06f567ee7e30bc5ab5760b48ab748e7e30bcd3


# Default values
LABEL=$(hostname)
TITLE=$(whoami)
MSG=
URI=

function USAGE(){

    echo "Usage: $0 [-l label] [-t title] [-u url] [message]"
    echo "  -l label        The application sending the message"
    echo "  -t title        The title of the message"
    echo "  -u url          The URL to take the user for more information"
    echo "  message         The message to display to the user or STDIN if not supplied"
    echo "  -h/-?           This help message"
    echo "Example: $0 -l Log -t "Access Granted" A user has logged into your system."
}

# Process command line arguments
while [ "$1" ]
do
   if [ "$1" = "-l" ]; then
        LABEL="$2"
        shift 2
   elif [ "$1" = "-t" ]; then
        TITLE="$2"
        shift 2
   elif [ "$1" = "-u" ]; then
        URI="$2"
        shift 2
   elif [ "$1" = "-h" ]; then
        USAGE
        exit 0
   elif [ "$1" = "-?" ]; then
        USAGE
        exit 0
   else
        MSG="$@"
        shift $#
   fi
done

# Change command line to curl depending on whether the
# message is coming from command line arguments or from STDIN.
if [ -n "$MSG" ]; then
    MSGCOMPONENT="msg=${MSG}"
else
    MSGCOMPONENT="msg@-"
fi

#echo \
curl -s -k \
    -u ${NOTIFO_USER}:${NOTIFO_API_KEY} \
    --data-urlencode "label=${LABEL}" \
    --data-urlencode "title=${TITLE}" \
    --data-urlencode "uri=${URI}" \
    --data-urlencode "${MSGCOMPONENT}" \
    https://api.notifo.com/v1/send_notification
echo

Step 5: Google Maps

Notifo lets us attach a URL to a notification, so we can create a Google Maps URL with the information we have discovered so far. The relevant portion of the log-location-to-notifo.sh script was this:

# Build a URL that will display a Google Maps page with useful info
URL="http://maps.google.com/maps?q=${LAT},${LON}($(hostname),+${MYIP},+$(echo $ERR | urlencode),+$(date | urlencode))&ie=UTF8&ll=${LAT},${LON}&t=roadmap&z=14&iwloc=A&mrt=loc"

Problems

Your wayward laptop can only notify you if it’s on a network, so if a thief does not connect to a network or wipes your computer clean before connecting, then you are out of luck. Also if your Mac cannot determine its position by its wifi signal, you also are out of luck. There’s not much you can do to get around that, but at least you are protecting yourself from some threats.

Categories: Utility Tags: , , ,
  1. Dave Johnson
    April 8th, 2011 at 07:55 | #1

    I built a homebrew version several years ago using IP-location, then augmented it when apple added the geolocation API in snow leopard. How you get around the “user wipes” issue, is just use filevault for your account, then have the system auto-log into an account on login.

    I made the auto-log account name “apple” with “Full Name” of the account “password = apple” so that it would appear like the account was setup for use by apple technicians during service (and in fact, it is).

    Unless I disable it (for instance, prior to sending into Apple for service), the system location info is emailed automatically every 20 minutes as long as the “apple” user is logged in. The thinking here is that anyone who finds a computer with Microsoft Office and Adobe Create Suite (I think the only 2 paid apps that I have other than shareware like SuperDuper!) will surely want to keep the system loaded rather than wipe it and loose those apps.

    I’ve luckily not had my system stolen so haven’t had to test any of the recovery capabilities but I am pretty confident that a bogus auto-login account is a much better choice, but then, I take the performance hit of filevault (although my new 500GB Momentus XT hybrid drive has sped that up considerable), and keep all my sensitive information like tax, medical, and banking info on a small TrueCrypt volume inside that. Typically a petty compuer thief is more interested in hardware than delving into Federal crimes 🙂

  2. Dave Johnson
    April 8th, 2011 at 08:08 | #2

    ps- forgot to mention, the IP-location is super simple:

    speedster@fiveangle[~]$ curl http://api.hostip.info/get_html.php?ip=`dig +short blog.iharder.net`
    Country: UNITED STATES (US)
    City: Scottsdale, AZ
    IP: 208.109.218.230
    speedster@fiveangle[~]$

  3. April 8th, 2011 at 09:26 | #3

    @Dave I like how you set up the honey pot to get around the obvious weakness of these tools, which is that someone has to use the computer and put it on a network for any of this to work!

    Does the apple user have admin privileges in your scenario? I guess you only need them to be comfortable enough that they log in to a network somewhere.

    I’ve gone back and forth on File Vault. I have a thorough and secure backup strategy, but File Vault makes me nervous as far as potential fragility goes. I take it you’ve had no problems?

  4. January 24th, 2012 at 07:49 | #4

    How would you turn on Location Services on via the command line

    Have you found a way to do this?

  5. Carlos
    March 13th, 2012 at 10:35 | #5

    Hey, i am surprised because i am trying to develop something like this. There are a couple of things you can do further. For example, it would be nice to be able to have an ssh connection and perform different actions like install keylogger and activity loggers, scp,…

    But if the thief connects to internet with a router, you will have to use something like netcat to reverse open ports.

    Also, the geolocation feature built in snow leopard sucks monkey’s ass unfortunately. It is way better google’s and more accurate (See, i am in Spain). Problem? you would need a browser (that supports geolocation) to get the coordinates for you. I am still working on how i could do this without being noticed.

    If i ever finish it, i will post it.

    Take care!

  6. January 21st, 2014 at 19:01 | #6

    Quality articles or reviews is the key to
    be a focus for the viewers to pay a visit the web site,
    that’s what this web page is providing.

  7. Dom
    March 6th, 2014 at 19:00 | #7

    LocateMe is for now the most accurate geo location i can not use it in a script because when you use the command LocateMe, it seems to wait for ages and need ctrl-c to recover
    Is there a fix ?

  8. October 11th, 2014 at 17:42 | #8

    Hi there, You hage done an excellent job. I’ll
    certainly digg it and personally suggest to my friends.
    I’m sure they will be benefited from this site.

  9. October 12th, 2014 at 10:27 | #9

    I used to be recommended this bkog bby my cousin. I’mnow nnot certain whether or not this submit is written through
    him as nobody else realze such precise about my problem. You’re amazing!
    Thank you!

  10. October 12th, 2014 at 18:07 | #10

    Yes! Finally somene writds about m88.

  11. October 12th, 2014 at 19:56 | #11

    My programmer is tying to convince me to move to .net from PHP.
    I have always disliked the idea befause of the expenses.
    But he’s tryiong none the less. I’ve been using
    Movable-type oon numerous websites for about a yer and am anxious about switching to
    another platform. I have heard very giod things about blogengine.net.
    Is there a way I can import aall my wordpress content into it?
    Any ind of help would be really appreciated!

  12. October 12th, 2014 at 20:20 | #12

    This site definitely has all the information I needed about this subject
    and didn’t know who to ask.

  13. October 12th, 2014 at 21:08 | #13

    It’s really a great and helpful piece of info. I’m satisfied that you simply
    shared this helpful information with us. Please stazy us up
    to date like this. Thanks for sharing.

  14. October 13th, 2014 at 16:09 | #14

    I every time used to study piece of writing in news pappers but
    now as I am a user of internet so from now I am using net for articles or reviews, thanks to web.

  15. October 13th, 2014 at 22:52 | #15

    What’s up to all, the contents existing at this web siite are actually amazing foor
    people experience, well, keep up the nice work fellows.

  16. October 16th, 2014 at 23:20 | #16

    I’ve severe zits on my chest & again. Cream.

  17. October 20th, 2014 at 13:21 | #17

    Tremendous issues here. I’m very happy to peer
    ypur article. Thank you so much and I’m looking forward
    to contact you. Will you please drop mee a e-mail?

  18. October 21st, 2014 at 06:33 | #18

    Thank you a bunch for sharing this with all peopple you really recognize what you’re speaking about!
    Bookmarked. Kindly also cnsult with my web site =).

    We can have a hyperlink change contract betwseen us

  1. January 21st, 2011 at 23:27 | #1
  2. March 17th, 2011 at 12:01 | #2