#! /bin/sh
# ipcheck.sh - Version 1.1
# Recherche de l'ip publique actuelle et utilisation pour la retrouver sur le net
# Synopsis:
# 1/ reherche d'une ip sur internet grâce à la commande route
# -> si déjà présente: suite
# -> si nonprésente: on force une deconnexion puis une reconnexion
# 2/ recherche du numéro ip
# 3/ test l'ip actuelle avec celle sauvegardée plus tot
# 4/ creation d'un fichier temporaire si ip différente
# 5/ création d'une page html avec l'ip publique
# 6/ copie sur le serveur local
# 7/ copie sur le serveur distant
# 8/ exit!
# ---------------------------------------------------------------------------
# Par R. Suinot (rsuinux <at> gmx.fr)
# version: 26 sep. 2005
# Rev°: 09 nov. 2005
# ---------------------------------------------------------------------------
# mise en place des variables locales
page_html="/tmp/index.html"
copie="/home/http/www/adresse-perso.html"
copie_ip="/home/http/www/ip"
rep_site="/rep_perso/"
adresse_ftp="ftpperso.free.fr"
username="rsuinux"
password="xxxxxxx"
DATE="00"
# Interface a surveiller
INTERFACE=eth0
# personne a qui le mail d'information doit être envoye.
# N'est peut être pas utile si vous passez par cron
INFORMED="root"
# sauvegarde en fichier temporaire de l'adresse en cours
FILE_REGISTERED_IP="/tmp/registered_ip"
# On commence par vérifier si eth0 est configuré
if ! `route -n | grep $INTERFACE > /dev/null` ; then
sudo ifdown eth0
rm $FILE_REGISTERED_IP
sleep 10
sudo ifup eth0
fi
# on récupère l'adresse ip actuelle de $INTERFACE
CURRENT_IP=`/sbin/ifconfig $INTERFACE | grep inet | cut -d ":" -f 2 | cut -d " " -f 1`
# on recherche l'adresse stoké dans $FILE_REGISTERED_IP
# ou on place 00.00.00.00 si le fichier n'existe pas
if test -f $FILE_REGISTERED_IP ; then
REGISTERED_IP=`cat $FILE_REGISTERED_IP`
else
REGISTERED_IP="00.00.00.00"
fi
# sortie ecran pour debugage
# echo "Current IP: $CURRENT_IP."
# echo "Registered IP: $REGISTERED_IP."
# Compare les ip afin de determiner s'il faut modifier ou non,
# l'adresse externe du serveur sur mon site.
if [ $CURRENT_IP != $REGISTERED_IP ] ; then
# Adresse ip modifiée. Création du nouveau référentielle
echo "Nouvelle adresse IP: $CURRENT_IP" | mail $INFORMED -s "Adresse ip de $HOSTNAME modifiée"
`echo "$CURRENT_IP" > $FILE_REGISTERED_IP`
DATE=date
# ouverture d'un fichier temporaire pour la creation de la page web
echo "<HTML>" > $page_html
echo "<HEAD>" >> $page_html
echo " <TITLE>Adresse perso</TITLE>" >> $page_html
echo " <meta http-equiv=\"refresh\" content=\"10; URL=http://$CURRENT_IP\">" >> $page_html
echo " <style type=\"text/css\">" >> $page_html
echo " <!--" >> $page_html
echo " body {background-color: #ffffff; color: #000000;} " >> $page_html
echo " body, td, th, h1, h2 {font-family: sans-serif;} " >> $page_html
echo " a:link {color: #000099; text-decoration: none; background-color: #ffffff;} " >> $page_html
echo " a:hover {text-decoration: underline; color: red;} " >> $page_html
echo " table {border-collapse: collapse;} " >> $page_html
echo " .center {text-align: center;} " >> $page_html
echo " .center table { margin-left: auto; margin-right: auto; text-align: left;} " >> $page_html
echo " .center th { text-align: center !important; } " >> $page_html
echo " td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;} " >> $page_html
echo " h1 {font-size: 150%;} " >> $page_html
echo " h2 {font-size: 175%;} " >> $page_html
echo " .p {text-align: left;} " >> $page_html
echo " i {color: #666666; background-color: #cccccc;} " >> $page_html
echo " hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;} " >> $page_html
echo " -->" >> $page_html
echo " </style>" >> $page_html
echo "</HEAD>" >> $page_html
echo " <body text=\"black\" link=\"red\" >" >> $page_html
echo "<table bgcolor=\"#B3F6F2\" width=\"100%\" cellspacing=0 cellpadding=0 border=0>" >> $page_html
echo " <tr>" >> $page_html
echo " <td>" >> $page_html
echo " <div align=\"center\"><h2><font color=\"#2200CC\">Information</font></h2></div>" >> $page_html
echo " </td>" >> $page_html
echo " </tr>" >> $page_html
echo "</table>" >> $page_html
echo "<br>" >> $page_html
echo " <font face=\"Lucida\" size=+1>" >> $page_html
echo " Valeurs actuelles du serveur:<br>" >> $page_html
echo "<ul>" >> $page_html
echo "<li>Adresse publique: <a href=\"http://$CURRENT_IP\">$CURRENT_IP</a><br></li>" >> $page_html
echo "</ul>" >> $page_html
echo "<P>" >> $page_html
echo "Vous allez être redirigé dans 10 secondes." >> $page_html
# echo "Depuis $DATE" >> $page_html
# echo "Depuis le $Jour{$date[0]} $date[2] $Mois{$date[1]} $date[4], à $date[3]" >> $page_html
echo "</P>" >> $page_html
echo "<br><hr><br>" >> $page_html
echo "<a href=\"http://rsuinux.free.fr\">Retour vers la page d'accueil du site</a>" >> $page_html
# et on l'envoi en local!
sudo cp $page_html $copie
sudo cp $FILE_REGISTERED_IP $copie_ip
sudo chown www-data.www-data $copie
# le fichier est créé, à pésent, on l'envoi sur le site
ncftpput -u $username -p $password $adresse_ftp $rep_site $page_html 2> /dev/null
# else
# echo "Adresse ip non modifiée. Référentielle maintenu."
fi