#!/bin/bash
#
# /etc/rc.d/rc.mailMyIp
#
# start/check the mailMyIp daemon
#
# To make mailMyIp start automatically at boot, make this
# file executable: chmod 755 /etc/rc.d/rc.mailMyIp
# and add this lines to /etc/rc.d/rc.local
#
# if [ -x /etc/rc.d/rc.mailMyIp ]; then
# . /etc/rc.d/rc.mailMyIp start
# fi
#
# Written by movebo@linux.ime.usp.br and tested on Slackware 13.1
#
###############
# DEFINITIONS #
###############
FILE=/tmp/.mailMyIP
LOG=/var/log/mailMyIp.log
DATE=`date +"%F %H:%M:%S"`
ADDRS="zeh@gmail.com"
ADDRS2="destino@gmail.com"
MSMTP=`which msmtp`
MSMTPRC=/home/zeh/.msmtprc
PING=`which ping`
SED=`which sed`
PRINTF=`which printf`
IFCONFIG=`which ifconfig`
NAVI_IP=/home/zeh/bin/naviip
####################
# FUNCTIONS: BEGIN #
####################
MSG() {
BODY="Subject: NAVI has booted, check your IP\nDate: $DATE\n\nNAVI was b
ooted at $DATE\nNAVI IP=$IP\n"
$PRINTF "%b" "$BODY" > $FILE
$PRINTF "%b" "$BODY" | $MSMTP -C $MSMTPRC $ADDRS
$PRINTF "%b" "NAVI has booted at $DATE with IP: $IP\n" >> $LOG
}
mailMyIp() {
if [ -f $FILE ]; then
OLDIP=`cat $FILE | sed '/IP=/!d;s/.*=//g'`
[ "$IP" != "$OLDIP" ] && MSG
else
MSG
fi
}
OK() {
echo -e "\\033[60G[ \e[32;1mOK\e[0m ]"
}
FAIL() {
echo -e "\\033[60G[ \e[31;1mFAIL\e[0m ]"
}
GET_IP() {
IP=`curl -s http://www.whatismyip.com/automation/n09230945.asp`
echo -n $IP
}
TEST_INET() {
ETH0=`$IFCONFIG eth0 | sed '/inet\ /!d;s/.*r://g;s/\..*$//g'`
[ -z "$ETH0" ] && ETH=`$IFCONFIG eth1 | sed '/inet\ /!d;s/.*r://g;s/\..*
$//g'` || ETH=$ETH0
if [ $ETH -ne 192 ]; then
echo "Network not available"
exit -1
else
printf "%b" "Testing your Internet Connection:"
PING_COUNT=`$PING -l3 -w1 -c3 200.160.4.2 2> /dev/null | $SED '/
rec/!d;s/.*ted,\ //g;s/\ .*//g'`
fi
}
##################
# FUNCTIONS: END #
##################
case "$1" in
'start')
TEST_INET
if [ $PING_COUNT -eq 0 ]; then
FAIL
else
OK
echo -n " Retrieving external IP: "
GET_IP
if [ -n "$IP" ]; then
OK
echo -en " Retrieving IP from gmail: "
if [ -n "$NAVI_IP" ]; then
echo -n "$NAVI_IP"
OK
else
FAIL
fi
if [ "$IP" != "$NAVI_IP" ]; then
echo -en " Mailing the new IP to $ADDRS"
MSG
OK
else
echo " Nothing to do."
fi
else
FAIL
fi
fi
;;
'check')
TEST_INET > /dev/null
if [ $PING_COUNT -ne 0 ]; then
GET_IP >> /dev/null
if [ -n "$IP" ]; then
[ "$IP" != "$NAVI_IP" ] && MSG
fi
fi
;;
*)
echo "usage $0 [start|check]"
esac