I need the IP address of my Raspberry Pi so that I can SSH into it. This must be possible to do at my university without a monitor connected. So I made a Python script which sends me the IP address by email, but I can't get it to run at boot. I tried adding python3 /home/pi/code/ipmailer.py
to the end of /home/pi/.bashrc
but I couldn't get it to work. Does anyone know how to get this to work? Or another way to get the IP?
I have a Raspberry Pi 2 model B running Raspbian.
The Python script:
import smtplib from email.mime.text
import MIMEText
import datetime
import subprocess
ip = subprocess.check_output(['hostname', '-I'])
to = 'My email address'
gmail_user = 'Gmail used to send from'
gmail_password = 'Password'
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(gmail_user, gmail_password)
today = datetime.date.today()
my_msg = '%s' % today.strftime('%b %d %Y')
msg = MIMEText(my_msg)
msg['Subject'] = 'Raspberry Pi IP: %s' % ip[:-2].decode("UTF-8")
msg['From'] = gmail_user
msg['To'] = to
smtpserver.sendmail(gmail_user, [to], msg.as_string())
smtpserver.quit()
ping raspberrypi.local
orssh raspberrypi.local
`zero-conf`` works on most networks, although university may block. – Milliways Feb 11 '16 at 22:52cron
and @reboot? – Ghanima Feb 11 '16 at 22:54cron
.Run
– Milliways Feb 12 '16 at 03:38crontab -e
Then add a line like@ reboot sudo python /home/pi/⋯.py