I have two RPi's. On the first I have a startup mailer written in Python which is called via rc.local. This works well.
I've copied the rc.local and the python script from the first RPi to the second. I've set the execution bit for the rc.local file.
If I do the following once I've logged in I get the result I'm after: an email with my internal and external IP address.
Code: Select all
sudo bash rc.local
Code: Select all
exec 2> /tmp.rc.local.debug
set -x
Code: Select all
+ sudo python /home/pi/scripts/startup_mailer.py
Traceback (most recent call last):
File "/home/pi/scripts/startup_mailer.py", line 12, in <module>
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
File "/usr/lib/python2.7/smtplib.py", line 249, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 309, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 284, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
Code: Select all
import urllib2
import subprocess
import smtplib
import socket
import string
from email.mime.text import MIMEText
import datetime
# Change to your own account information
to = 'XXXXXX' # enter your receiving email account here
gmail_user = 'XXXXXXXXX' # enter your gmail acc here to send
gmail_password = 'XXXXXXXX' # enter your gmail password here
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_password)
today = datetime.date.today()
ext_ip = urllib2.urlopen('http://checkip.dyndns.org').read()
# Very Linux Specific
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]
my_ip = 'PiA IP: LAN Address: %s' % ipaddr + (' External '+ ext_ip[56:91])
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()
Thank you