Getting a headless Raspberry Pi to speak its IP address on boot

After initial set up and configuration, I usually use my Pis headless (just connecting power, and either ethernet or a wifi dongle), and SSH in to them from a laptop (or tablet, or phone). Sometimes it would be useful to know the IP address that the Pi has been assigned in order to know what to SSH to. There are various ways that this can be achieved, but the idea is to run a script on boot which somehow tells you the address. For example, you could use twidge to tweet the address. However, there are potentially security issues with this, so I prefer to get the Pi to just speak its address to me (I usually have some headphones handy to plug in while the Pi boots). So that’s what I’ll describe in this post, but it should be obvious how to modify to use alternative methods. I’m assuming a recent Raspbian image is being used, and that the Pi has been configured, and that the SSH server has been enabled.

First you need to make sure that “espeak” is installed.

$ sudo apt-get install espeak

Then (as root), create a file in /etc/init.d called “sayIPbs” containing the following contents (eg. do sudo vi /etc/init.d/sayIPbs, press “i”, then copy and paste in from clipboard, then Esc-Z-Z to save and quit)¬†


#! /bin/sh
# /etc/init.d/sayIPbs
## Some things that run always
# Carry out specific functions when asked to by the system
case "$1" in  start)
    echo "Starting script sayIPbs "
    sleep 5
    public=`curl ifconfig.me`
    private=`hostname -I`
    string="public address is $public and private address is $private"
    echo $string | espeak -s 120 -v en-uk
    sleep 2
    echo $string | espeak -s 120 -v en-uk
    ;;  stop)    
echo "Stopping script sayIPbs"
    ;;  *)
    echo "Usage: /etc/init.d/sayIPbs {start|stop}"
    exit 1
    ;;esac
exit 0

Once this is done, execute the following commands.

$ cd /etc/init.d
$ sudo chmod a+x sayIPbs
$ sudo update-rc.d -f sayIPbs defaults

That’s it. On the next boot, plug in some headphones and listen out towards the end of the boot process.

 

Advertisements

Published by

darrenjw

I am Professor of Stochastic Modelling within the School of Mathematics & Statistics at Newcastle University, UK. I am also a computational systems biologist.

5 thoughts on “Getting a headless Raspberry Pi to speak its IP address on boot”

  1. Hi, unfortunately I have issues with your sayIPbs script:
    pi@raspberrypi ~ $ cd /etc/init.d
    pi@raspberrypi /etc/init.d $ sudo chmod a+x sayIPbs
    pi@raspberrypi /etc/init.d $ sudo update-rc.d -f sayIPbs defaults
    update-rc.d: using dependency based boot sequencing
    insserv: warning: script ‘K01sayIPbs’ missing LSB tags and overrides
    insserv: warning: script ‘sayIPbs’ missing LSB tags and overrides
    pi@raspberrypi /etc/init.d $ ./sayIPbs
    -bash: ./sayIPbs: /bin/sh^M: Defekter Interpreter: Datei oder Verzeichnis nicht gefunden (file or folder not found)

    vern-
    pixlfuxa # at # yahoo # dot # com

  2. when the system boots up the voice just speaks the adress as” the private address is curl ifconfig” .Why is it not able to get the ip adress, do i need to install any other things to get the ip in to $private veriable??

    please provide a solution for this error

    thanks in advance

    1. No – it still works for me. I suspect you’ve not copied-and-pasted the code correctly – it’s important to use backquotes rather than forward quotes where I use them.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s