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.
Setup scripts and auto-ssh
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
### BEGIN INIT INFO
# 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.
### END INIT INFO
# Author: slowpoke <slowpoke@slowb.ro>
#
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="callhome.sh"
NAME=callhome.sh
DAEMON=/usr/bin/sshconnect.sh
DAEMON_ARGS=""
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# 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
#
do_start()
{
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --
$DAEMON_ARGS
|| return 2
}
do_stop()
{
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
RETVAL="$?"
[ "$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
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
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
;;
esac
:
Actual ssh command script:
#!/bin/bash
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)