atux_null
Posts: 109
Joined: Thu Jun 21, 2012 1:58 pm

send ip on boot

Thu Aug 16, 2018 9:07 am

Hi. i am trying to make a setup on the system to send an email with its IP on boot.
i have ssmtp in the system and i can send email from the cli by issuing

Code: Select all

echo "Hello world email body" | mail -s "Test Subject" [email protected]
then i am trying to follow the code from https://elinux.org/RPi_Email_IP_On_Boot_Debian.
if i reboot it, there is not an email with IP. then i am trying to execute it manually in the cli by issuing

Code: Select all

[email protected]:~/Code $ ls
startup_mailer.py
[email protected]:~/Code $ python
python      python2.7   python3.5   python3m
python2     python3     python3.5m
[email protected]:~/Code $ python3 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
[email protected]:~/Code $ 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
[email protected]:~/Code $

it points out that

Code: Select all

split_line_b = ip_lines[2].split()
is out of range. No idea how to fix that.
any ideas on how to fix that problem please?

pcmanbob
Posts: 4253
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: send ip on boot

Thu Aug 16, 2018 11:58 am

Without seeing your actual code we cant tell if you have an error in it, just because the web site has the code listed does not mean your code is exactly the same.

If you have SSMTP and mail utils installed on your pi then you could try this code which is just a bash script , which i saved as ipemail.sh in /home/pi/

Code: Select all


#!/bin/bash
# send IP email from 

echo "Pi IP Address " > /home/pi/ipmessage.txt
hostname -I >> /home/pi/ipmessage.txt

SUBJECT="Pi IP Address : "$(hostname -I)

echo "read file"
while read -r address ; do 
    echo "Sending mail to :" $address
    mail -s "$SUBJECT"  < /home/pi/ipmessage.txt $address
    echo "Mail sent"
done < "/home/pi/address.txt"

rm /home/pi/ipmessage.txt
you also need to have a text file in the /home/pi directory with the addresses in that you want to send the mail to which should look like this

Code: Select all

[email protected]
[email protected]
 a blank line
[code]


after making it executable I call it on boot up using cron like this.
[code]
@reboot /home/pi/ipemail.sh
if you are using WI-FI you might need to call it like this

Code: Select all

@reboot sleep 30 && /home/pi/ipemail.sh
which will allow an extra 30 seconds for the connection to be established.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

atux_null
Posts: 109
Joined: Thu Jun 21, 2012 1:58 pm

Re: send ip on boot

Fri Aug 17, 2018 6:10 am

thanks for your reply.
here is the code for my pi 0 w

Code: Select all

__author__ = 'Cody Giles'
__license__ = "Creative Commons Attribution-ShareAlike 3.0 Unported License"
__version__ = "1.0"
__maintainer__ = "Cody Giles"
__status__ = "Production"

import subprocess
import smtplib
from email.mime.text import MIMEText
import datetime

def connect_type(word_list):
    """ This function takes a list of words, then, depeding which key word, returns the corresponding
    internet connection type as a string. ie) 'ethernet'.
    """
    if 'wlan0' in word_list or 'wlan1' in word_list:
        con_type = 'wifi'
    elif 'eth0' in word_list:
        con_type = 'ethernet'
    else:
        con_type = 'current'

    return con_type

# Change to your own account information
# Account Information
to = '[email protected]' # Email to send to.
gmail_user = '[email protected]' # Email to send from. (MUST BE GMAIL)
gmail_password = '[email protected]' # Gmail password.
smtpserver = smtplib.SMTP('smtp.gmail.com', 587) # Server to use.

smtpserver.ehlo()  # Says 'hello' to the server
smtpserver.starttls()  # Start TLS encryption
smtpserver.ehlo()
smtpserver.login(gmail_user, gmail_password)  # Log in to server
today = datetime.date.today()  # Get current time/date

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()

# con_type variables for the message text. ex) 'ethernet', 'wifi', etc.
ip_type_a = connect_type(split_line_a)
ip_type_b = connect_type(split_line_b)

"""Because the text 'src' is always followed by an ip address,
we can use the 'index' function to find 'src' and add one to
get the index position of our ip.
"""
ipaddr_a = split_line_a[split_line_a.index('src')+1]
ipaddr_b = split_line_b[split_line_b.index('src')+1]

# Creates a sentence for each ip address.
my_ip_a = 'Your %s ip is %s' % (ip_type_a, ipaddr_a)
my_ip_b = 'Your %s ip is %s' % (ip_type_b, ipaddr_b)

# Creates the text, subject, 'from', and 'to' of the message.
msg = MIMEText(my_ip_a + "\n" + my_ip_b)
msg['Subject'] = 'IPs For RaspberryPi on %s' % today.strftime('%b %d %Y')
msg['From'] = gmail_user
msg['To'] = to
# Sends the message
smtpserver.sendmail(gmail_user, [to], msg.as_string())
# Closes the smtp server.
smtpserver.quit()
where is the error?

pcmanbob
Posts: 4253
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: send ip on boot

Fri Aug 17, 2018 12:50 pm

How many lines are in ip_lines ?

if its only 2 the you need to change the lines

Code: Select all

split_line_a = ip_lines[1].split()
split_line_b = ip_lines[2].split()
to

Code: Select all

split_line_a = ip_lines[0].split()
split_line_b = ip_lines[1].split()
because you have to start from 0 when selecting split data fields.

if you are not sure what ip_lines contains try adding a print statement like this

Code: Select all

# Split IP text block into three, and divide the two containing IPs into words.
ip_lines = data[0].splitlines()
print (ip_lines)
split_line_a = ip_lines[1].split()
split_line_b = ip_lines[2].split()
then run from command line and look at the output to see what ip_lines contains.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Return to “Networking and servers”

Who is online

Users browsing this forum: No registered users and 6 guests