Do you know what really grinds my gears. Having to boot Raspberry Pi with a new image on and no HDTV to configure and find the IP in order to ssh to it, and have fun with it. Grrrrrrrrr…
That’s when I decided why not create a script, that when the pi boots up it sends me the internal and external IP addresses…
Clever right.
gedit whats_my_ip.py
copy and paste the following code and don’t forget to edit the username, password and address.
import subprocess import smtplib import socket from email.mime.text import MIMEText import datetime import urllib2 import time time.sleep(10) # Change to your own account information gmail_user = "***@gmail.com" gmail_password= "******" to = "***@gmail.com" smtpserver = smtplib.SMTP('smtp.gmail.com', 587) smtpserver.ehlo() smtpserver.starttls() smtpserver.ehlo smtpserver.login(gmail_user, gmail_password) today = datetime.date.today() arg='ip route list' p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE) data = p.communicate() split_data = data[0].split() ipaddr = split_data[split_data.index('src')+1] extipaddr = urllib2.urlopen("http://icanhazip.com").read() my_ip = 'Local address: %s\nExternal address: %s' % (ipaddr, extipaddr) msg = MIMEText(my_ip) msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y') msg['From'] = gmail_user msg['To'] = to smtpserver.sendmail(gmail_user, [to], msg.as_string()) smtpserver.quit()
Now for this to work you need to link the script to /etc/rc.local this will only run it once on reboot.
python /home/whats_my_ip.py
# More mods are underway