A "call home" script for any linux box, and especially useful for a rasberry Pi

I really wanted a call home script! I searched and searched I tell you, but none could come to suffice. I couldn't believe there was not another user who hadn't had this problem. Sure, I could just install a "remote access trojan" in it, but... it just wouldn't be the same. (And where would the fun be in that)

Every time I wanted to test something on my rasberryPi I had to take out my HDMI cable, find the spare keyboard, and start fixing it whilst stealing the TV for output. So to come to the end of my rant, in specifics I use this function for my rasberryPi, so that when it powers on it creates an open port for me to remote access it.

The Setup

You are about to setup an automatic remote connection to your box from the outside. So you will need to setup your SSH keys.

ssh-keygen [enter][enter][enter]  
ssh-copy-id pikachu@tailwhip.slowb.ro  

As I'm making this for my rasberryPi I want to add it to after networking is up, specifically I wanted to check that it has networking capabilities first, because otherwise your just shit out-a-luck

Init script:

We use an init script to start the callhome script.

#! /bin/sh
# Provides:          callhome
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.

# Author: slowpoke <slowpoke@slowb.ro>
# PATH should only include /usr/* if it runs after the mountnfs.sh script

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

# Function that starts the daemon/service
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null
        || return 1
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --
        || return 2
    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
    [ "$RETVAL" = 2 ] && return 2
    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
    [ "$?" = 2 ] && return 2
    # Many daemons don't delete their pidfiles when they exit.
    rm -f $PIDFILE
    return "$RETVAL"

do_reload() {  
    start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
    return 0

case "$1" in  
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
    case "$?" in
        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
    case "$?" in
        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
    #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
    echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
    exit 3


Actual ssh command script:

sleep 30;  
/usr/bin/autossh pikachu@tailwhip.slowb.ro -R3500:localhost:22 -p12345 &

Command Explained

tl;dr people look here:

autossh pikachu@tailwhip.slowb.ro -R3500:localhost:22 -p12345  

Basics: Open up the remote port 3500, on the tailwhip box, to point to port 22 on my rasberryPi. Then we just open up an ssh session to port 3500, from the tailwhip box.

ssh localhost -p3500  
root@localhost's password:

Linux pwnpi 3.6.11+ #371 PREEMPT Thu Feb 7 16:31:35 GMT 2013 armv6l  
"Hi Commander, Welcome back!"
{17:54} pwnpi:~ root

Yes its a bit hacky, using an init script to call another script, I did try adding the ssh command to the init script, but evidently I failed.

And congratulations every time you startup your rasberryPi you now know its automatically going to be accessible. (I suggest you dd your SDcard image now so you have a backup :D)

Tim Coombs

Administrator of Slowb.ro and world leader of my own mind, the only place our ideas and thoughts are our own in a world gone mad

In a terminal https://slowb.ro

Subscribe to Slowb.ro's Blog

Get the latest posts delivered right to your inbox.

or subscribe via RSS