YouKidsGetOffMyLAN
Posts: 1
Joined: Wed Oct 07, 2015 11:35 pm

Help on email at bootup

Wed Oct 07, 2015 11:51 pm

Hello! I recently puchased a raspberry pi.
I already have a vnc server running for use without a monitor.
Right now im trying to setup a script that will allow me to send myself an email with the Pi's ip address
following this guide:
http://elinux.org/RPi_Email_IP_On_Boot_Debian

I never recieve a email upon startup. Running the program itself replies with

Code: Select all

pi@raspberrypi ~/Code $ sudo python startup_mailer.py
Traceback (most recent call last):
  File "startup_mailer.py", line 46, in <module>
    split_line_b = ip_lines[2].split()
IndexError: list index out of range
pi@raspberrypi ~/Code $ 
I have been trying to solve this now for a few hours with no luck.
Google is letting me down on this one. Does anyone have expirence with this
setup? Any input with be appriciated.

Code: Select all

arg='ip route list'  # Linux command to retrieve ip addresses.
# Runs 'arg' in a 'hidden terminal'.
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
data = p.communicate()  # Get data from 'p terminal'.

# Split IP text block into three, and divide the two containing IPs into words.
ip_lines = data[0].splitlines()
split_line_a = ip_lines[1].split()
split_line_b = ip_lines[2].split()

Code: Select all

pi@raspberrypi ~/Code $ ip route list
default via 192.168.0.1 dev wlan0  metric 303 
192.168.0.0/24 dev wlan0  proto kernel  scope link  src 192.168.0.103  metric 303 
pi@raspberrypi ~/Code $ 

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Help on email at bootup

Thu Oct 08, 2015 12:19 pm

Well, I can see why you're getting the error: the script expects (at least) 3 lines to be returned by "ip route list" but it's only getting two so you get an error when it tries to access the third line.

The problem is I don't know what "ip route list" should be returning and why that 3rd line is important? If your IP address was the "192.168.0.103" in the second line then the code could be changed to get the value.

EDIT: Looking at the code, I think this is just looking for two network interfaces but you've only got one. You could therefore edit the code to just look at the first network interface, or, if you're feeling adventurous, change the code so it can handle 1 or more interfaces.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

suneetagera
Posts: 12
Joined: Mon Dec 07, 2015 10:28 am

Re: Help on email at bootup

Fri Dec 11, 2015 4:23 am

Faced the same issue, saw the resolution .... changed my code accordingly. Works great. Thanks.

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Help on email at bootup

Fri Dec 11, 2015 8:50 pm

Try this

Code: Select all

#!/usr/bin/python3
import time
from datetime import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import socket
import os
testIP = "8.8.8.8"
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((testIP, 0))
ipaddr = s.getsockname()[0]
host = socket.gethostname()

f_time = datetime.now().strftime('%a %d %b @ %H:%M')

toaddr = 'example@gmail.com'    # <== CHANGE ME
me = 'example@gmail.com'       # <== AND ME
password = 'redacted'            # <== AND ME
subject = 'IP address for '+ host + " at "  + f_time

msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = me
msg['To'] = toaddr

msg.preamble = "Test message @ " + f_time
message = "Current IP: " + ipaddr
html = "<html><head></head><body><h1>Your IP address</h1><p>Your Raspberry has IP address: "+ ipaddr + "</body></html>"
plainText = MIMEText(message, 'plain')
htmlText = MIMEText(html,'html')
msg.attach(plainText)

try:
   s = smtplib.SMTP('smtp.gmail.com',587)
   s.ehlo()
   s.starttls()
   s.login(me,password)
   s.send_message(msg)
   s.quit()
except smtplib.SMTPException as e:
   print (str(e))
   print ("Error: unable to send email")
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “Python”