Skip to content

Instantly share code, notes, and snippets.

@slottermoser
Forked from Morrolan/check_ip.py
Last active December 10, 2015 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slottermoser/4441249 to your computer and use it in GitHub Desktop.
Save slottermoser/4441249 to your computer and use it in GitHub Desktop.

Check IP

Have your Raspberry Pi report its WIFI IP on boot via email. This works well with the Edimax EW-7811n WIFI Adapter.

Installation

Download the check_ip.py file:

wget https://gist.github.com/raw/4441249/check_ip.py

Edit the variables at the top of the script to add your own email credentials. Setting up a standalone gmail account to do the actual email sending would be a good idea.

Setup the script to run on boot with the following:

chmod 755 check_ip.py
sudo cp check_ip.py /etc/init.d/
sudo update-rc.d check_ip.py defaults 19

For help setting up the Edimax wifi adapter, check out this document. I just edited the /etc/network/interfaces and /etc/wpa_supplicant.conf files.

#!/usr/bin/python
# 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.
import smtplib, string, subprocess, time, os
################################################
###### Remember to set all constants here ######
################################################
FIXED_IP = '10.0.1.2'
IP_FILEPATH = '~/current_ip.txt'
SMTP_USERNAME = 'feersumendjinn@gmail.com'
SMTP_PASSWORD = 'password'
SMTP_RECIPIENT = 'feersumendjinn@gmail.com'
SMTP_SERVER = 'smtp.gmail.com'
SSL_PORT = 465
################################################
IP_FILEPATH = os.path.expanduser(IP_FILEPATH)
adapter = 'wlan0:'
ipaddr_string = 'ip -4 addr > ' + IP_FILEPATH
subprocess.call(ipaddr_string, shell=True)
inet_string = ''
ip_file = file(IP_FILEPATH, 'r')
for line in ip_file:
if adapter in line:
inet_line = ip_file.next()
_time = time.asctime()
_mac = file('/sys/class/net/eth0/address').read()
inet_string = inet_line[9:(inet_line.index('/'))]
if inet_string:
p = subprocess.Popen(["runlevel"], stdout=subprocess.PIPE)
out, err=p.communicate()
print out
print out[2]
if out[2] == '0':
print 'Halt detected'
exit(0)
if out [2] == '6':
print 'Shutdown detected'
exit(0)
SUBJECT = 'IP Address from Raspberry Pi at: %s' % time.asctime()
TO = SMTP_RECIPIENT
FROM = SMTP_USERNAME
text = 'The IP address is: %s and the mac address is %s' % (inet_string, _mac)
BODY = string.join((
'From: %s' % FROM,
'To: %s' % TO,
'Subject: %s' % SUBJECT,
'',
text
), '\r\n')
server = smtplib.SMTP_SSL(SMTP_SERVER, SSL_PORT)
server.login(SMTP_USERNAME, SMTP_PASSWORD)
server.sendmail(FROM, [TO], BODY)
server.quit()
print '[' + '\033[36;1m' + 'info' + '\033[0m' + ']' + ' Emailing wlan0 IP address ' + inet_string + ' to ' + TO + ' from ' + FROM
ip_file.close()
rm_ipaddr_string = 'rm ' + IP_FILEPATH
subprocess.call(rm_ipaddr_string, shell=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment