I recently took mine to show my brother, but with his 42" TV on the wall and his router in another room, I had to choose - networking or TV output?
I opted for networking and then realised my fatal flaw - I had no idea what IP address the RaspberryPi was given, so couldn't remote into it.
So, last night I wrote myself a little Python script that runs on boot. If it detects an IP address other than it's usual reserved address from my network, it emails me it's IP address with a timestamp (to ensure it is accurate/up to date).
It can be found here https://gist.github.com/2987424 which will be kept up to date with any minor revisions, or below:
- Code: Select all
# The idea behind this script is if plugging a RaspberryPi into a foreign network whilst running it headless
# (i.e. without a monitor/TV), you need to know what the IP address is to SSH into it.
# This script emails you the IP address if it detects an ethernet address other than it's usual address
# that it normally has, i.e. on your home network.
FIXED_IP = '10.10.2.10'
ipaddr_string = 'ip -4 addr > ~/current_ip.txt'
ip_file = file('current_ip.txt', 'r')
for line in ip_file:
if 'eth0:' in line:
inet_line = ip_file.next()
_time = time.asctime()
inet_string = inet_line[9:(inet_line.index('/'))]
if inet_string != FIXED_IP:
print 'Found eth0: %s' % inet_string
SUBJECT = 'IP Address from Raspberry Pi at: %s' % time.asctime()
TO = 'firstname.lastname@example.org'
FROM = 'email@example.com'
text = 'The IP address is: %s' % inet_string
BODY = string.join((
'From: %s' % FROM,
'To: %s' % TO,
'Subject: %s' % SUBJECT,
server = smtplib.SMTP_SSL('smtp.gmail.com', 465) # NOTE: This is the GMAIL SSL port.
server.sendmail(FROM, [TO], BODY)
# enable the following 2 lines to delete the text file afterwards, i.e. to make it a bit cleaner.
#_string = 'rm -f ~/current_ip.txt'
Once you download it and save it as whatever (I called it check_ip.py), run
- Code: Select all
$ chmod 755 check_ip.py
$ sudo cp check_ip.py /etc/initid/
$ sudo update-rc.d check_ip.py defaults 19
This will run the script pretty much after everything else (with a priority of 19).
There are a few things to remember -
[*] Remember to change the FIXED_IP constant to be your usual Raspberry Pi IP address.
[*] Remember to change the emails addresses, stmp server, SSL port and user details.
It's up to you, but I registered a new gmail account to use for things like this so that I don't compromise my main email address.
Hopefully others besides myself will find it useful