Monday, 17 September 2012

Dead Man's Switch on Linux, Part 1: Basic bash

I've always liked the idea of a dead man's switch. It's a partial fix for the 'what happens if I get hit by a bus' problem: how would you give your important account details to your family? How can you get a last minute message to your loved ones? Most importantly, who will delete your porn?

Traditionally, they work with the assistance of a third party. They've been used to launch nukes. Wikileaks used it to cause shenanigans. And you can sign up for some web-based solutions that will send pre-recorded emails for you when you show signs of being dead.

Of course, you have no control over that. the whole site might go down without you realising, rendering your efforts useless. So here's the same basic principle, implemented on your own software in bash script.

If you just want the script, skip to the end. It does things like this:

The Simplest Script Of All

We need three basic components to make this work:
  1. The main script file. When run, it checks to see if the timer has expired. If it has, it performs pre-set actions (like sending email)
  2. A method to 'reset' the timer. A one-line script file will work fine here.
  3. A way to trigger the first script to run. We'll use cron.
We also need a way to check whether we've hit the time limit. To keep things simple, I'm going to use the 'modified' time on the main script file. When it runs, it'll compare when it was last modified against the current time to act as the timer. To reset, you just need to run 'touch' to change the modified time. Easy, right? 

So here's the main script, set to do the most important task of all: Delete Your Porn.

#! /bin/bash
# Simple Dead Man's Script

timelimit=30 #Number of days to wait without update

let timelimit=$timelimit*60*60*24 #turn that into seconds
lastaccessed=$(stat -c %Y $BASH_SOURCE)
timenow=$(date +%s)
let timeleft=($timenow-$lastaccessed)
if [ $timeleft -gt $timelimit ]
    rm -rf $HOME/myporndirectory

Here's the 'reset' script, saved somewhere on your desktop:

#! /bin/bash

touch pathto/

And here's the line you have to add to cron (crontab -e to get there):
0 0 * * * pathto/

Simple, right? make the scripts executable, and if you ever go 30 days without running the reset script it'll delete all your porn. Careful with your holidays.
It can do anything that can be done from the command line. with minimal trouble it can ftp files around, modify/upload entire websites, start torrents, or just update twitter with what you had for lunch the other day. I want to use it to send an email.

The Slightly Less Simple Script

Obviously I took a simple concept and made it both unnecessarily complex and annoyingly basic. It now has a setup option that should work most of the time, and a 'test' option that emails yourself and makes sure it works.

Boring Notes:
  • The setup option assumes a generic install - that you have crontab access, that you have a home directory, etc. It will fail horribly if run as root.
  • The setup script creates a .dmswitch/ folder in your home dir. Inside will be the check script, the message file, and an attachment directory. The message file is plain text and as long as you need.
  • It also adds a file to your desktop. each time you run this, it resets the counter.
  • The setup is optional. If you manually move the files where you like and change the variables, it'll work just fine.
  • I'm using sendemail to do the email sending since it plays nicely with gmail. You can use any other method you like. If you use the script unmodified you'll need to install sendemail.
  • Any files in the attachment directory will be attached to the email. No spaces in filenames though, because I am lazy.

Quick Install:
On debian/ubuntu, it takes four lines and some variable editing.
  • sudo apt-get install sendemail libio-socket-ssl-perl
  • gedit (or nano, vim, emacs, etc.)
  • paste in the script, and edit the email account details to your own. Save.
  • chmod +x
  • ./ setup
Test it by running .dmswitch/ test . If the email settings are correct, you should have a test email arrive shortly.

  • rm -rf .dmswitch/
  • crontab -e, remove the entry pointing to the script.
Finally, The Script itself.
# Basic Dead Man's Switch v1.0
# Options:
# 1) dmswitch setup
#    sets up the script in your home dir with some default settings.
#    best to set it up manually or automatically rather than a mix of the two.
# 2) dmswitch test
#    sends a test email to the itself.
# 3) dmswitch reset
#    'checks in' with the dead man switch and resets the counter to zero
#    Just does the same thing as touching the scriptfile.
# 4) dmswitch
#    default. if the time has expired, IT WILL SEND AN EMAIL.

#some running variables are based on the setup vars
croncommand="0 0 * * * "$setupdir"/"$checkscript #cron line for how often it checks expiry. Default is daily.

#make sure you change the email settings. 
dmdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
timelimit=30 #Number of days to wait without update
emailto="" #The target address
emailfrom="" #The account you send with
emailusername="" #The username for your email account
emailpass="myemailpassword" #password
emailsubject="Automated email from Dead Man's Switch."
message=$dmdir"/"$setupmessage # the text file you want your message stored in
attachmentdir=$dmdir"/"$setupattachmentdir # put any attachments you want to include here

#checks if the time has run out. Does the maths in unix time.
function checkifexpired() {
    let timelimit=$timelimit*60*60*24 
    lastaccessed=$(stat -c %Y $BASH_SOURCE)
    timenow=$(date +%s)
    let timeleft=($timenow-$lastaccessed)
    if [ $timeleft -gt $timelimit ]
    then expired=1

#sends an email. replace sendemail with whatever program you prefer.
function sendemail() {
    attachmentlist=$(ls $attachmentdir)
    cd $attachmentdir
    sendEmail -f $emailfrom -t $emailto -u $emailsubject -s $emailserver":"$emailport -xu $emailusername -xp $emailpass -a $attachmentlist -o message-file=$message

#sets up a directory to run from and creates the necessary files.
function setupdm() {
    mkdir -p $setupdir
    cp $BASH_SOURCE $setupdir"/"$checkscript 
    cd $setupdir
    chmod +x $checkscript
    mkdir $setupattachmentdir
    touch $setupmessage
    echo "If you can read this, I'm dead or arrested or something" >>$setupmessage

    #append cron job to existing cron file
    (crontab -l; echo "$croncommand" ) | crontab
    #setup reset script on desktop
 touch $resetscript
    echo "#! /bin/bash" >>$resetscript
    echo "checkfile="$setupdir"/"$checkscript >>$resetscript
    echo "touch $""checkfile" >>$resetscript
    chmod +x $resetscript
    echo "setup complete"

#main script starts here


if [ "$1" == "setup" ]
elif [ "$1" == "test" ]
    #send test email to yourself
    emailsubject="TEST: "$emailsubject
elif [ "$1" == "reset" ]
elif [ $expired -eq 1 ]
    #send the email, and disable the script from running
    chmod -x $BASH_SOURCE
else exit

Go wild.
Part two will cover the fact that not everybody leaves their desktop on for months at a time and suggest several overly-elaborate ways to trigger the reset while running it on a remote machine.

Sunday, 2 September 2012

iPad hanger v0.1


  • One wire coat hanger. (30c)
  • Your bare hands. (free?)
  • One tablet computer. ($150-$1000)

About 3 minutes, but it really depends on how quickly you can bend stuff.


  1. Take the coathanger and bend it into a vague rectangle. 
  2. Make a few extra bends in it to stop the iPad falling out the bottom
  3. Make a slight concave bend towards the top. This is just to make the iPad lean backwards so it's more structurally stable if you knock it.
  4. Just look at these pictures.
I know my lighting and camera work sucks. Pro cameras are wasted on coat hangers.

If  you use fancy tools like 'pliers' you can probably make it way more attractive to look at.

You now have a flexible, mouldable tablet stand that works surprisingly well and can hang off damn near anything in your vicinity. If you're in a room doing something, chances are you can hang your iPad somewhere.

Watch TV while cooking!

Use the Verge Collection Map while driving!
Also a great way to keep up with facebook on the drive to work. 

And of course, look up recipes in the shower.
For demonstration purposes only. You should use a ziploc bag when actually showering with an iPad.

Entirely seriously though; I have half a dozen home made and bought tablet stands at home and at work. The coathanger now gets more use than all of them, cost a grand total of 30c, and can be quickly remodelled on the fly to fit whatever you need it to do.

This is also just the prototype. With a pair of pliers and a ruler you can hide that metal bar at the base, even up the bends on either side, and do pretty much whatever else you can imagine. It's weirdly stable and I haven't managed to knock it loose once (despite my clumsy showering technique).

Make one.