pittsat
Posts: 5
Joined: Sun Jan 27, 2013 5:22 am

Re: RPi IP Emailer

Mon Jan 28, 2013 12:14 am

pittsat wrote:
pittsat wrote:No matter what I do, my RPi only emails me on reboot and poweroff. I really want it to email me its IP address when it turns on or comes back up. Here's my attempt.

Code: Select all

#!/usr/bin/python
# The idea behind this script is if plugging a RaspberryPi into a foreign network whilst running it headless
# (i.e. without a monitor/TV), you need to know what the IP address is to SSH into it.
#
# This script emails you the IP address if it detects an ethernet address other than it's usual address
# that it normally has, i.e. on your home network.

import smtplib, string, subprocess, time

################################################
###### Remember to set all constants here ######
################################################
FIXED_IP = '192.168.0.8'
IP_FILEPATH = '/home/pi/scripts/current_ip.txt'
SMTP_USERNAME = '[email protected]'
SMTP_PASSWORD = 'xxxx'
SMTP_RECIPIENT = '[email protected]'
SMTP_SERVER = 'smtp.gmail.com'
SSL_PORT = 465
################################################
 
ipaddr_string = 'ip -4 addr > ' + IP_FILEPATH
subprocess.call(ipaddr_string, shell=True)
inet_string = ''
 
ip_file = file(IP_FILEPATH, 'r')
for line in ip_file:
        
    if 'eth0:' in line:
        inet_line = ip_file.next()
        _time = time.asctime()
        inet_string = inet_line[9:(inet_line.index('/'))]
 
        if inet_string != FIXED_IP:
        
            SUBJECT = 'IP Address from Raspberry Pi at: %s' % time.asctime()
            TO = SMTP_RECIPIENT
            FROM = SMTP_USERNAME
            text = 'The IP address is: %s' % inet_string
            BODY = string.join((
                'From: %s' % FROM,
                'To: %s' % TO,
                'Subject: %s' % SUBJECT,
                '',
                text
                ), '\r\n')
            server = smtplib.SMTP_SSL(SMTP_SERVER, SSL_PORT)
            server.login(SMTP_USERNAME, SMTP_PASSWORD)
            server.sendmail(FROM, [TO], BODY)
            server.quit()
            print '[' + '\033[36;1m' + 'info'  + '\033[0m' + ']' + ' Emailing eth0 IP address ' + inet_string + ' to ' + TO + ' from ' + FROM
  
        elif inet_string == FIXED_IP:
            print '[' + '\033[32;1m' + ' ok '  + '\033[0m' + ']' + ' Normal IP address' + inet_string + 'found'
 
        else:
            print '[' + '\033[31;1m' + 'warning'  + '\033[0m' + ']' + ' eth0 not found in file!'
 
 
ip_file.close()
I would greatly appreciate any help you guys could offer me. Also, my programming ability is in its infantile stage.
Actually any python script I write will successfully send me an email if I manually run it from lxterminal. When I add it to /etc/init.d and update-rc.d script default 19 (or [start 99 2]), it only runs when the system is going down, always fails to send when it is coming back up.
So I have figured out that my RPi starts the scripts at startup, but the script isn't fully executed until shutdown. Here is a script edited to manage the issue, but I don't know how to get it to execute the script within the startup.

Code: Select all

 #! /bin/sh
# /etc/init.d/check_ip
#

# Some things that run always
python /home/pi/check_ip.py

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting script check_ip.py "
    ;;
  stop)
    echo "Stopping script check_ip.py"
    ;;
  *)
    echo "Usage: /etc/init.d/check_ip {start|stop}"
    exit 1
    ;;
esac

exit 0 

pittsat
Posts: 5
Joined: Sun Jan 27, 2013 5:22 am

Re: RPi IP Emailer

Tue Jan 29, 2013 4:35 am

I figured out my problem. I wrote a shell script to handle the python code. I directed crontab to run it @reboot. The program didn't execute. I checked syslog and found that cron started the program before the IP address resolved. So I added "Sleep 15" to my shell script. The delay worked. Thanks for the program!

pittsat
Posts: 5
Joined: Sun Jan 27, 2013 5:22 am

Re: RPi IP Emailer

Tue Jan 29, 2013 4:39 am

pittsat wrote:
pittsat wrote:
pittsat wrote:No matter what I do, my RPi only emails me on reboot and poweroff. I really want it to email me its IP address when it turns on or comes back up. Here's my attempt.

Code: Select all

#!/usr/bin/python
# The idea behind this script is if plugging a RaspberryPi into a foreign network whilst running it headless
# (i.e. without a monitor/TV), you need to know what the IP address is to SSH into it.
#
# This script emails you the IP address if it detects an ethernet address other than it's usual address
# that it normally has, i.e. on your home network.

import smtplib, string, subprocess, time

################################################
###### Remember to set all constants here ######
################################################
FIXED_IP = '192.168.0.8'
IP_FILEPATH = '/home/pi/scripts/current_ip.txt'
SMTP_USERNAME = '[email protected]'
SMTP_PASSWORD = 'xxxx'
SMTP_RECIPIENT = '[email protected]'
SMTP_SERVER = 'smtp.gmail.com'
SSL_PORT = 465
################################################
 
ipaddr_string = 'ip -4 addr > ' + IP_FILEPATH
subprocess.call(ipaddr_string, shell=True)
inet_string = ''
 
ip_file = file(IP_FILEPATH, 'r')
for line in ip_file:
        
    if 'eth0:' in line:
        inet_line = ip_file.next()
        _time = time.asctime()
        inet_string = inet_line[9:(inet_line.index('/'))]
 
        if inet_string != FIXED_IP:
        
            SUBJECT = 'IP Address from Raspberry Pi at: %s' % time.asctime()
            TO = SMTP_RECIPIENT
            FROM = SMTP_USERNAME
            text = 'The IP address is: %s' % inet_string
            BODY = string.join((
                'From: %s' % FROM,
                'To: %s' % TO,
                'Subject: %s' % SUBJECT,
                '',
                text
                ), '\r\n')
            server = smtplib.SMTP_SSL(SMTP_SERVER, SSL_PORT)
            server.login(SMTP_USERNAME, SMTP_PASSWORD)
            server.sendmail(FROM, [TO], BODY)
            server.quit()
            print '[' + '\033[36;1m' + 'info'  + '\033[0m' + ']' + ' Emailing eth0 IP address ' + inet_string + ' to ' + TO + ' from ' + FROM
  
        elif inet_string == FIXED_IP:
            print '[' + '\033[32;1m' + ' ok '  + '\033[0m' + ']' + ' Normal IP address' + inet_string + 'found'
 
        else:
            print '[' + '\033[31;1m' + 'warning'  + '\033[0m' + ']' + ' eth0 not found in file!'
 
 
ip_file.close()
I would greatly appreciate any help you guys could offer me. Also, my programming ability is in its infantile stage.
Actually any python script I write will successfully send me an email if I manually run it from lxterminal. When I add it to /etc/init.d and update-rc.d script default 19 (or [start 99 2]), it only runs when the system is going down, always fails to send when it is coming back up.
So I have figured out that my RPi starts the scripts at startup, but the script isn't fully executed until shutdown. Here is a script edited to manage the issue, but I don't know how to get it to execute the script within the startup.

Code: Select all

 #! /bin/sh
# /etc/init.d/check_ip
#

# Some things that run always
python /home/pi/check_ip.py

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting script check_ip.py "
    ;;
  stop)
    echo "Stopping script check_ip.py"
    ;;
  *)
    echo "Usage: /etc/init.d/check_ip {start|stop}"
    exit 1
    ;;
esac

exit 0 
By the way, the above shell script is bad. Loops the program until the system is disabled.

raspi2013
Posts: 2
Joined: Sun Jan 27, 2013 9:11 pm

Re: RPi IP Emailer

Thu Jan 31, 2013 9:07 pm

If you publish a script involving email or web addresses you should never ever use real existing IPs or domains, else you're starting a dDoS attack on these domains/IPs.
http://en.wikipedia.org/wiki/DoS#Distributed_attack

There are special domains like example.com for this purpose.
http://en.wikipedia.org/wiki/Example.com

Many people will just start this script without even reading any documentation, and those people all attacking gmail.com with a wrong sender and wrong password. Of course, google will for sure handle the amount of login tries. But imagine if everyone in every script would use gmail.com as an example ...

Return to “Networking and servers”