FR:RPi Email IP On Boot Debian

From eLinux.org
Revision as of 06:23, 1 June 2013 by Xavier (talk | contribs) (Édition de /boot/boot.rc)
Jump to: navigation, search

Retour vers les Guides RPi.

Envoi au démarrage d'un courriel contenant l'adresse IP

Qu'est-ce-que ça fait ?

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)

De quoi avez-vous besoin ?

Un Raspberry Pi fonctionnel, avec accès réseau

Quelles compétences sont requises ?

Niveau moyen. Vous devez être à l'aise pour naviguer sur un système linux et aussi pour utiliser sudo (si vous voulez utiliser ce script, il y a des chances que vous ayez une petite expérience de l'invite de commande).

Aperçu de ce guide

Vous devez

  • Créer un script python et l'enregistrer dans un répertoire
  • Rendre le script python exécutable
  • Éditer /boot/boot.rc

Allons-y

Création du script python

Copiez et collez le code suivant dans un éditeur de texte (je suis moi-même adepte de nano)

import subprocess
import smtplib
import socket
from email.mime.text import MIMEText
import datetime
# À remplacer par vos propres informations de compte
to = 'moi@exemple.com'
gmail_user = 'test@gmail.com'
gmail_password = 'votremotdepasse'
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_password)
today = datetime.date.today()
# Très spécifique à Linux
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 = 'Votre ip est %s' %  ipaddr
msg = MIMEText(my_ip)
msg['Subject'] = 'IP du RaspberryPi à %s' % today.strftime('%b %d %Y')
msg['From'] = gmail_user
msg['To'] = to
smtpserver.sendmail(gmail_user, [to], msg.as_string())
smtpserver.quit()

Enregistrez le script sous un nom parlant tel que 'startup_mailer.py' et prenez note de son chemin (comme /home/pi/Code/startup_mailer.py)

Pour faire bonne mesure, rendez-le exécutable

sudo chmod +x startup_mailer.py

Édition de /boot/boot.rc

En utilisant encore une fois votre éditeur de texte, ouvrez /boot/boot.rc (cela suppose que vous avez déjà renommé ce fichier en boot.rc Sinon, voir RPi_Advanced_Setup). Par exemple :

sudo nano /boot/boot.rc

Ajoutez les lignes suivantes en fin de fichier, en faisant la modification avec le chemin de votre répertoire et enregistrez.

#Script pour envoyer l'adresse ip par courriel au redémarrage
python /home/pi/Code/startup_mailer.py

Alternative si Rasbian est utilisée

If you are using Rasbian you won't have a /boot/boot.rc file. Instead you can edit /etc/rc.local as follows:

 sudo nano /etc/rc.local

Add the python line so the file now looks like this:

 # rc.local
 #
 # This script is executed at the end of each multiuser runlevel.
 # Make sure that the script will "exit 0" on success or any other
 # value on error.
 #
 # In order to enable or disable this script just change the execution
 # bits.
 #
 # By default this script does nothing.
 # Print the IP address
 _IP=$(hostname -I) || true
 if [ "$_IP" ]; then
   printf "My IP address is %s\n" "$_IP"
   python /home/pi/Code/startup_mailer.py
 fi
 exit 0

Pour finir

Redémarrez votre Pi et vous devriez recevoir un courriel avec votre adresse ip