https://elinux.org/api.php?action=feedcontributions&user=Kenwaldek&feedformat=atomeLinux.org - User contributions [en]2024-03-28T15:11:35ZUser contributionsMediaWiki 1.31.0https://elinux.org/index.php?title=RPi_Email_IP_On_Boot_Debian&diff=310652RPi Email IP On Boot Debian2014-01-02T17:04:30Z<p>Kenwaldek: /* Trouble shooting */</p>
<hr />
<div>[[Category:RaspberryPi]]<br />
Back to [[RPi Guides]].<br />
<br />
'''Send Email Containing IP Address On Boot'''<br />
<br />
=What does it do?=<br />
<br />
This code will extract the ip address of your Pi and then send an email containing the ip to the specified email address. This is inspired by the need to access the Pi via SSH or other network protocols without a monitor and moving from network to network. This assumes a Gmail SMTP server. You may need to alter a bit for other servers (beyond scope here)<br />
<br />
=What do you need?=<br />
<br />
A working, and network enabled Raspberry Pi<br />
<br />
=What skill level is required?=<br />
<br />
Medium Level. You should be comfortable navigating a linux system and be comfortable using sudo (if you want to use this script, odds are you are quite comfortable at the command prompt).<br />
<br />
=Overview of this guide=<br />
You need to<br />
*Create a python script and store it in a Directory<br />
*Make python script executable<br />
*Edit '''''/boot/boot.rc'''''<br />
<br />
=Let's Do It=<br />
<br />
==Create the python script==<br />
Copy and paste the following code into a text editor (I'm a nano man myself)<br />
<br />
import subprocess<br />
import smtplib<br />
import socket<br />
from email.mime.text import MIMEText<br />
import datetime<br />
# Change to your own account information<br />
to = 'me@example.com'<br />
gmail_user = 'test@gmail.com'<br />
gmail_password = 'yourpassword'<br />
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)<br />
smtpserver.ehlo()<br />
smtpserver.starttls()<br />
smtpserver.ehlo<br />
smtpserver.login(gmail_user, gmail_password)<br />
today = datetime.date.today()<br />
# Very Linux Specific<br />
arg='ip route list'<br />
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)<br />
data = p.communicate()<br />
split_data = data[0].split()<br />
ipaddr = split_data[split_data.index('src')+1]<br />
my_ip = 'Your ip is %s' % ipaddr<br />
msg = MIMEText(my_ip)<br />
msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y')<br />
msg['From'] = gmail_user<br />
msg['To'] = to<br />
smtpserver.sendmail(gmail_user, [to], msg.as_string())<br />
smtpserver.quit()<br />
<br />
Save this script using a nice name like ''''startup_mailer.py'''' and make note of its path (like '''''/home/pi/Code/startup_mailer.py''''')<br />
<br />
For good measure, make the script executable<br />
sudo chmod +x startup_mailer.py<br />
<br />
==Edit /boot/boot.rc==<br />
Using your text editor once again, edit '''''/boot/boot.rc''''' (this assumes you have already renamed this file to boot.rc If not, see [[RPi_Advanced_Setup]]). For example:<br />
sudo nano /boot/boot.rc<br />
<br />
Add the following at the end of the file, making changes to the path for your directory tree and save.<br />
#Script to email ip address upon reboot<br />
python /home/pi/Code/startup_mailer.py<br />
<br />
== Alternative if using Rasbian ==<br />
If you are using Rasbian you won't have a /boot/boot.rc file. Instead you can edit /etc/rc.local as follows:<br />
<br />
sudo nano /etc/rc.local<br />
<br />
Add the python line so the file now looks like this:<br />
<br />
# rc.local<br />
#<br />
# This script is executed at the end of each multiuser runlevel.<br />
# Make sure that the script will "exit 0" on success or any other<br />
# value on error.<br />
#<br />
# In order to enable or disable this script just change the execution<br />
# bits.<br />
#<br />
# By default this script does nothing.<br />
# Print the IP address if it doesn't work ad sleep 30 before all your code <br />
_IP=$(hostname -I) || true<br />
if [ "$_IP" ]; then<br />
printf "My IP address is %s\n" "$_IP"<br />
python /home/pi/Code/startup_mailer.py<br />
fi<br />
exit 0<br />
<br />
==Finish up==<br />
Reboot your Pi and you should receive an email with your ip address<br />
<br />
== Trouble shooting ==<br />
<br />
+ you may add (sleep 30) in the etc/rc.local just after the # if you don't get your email notification<br />
hang your pi to a monitor and check at bootup if it shows My ip adresss is .... if it doesn't do that! sleep 30.<br />
etc/rc.local is linked to etc/init.d/rc.local it's an special service that starts on bootup but it seems there are some bugs in it.<br />
<br />
+ If you don't get email when rebooting, you have to check the hostname you currently have. because the script calls for #raspberrypi. Just type in the command line # hostname. if you have an other hostname its simple just change you hostname #with your preferred editor. Also in etc/hosts on the bottom of the page <br />
<br />
$ msg['Subject'] = 'IP For YOUR HOSTNAME on %s' % today.strftime('%b %d %Y')</div>Kenwaldekhttps://elinux.org/index.php?title=RPi_Email_IP_On_Boot_Debian&diff=310646RPi Email IP On Boot Debian2014-01-02T17:01:10Z<p>Kenwaldek: /* Trouble shooting */</p>
<hr />
<div>[[Category:RaspberryPi]]<br />
Back to [[RPi Guides]].<br />
<br />
'''Send Email Containing IP Address On Boot'''<br />
<br />
=What does it do?=<br />
<br />
This code will extract the ip address of your Pi and then send an email containing the ip to the specified email address. This is inspired by the need to access the Pi via SSH or other network protocols without a monitor and moving from network to network. This assumes a Gmail SMTP server. You may need to alter a bit for other servers (beyond scope here)<br />
<br />
=What do you need?=<br />
<br />
A working, and network enabled Raspberry Pi<br />
<br />
=What skill level is required?=<br />
<br />
Medium Level. You should be comfortable navigating a linux system and be comfortable using sudo (if you want to use this script, odds are you are quite comfortable at the command prompt).<br />
<br />
=Overview of this guide=<br />
You need to<br />
*Create a python script and store it in a Directory<br />
*Make python script executable<br />
*Edit '''''/boot/boot.rc'''''<br />
<br />
=Let's Do It=<br />
<br />
==Create the python script==<br />
Copy and paste the following code into a text editor (I'm a nano man myself)<br />
<br />
import subprocess<br />
import smtplib<br />
import socket<br />
from email.mime.text import MIMEText<br />
import datetime<br />
# Change to your own account information<br />
to = 'me@example.com'<br />
gmail_user = 'test@gmail.com'<br />
gmail_password = 'yourpassword'<br />
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)<br />
smtpserver.ehlo()<br />
smtpserver.starttls()<br />
smtpserver.ehlo<br />
smtpserver.login(gmail_user, gmail_password)<br />
today = datetime.date.today()<br />
# Very Linux Specific<br />
arg='ip route list'<br />
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)<br />
data = p.communicate()<br />
split_data = data[0].split()<br />
ipaddr = split_data[split_data.index('src')+1]<br />
my_ip = 'Your ip is %s' % ipaddr<br />
msg = MIMEText(my_ip)<br />
msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y')<br />
msg['From'] = gmail_user<br />
msg['To'] = to<br />
smtpserver.sendmail(gmail_user, [to], msg.as_string())<br />
smtpserver.quit()<br />
<br />
Save this script using a nice name like ''''startup_mailer.py'''' and make note of its path (like '''''/home/pi/Code/startup_mailer.py''''')<br />
<br />
For good measure, make the script executable<br />
sudo chmod +x startup_mailer.py<br />
<br />
==Edit /boot/boot.rc==<br />
Using your text editor once again, edit '''''/boot/boot.rc''''' (this assumes you have already renamed this file to boot.rc If not, see [[RPi_Advanced_Setup]]). For example:<br />
sudo nano /boot/boot.rc<br />
<br />
Add the following at the end of the file, making changes to the path for your directory tree and save.<br />
#Script to email ip address upon reboot<br />
python /home/pi/Code/startup_mailer.py<br />
<br />
== Alternative if using Rasbian ==<br />
If you are using Rasbian you won't have a /boot/boot.rc file. Instead you can edit /etc/rc.local as follows:<br />
<br />
sudo nano /etc/rc.local<br />
<br />
Add the python line so the file now looks like this:<br />
<br />
# rc.local<br />
#<br />
# This script is executed at the end of each multiuser runlevel.<br />
# Make sure that the script will "exit 0" on success or any other<br />
# value on error.<br />
#<br />
# In order to enable or disable this script just change the execution<br />
# bits.<br />
#<br />
# By default this script does nothing.<br />
# Print the IP address if it doesn't work ad sleep 30 before all your code <br />
_IP=$(hostname -I) || true<br />
if [ "$_IP" ]; then<br />
printf "My IP address is %s\n" "$_IP"<br />
python /home/pi/Code/startup_mailer.py<br />
fi<br />
exit 0<br />
<br />
==Finish up==<br />
Reboot your Pi and you should receive an email with your ip address<br />
<br />
== Trouble shooting ==<br />
<br />
# you may add (sleep 30) in the rc.local just after the # if you don't get your email notification<br />
hang your pi to a monitor and check at bootup if it shows My ip adresss is .... if it doesn't do that! sleep 30.<br />
etc/rc.local is linked to etc/init.d/rc.local it's an special service that starts on bootup but it seems there are some bugs in it.<br />
<br />
# If you don't get email when rebooting, you have to check the hostname you currently have. because the script calls for #raspberrypi. Just type in the command line # hostname. if you have an other hostname its simple just change you hostname #with your preferred editor.<br />
<br />
$ msg['Subject'] = 'IP For YOUR HOSTNAME on %s' % today.strftime('%b %d %Y')</div>Kenwaldekhttps://elinux.org/index.php?title=RPi_Email_IP_On_Boot_Debian&diff=310640RPi Email IP On Boot Debian2014-01-02T16:55:47Z<p>Kenwaldek: /* Alternative if using Rasbian */</p>
<hr />
<div>[[Category:RaspberryPi]]<br />
Back to [[RPi Guides]].<br />
<br />
'''Send Email Containing IP Address On Boot'''<br />
<br />
=What does it do?=<br />
<br />
This code will extract the ip address of your Pi and then send an email containing the ip to the specified email address. This is inspired by the need to access the Pi via SSH or other network protocols without a monitor and moving from network to network. This assumes a Gmail SMTP server. You may need to alter a bit for other servers (beyond scope here)<br />
<br />
=What do you need?=<br />
<br />
A working, and network enabled Raspberry Pi<br />
<br />
=What skill level is required?=<br />
<br />
Medium Level. You should be comfortable navigating a linux system and be comfortable using sudo (if you want to use this script, odds are you are quite comfortable at the command prompt).<br />
<br />
=Overview of this guide=<br />
You need to<br />
*Create a python script and store it in a Directory<br />
*Make python script executable<br />
*Edit '''''/boot/boot.rc'''''<br />
<br />
=Let's Do It=<br />
<br />
==Create the python script==<br />
Copy and paste the following code into a text editor (I'm a nano man myself)<br />
<br />
import subprocess<br />
import smtplib<br />
import socket<br />
from email.mime.text import MIMEText<br />
import datetime<br />
# Change to your own account information<br />
to = 'me@example.com'<br />
gmail_user = 'test@gmail.com'<br />
gmail_password = 'yourpassword'<br />
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)<br />
smtpserver.ehlo()<br />
smtpserver.starttls()<br />
smtpserver.ehlo<br />
smtpserver.login(gmail_user, gmail_password)<br />
today = datetime.date.today()<br />
# Very Linux Specific<br />
arg='ip route list'<br />
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)<br />
data = p.communicate()<br />
split_data = data[0].split()<br />
ipaddr = split_data[split_data.index('src')+1]<br />
my_ip = 'Your ip is %s' % ipaddr<br />
msg = MIMEText(my_ip)<br />
msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y')<br />
msg['From'] = gmail_user<br />
msg['To'] = to<br />
smtpserver.sendmail(gmail_user, [to], msg.as_string())<br />
smtpserver.quit()<br />
<br />
Save this script using a nice name like ''''startup_mailer.py'''' and make note of its path (like '''''/home/pi/Code/startup_mailer.py''''')<br />
<br />
For good measure, make the script executable<br />
sudo chmod +x startup_mailer.py<br />
<br />
==Edit /boot/boot.rc==<br />
Using your text editor once again, edit '''''/boot/boot.rc''''' (this assumes you have already renamed this file to boot.rc If not, see [[RPi_Advanced_Setup]]). For example:<br />
sudo nano /boot/boot.rc<br />
<br />
Add the following at the end of the file, making changes to the path for your directory tree and save.<br />
#Script to email ip address upon reboot<br />
python /home/pi/Code/startup_mailer.py<br />
<br />
== Alternative if using Rasbian ==<br />
If you are using Rasbian you won't have a /boot/boot.rc file. Instead you can edit /etc/rc.local as follows:<br />
<br />
sudo nano /etc/rc.local<br />
<br />
Add the python line so the file now looks like this:<br />
<br />
# rc.local<br />
#<br />
# This script is executed at the end of each multiuser runlevel.<br />
# Make sure that the script will "exit 0" on success or any other<br />
# value on error.<br />
#<br />
# In order to enable or disable this script just change the execution<br />
# bits.<br />
#<br />
# By default this script does nothing.<br />
# Print the IP address if it doesn't work ad sleep 30 before all your code <br />
_IP=$(hostname -I) || true<br />
if [ "$_IP" ]; then<br />
printf "My IP address is %s\n" "$_IP"<br />
python /home/pi/Code/startup_mailer.py<br />
fi<br />
exit 0<br />
<br />
==Finish up==<br />
Reboot your Pi and you should receive an email with your ip address<br />
<br />
== Trouble shooting ==<br />
# If you don't get email when rebooting, you have to check the hostname you currently have. because the script calls for #raspberrypi. Just type in the command line # hostname. if you have an other hostname its simple just change you hostname #with your preferred editor.<br />
<br />
msg['Subject'] = 'IP For YOUR HOSTNAME on %s' % today.strftime('%b %d %Y')</div>Kenwaldekhttps://elinux.org/index.php?title=RPi_Email_IP_On_Boot_Debian&diff=310634RPi Email IP On Boot Debian2014-01-02T16:54:23Z<p>Kenwaldek: /* Alternative if using Rasbian */</p>
<hr />
<div>[[Category:RaspberryPi]]<br />
Back to [[RPi Guides]].<br />
<br />
'''Send Email Containing IP Address On Boot'''<br />
<br />
=What does it do?=<br />
<br />
This code will extract the ip address of your Pi and then send an email containing the ip to the specified email address. This is inspired by the need to access the Pi via SSH or other network protocols without a monitor and moving from network to network. This assumes a Gmail SMTP server. You may need to alter a bit for other servers (beyond scope here)<br />
<br />
=What do you need?=<br />
<br />
A working, and network enabled Raspberry Pi<br />
<br />
=What skill level is required?=<br />
<br />
Medium Level. You should be comfortable navigating a linux system and be comfortable using sudo (if you want to use this script, odds are you are quite comfortable at the command prompt).<br />
<br />
=Overview of this guide=<br />
You need to<br />
*Create a python script and store it in a Directory<br />
*Make python script executable<br />
*Edit '''''/boot/boot.rc'''''<br />
<br />
=Let's Do It=<br />
<br />
==Create the python script==<br />
Copy and paste the following code into a text editor (I'm a nano man myself)<br />
<br />
import subprocess<br />
import smtplib<br />
import socket<br />
from email.mime.text import MIMEText<br />
import datetime<br />
# Change to your own account information<br />
to = 'me@example.com'<br />
gmail_user = 'test@gmail.com'<br />
gmail_password = 'yourpassword'<br />
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)<br />
smtpserver.ehlo()<br />
smtpserver.starttls()<br />
smtpserver.ehlo<br />
smtpserver.login(gmail_user, gmail_password)<br />
today = datetime.date.today()<br />
# Very Linux Specific<br />
arg='ip route list'<br />
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)<br />
data = p.communicate()<br />
split_data = data[0].split()<br />
ipaddr = split_data[split_data.index('src')+1]<br />
my_ip = 'Your ip is %s' % ipaddr<br />
msg = MIMEText(my_ip)<br />
msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y')<br />
msg['From'] = gmail_user<br />
msg['To'] = to<br />
smtpserver.sendmail(gmail_user, [to], msg.as_string())<br />
smtpserver.quit()<br />
<br />
Save this script using a nice name like ''''startup_mailer.py'''' and make note of its path (like '''''/home/pi/Code/startup_mailer.py''''')<br />
<br />
For good measure, make the script executable<br />
sudo chmod +x startup_mailer.py<br />
<br />
==Edit /boot/boot.rc==<br />
Using your text editor once again, edit '''''/boot/boot.rc''''' (this assumes you have already renamed this file to boot.rc If not, see [[RPi_Advanced_Setup]]). For example:<br />
sudo nano /boot/boot.rc<br />
<br />
Add the following at the end of the file, making changes to the path for your directory tree and save.<br />
#Script to email ip address upon reboot<br />
python /home/pi/Code/startup_mailer.py<br />
<br />
== Alternative if using Rasbian ==<br />
If you are using Rasbian you won't have a /boot/boot.rc file. Instead you can edit /etc/rc.local as follows:<br />
<br />
sudo nano /etc/rc.local<br />
<br />
Add the python line so the file now looks like this:<br />
<br />
# rc.local<br />
#<br />
# This script is executed at the end of each multiuser runlevel.<br />
# Make sure that the script will "exit 0" on success or any other<br />
# value on error.<br />
#<br />
# In order to enable or disable this script just change the execution<br />
# bits.<br />
#<br />
# By default this script does nothing.<br />
# Print the IP address<br />
sleep 30<br />
_IP=$(hostname -I) || true<br />
if [ "$_IP" ]; then<br />
printf "My IP address is %s\n" "$_IP"<br />
python /home/pi/Code/startup_mailer.py<br />
fi<br />
exit 0<br />
<br />
==Finish up==<br />
Reboot your Pi and you should receive an email with your ip address<br />
<br />
== Trouble shooting ==<br />
# If you don't get email when rebooting, you have to check the hostname you currently have. because the script calls for #raspberrypi. Just type in the command line # hostname. if you have an other hostname its simple just change you hostname #with your preferred editor.<br />
<br />
msg['Subject'] = 'IP For YOUR HOSTNAME on %s' % today.strftime('%b %d %Y')</div>Kenwaldekhttps://elinux.org/index.php?title=RPi_Email_IP_On_Boot_Debian&diff=309860RPi Email IP On Boot Debian2013-12-23T20:50:46Z<p>Kenwaldek: small update</p>
<hr />
<div>[[Category:RaspberryPi]]<br />
Back to [[RPi Guides]].<br />
<br />
'''Send Email Containing IP Address On Boot'''<br />
<br />
=What does it do?=<br />
<br />
This code will extract the ip address of your Pi and then send an email containing the ip to the specified email address. This is inspired by the need to access the Pi via SSH or other network protocols without a monitor and moving from network to network. This assumes a Gmail SMTP server. You may need to alter a bit for other servers (beyond scope here)<br />
<br />
=What do you need?=<br />
<br />
A working, and network enabled Raspberry Pi<br />
<br />
=What skill level is required?=<br />
<br />
Medium Level. You should be comfortable navigating a linux system and be comfortable using sudo (if you want to use this script, odds are you are quite comfortable at the command prompt).<br />
<br />
=Overview of this guide=<br />
You need to<br />
*Create a python script and store it in a Directory<br />
*Make python script executable<br />
*Edit '''''/boot/boot.rc'''''<br />
<br />
=Let's Do It=<br />
<br />
==Create the python script==<br />
Copy and paste the following code into a text editor (I'm a nano man myself)<br />
<br />
import subprocess<br />
import smtplib<br />
import socket<br />
from email.mime.text import MIMEText<br />
import datetime<br />
# Change to your own account information<br />
to = 'me@example.com'<br />
gmail_user = 'test@gmail.com'<br />
gmail_password = 'yourpassword'<br />
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)<br />
smtpserver.ehlo()<br />
smtpserver.starttls()<br />
smtpserver.ehlo<br />
smtpserver.login(gmail_user, gmail_password)<br />
today = datetime.date.today()<br />
# Very Linux Specific<br />
arg='ip route list'<br />
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)<br />
data = p.communicate()<br />
split_data = data[0].split()<br />
ipaddr = split_data[split_data.index('src')+1]<br />
my_ip = 'Your ip is %s' % ipaddr<br />
msg = MIMEText(my_ip)<br />
msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y')<br />
msg['From'] = gmail_user<br />
msg['To'] = to<br />
smtpserver.sendmail(gmail_user, [to], msg.as_string())<br />
smtpserver.quit()<br />
<br />
Save this script using a nice name like ''''startup_mailer.py'''' and make note of its path (like '''''/home/pi/Code/startup_mailer.py''''')<br />
<br />
For good measure, make the script executable<br />
sudo chmod +x startup_mailer.py<br />
<br />
==Edit /boot/boot.rc==<br />
Using your text editor once again, edit '''''/boot/boot.rc''''' (this assumes you have already renamed this file to boot.rc If not, see [[RPi_Advanced_Setup]]). For example:<br />
sudo nano /boot/boot.rc<br />
<br />
Add the following at the end of the file, making changes to the path for your directory tree and save.<br />
#Script to email ip address upon reboot<br />
python /home/pi/Code/startup_mailer.py<br />
<br />
== Alternative if using Rasbian ==<br />
If you are using Rasbian you won't have a /boot/boot.rc file. Instead you can edit /etc/rc.local as follows:<br />
<br />
sudo nano /etc/rc.local<br />
<br />
Add the python line so the file now looks like this:<br />
<br />
# rc.local<br />
#<br />
# This script is executed at the end of each multiuser runlevel.<br />
# Make sure that the script will "exit 0" on success or any other<br />
# value on error.<br />
#<br />
# In order to enable or disable this script just change the execution<br />
# bits.<br />
#<br />
# By default this script does nothing.<br />
# Print the IP address<br />
_IP=$(hostname -I) || true<br />
if [ "$_IP" ]; then<br />
printf "My IP address is %s\n" "$_IP"<br />
python /home/pi/Code/startup_mailer.py<br />
fi<br />
exit 0<br />
<br />
==Finish up==<br />
Reboot your Pi and you should receive an email with your ip address<br />
<br />
== Trouble shooting ==<br />
# If you don't get email when rebooting, you have to check the hostname you currently have. because the script calls for #raspberrypi. Just type in the command line # hostname. if you have an other hostname its simple just change you hostname #with your preferred editor.<br />
<br />
msg['Subject'] = 'IP For YOUR HOSTNAME on %s' % today.strftime('%b %d %Y')</div>Kenwaldekhttps://elinux.org/index.php?title=RPi_Email_IP_On_Boot_Debian&diff=309854RPi Email IP On Boot Debian2013-12-23T20:50:09Z<p>Kenwaldek: small update</p>
<hr />
<div>[[Category:RaspberryPi]]<br />
Back to [[RPi Guides]].<br />
<br />
'''Send Email Containing IP Address On Boot'''<br />
<br />
=What does it do?=<br />
<br />
This code will extract the ip address of your Pi and then send an email containing the ip to the specified email address. This is inspired by the need to access the Pi via SSH or other network protocols without a monitor and moving from network to network. This assumes a Gmail SMTP server. You may need to alter a bit for other servers (beyond scope here)<br />
<br />
=What do you need?=<br />
<br />
A working, and network enabled Raspberry Pi<br />
<br />
=What skill level is required?=<br />
<br />
Medium Level. You should be comfortable navigating a linux system and be comfortable using sudo (if you want to use this script, odds are you are quite comfortable at the command prompt).<br />
<br />
=Overview of this guide=<br />
You need to<br />
*Create a python script and store it in a Directory<br />
*Make python script executable<br />
*Edit '''''/boot/boot.rc'''''<br />
<br />
=Let's Do It=<br />
<br />
==Create the python script==<br />
Copy and paste the following code into a text editor (I'm a nano man myself)<br />
<br />
import subprocess<br />
import smtplib<br />
import socket<br />
from email.mime.text import MIMEText<br />
import datetime<br />
# Change to your own account information<br />
to = 'me@example.com'<br />
gmail_user = 'test@gmail.com'<br />
gmail_password = 'yourpassword'<br />
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)<br />
smtpserver.ehlo()<br />
smtpserver.starttls()<br />
smtpserver.ehlo<br />
smtpserver.login(gmail_user, gmail_password)<br />
today = datetime.date.today()<br />
# Very Linux Specific<br />
arg='ip route list'<br />
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)<br />
data = p.communicate()<br />
split_data = data[0].split()<br />
ipaddr = split_data[split_data.index('src')+1]<br />
my_ip = 'Your ip is %s' % ipaddr<br />
msg = MIMEText(my_ip)<br />
msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y')<br />
msg['From'] = gmail_user<br />
msg['To'] = to<br />
smtpserver.sendmail(gmail_user, [to], msg.as_string())<br />
smtpserver.quit()<br />
<br />
Save this script using a nice name like ''''startup_mailer.py'''' and make note of its path (like '''''/home/pi/Code/startup_mailer.py''''')<br />
<br />
For good measure, make the script executable<br />
sudo chmod +x startup_mailer.py<br />
<br />
==Edit /boot/boot.rc==<br />
Using your text editor once again, edit '''''/boot/boot.rc''''' (this assumes you have already renamed this file to boot.rc If not, see [[RPi_Advanced_Setup]]). For example:<br />
sudo nano /boot/boot.rc<br />
<br />
Add the following at the end of the file, making changes to the path for your directory tree and save.<br />
#Script to email ip address upon reboot<br />
python /home/pi/Code/startup_mailer.py<br />
<br />
== Alternative if using Rasbian ==<br />
If you are using Rasbian you won't have a /boot/boot.rc file. Instead you can edit /etc/rc.local as follows:<br />
<br />
sudo nano /etc/rc.local<br />
<br />
Add the python line so the file now looks like this:<br />
<br />
# rc.local<br />
#<br />
# This script is executed at the end of each multiuser runlevel.<br />
# Make sure that the script will "exit 0" on success or any other<br />
# value on error.<br />
#<br />
# In order to enable or disable this script just change the execution<br />
# bits.<br />
#<br />
# By default this script does nothing.<br />
# Print the IP address<br />
_IP=$(hostname -I) || true<br />
if [ "$_IP" ]; then<br />
printf "My IP address is %s\n" "$_IP"<br />
python /home/pi/Code/startup_mailer.py<br />
fi<br />
exit 0<br />
<br />
==Finish up==<br />
Reboot your Pi and you should receive an email with your ip address<br />
<br />
== Trouble shooting ==<br />
If you don't get email when rebooting, you have to check the hostname you currently have. because the script calls for raspberrypi. Just type in the command line # hostname. if you have an other hostname its simple just change you hostname with your preferred editor.<br />
<br />
msg['Subject'] = 'IP For YOUR HOSTNAME on %s' % today.strftime('%b %d %Y')</div>Kenwaldekhttps://elinux.org/index.php?title=RPi_Email_IP_On_Boot_Debian&diff=309848RPi Email IP On Boot Debian2013-12-23T20:48:01Z<p>Kenwaldek: if the hostname on your rpi is different you have to change some code in the script</p>
<hr />
<div>[[Category:RaspberryPi]]<br />
Back to [[RPi Guides]].<br />
<br />
'''Send Email Containing IP Address On Boot'''<br />
<br />
=What does it do?=<br />
<br />
This code will extract the ip address of your Pi and then send an email containing the ip to the specified email address. This is inspired by the need to access the Pi via SSH or other network protocols without a monitor and moving from network to network. This assumes a Gmail SMTP server. You may need to alter a bit for other servers (beyond scope here)<br />
<br />
=What do you need?=<br />
<br />
A working, and network enabled Raspberry Pi<br />
<br />
=What skill level is required?=<br />
<br />
Medium Level. You should be comfortable navigating a linux system and be comfortable using sudo (if you want to use this script, odds are you are quite comfortable at the command prompt).<br />
<br />
=Overview of this guide=<br />
You need to<br />
*Create a python script and store it in a Directory<br />
*Make python script executable<br />
*Edit '''''/boot/boot.rc'''''<br />
<br />
=Let's Do It=<br />
<br />
==Create the python script==<br />
Copy and paste the following code into a text editor (I'm a nano man myself)<br />
<br />
import subprocess<br />
import smtplib<br />
import socket<br />
from email.mime.text import MIMEText<br />
import datetime<br />
# Change to your own account information<br />
to = 'me@example.com'<br />
gmail_user = 'test@gmail.com'<br />
gmail_password = 'yourpassword'<br />
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)<br />
smtpserver.ehlo()<br />
smtpserver.starttls()<br />
smtpserver.ehlo<br />
smtpserver.login(gmail_user, gmail_password)<br />
today = datetime.date.today()<br />
# Very Linux Specific<br />
arg='ip route list'<br />
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)<br />
data = p.communicate()<br />
split_data = data[0].split()<br />
ipaddr = split_data[split_data.index('src')+1]<br />
my_ip = 'Your ip is %s' % ipaddr<br />
msg = MIMEText(my_ip)<br />
msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y')<br />
msg['From'] = gmail_user<br />
msg['To'] = to<br />
smtpserver.sendmail(gmail_user, [to], msg.as_string())<br />
smtpserver.quit()<br />
<br />
Save this script using a nice name like ''''startup_mailer.py'''' and make note of its path (like '''''/home/pi/Code/startup_mailer.py''''')<br />
<br />
For good measure, make the script executable<br />
sudo chmod +x startup_mailer.py<br />
<br />
==Edit /boot/boot.rc==<br />
Using your text editor once again, edit '''''/boot/boot.rc''''' (this assumes you have already renamed this file to boot.rc If not, see [[RPi_Advanced_Setup]]). For example:<br />
sudo nano /boot/boot.rc<br />
<br />
Add the following at the end of the file, making changes to the path for your directory tree and save.<br />
#Script to email ip address upon reboot<br />
python /home/pi/Code/startup_mailer.py<br />
<br />
== Alternative if using Rasbian ==<br />
If you are using Rasbian you won't have a /boot/boot.rc file. Instead you can edit /etc/rc.local as follows:<br />
<br />
sudo nano /etc/rc.local<br />
<br />
Add the python line so the file now looks like this:<br />
<br />
# rc.local<br />
#<br />
# This script is executed at the end of each multiuser runlevel.<br />
# Make sure that the script will "exit 0" on success or any other<br />
# value on error.<br />
#<br />
# In order to enable or disable this script just change the execution<br />
# bits.<br />
#<br />
# By default this script does nothing.<br />
# Print the IP address<br />
_IP=$(hostname -I) || true<br />
if [ "$_IP" ]; then<br />
printf "My IP address is %s\n" "$_IP"<br />
python /home/pi/Code/startup_mailer.py<br />
fi<br />
exit 0<br />
<br />
==Finish up==<br />
Reboot your Pi and you should receive an email with your ip address<br />
<br />
== Trouble shooting ==<br />
# if you don't get email when rebooting, you have to check the hostname you currently have. because the script calls for raspberrypi. Just type in the command line # hostname. if you have an other hostname its simple just change...<br />
<br />
msg['Subject'] = 'IP For YOUR HOSTNAME on %s' % today.strftime('%b %d %Y')<br />
<br />
with your preferred editor mine is nano to.</div>Kenwaldek