@ -19,7 +19,7 @@
###########################################################
# A script for remote server environments which are behind
# dynamic (non-static) DHCP. Usually these dynamic IPs are
# dynamic (non-static) DHCP. Usually these dynamic IPs are
# used in common household networks in non-corporate
# environments.
@ -44,95 +44,86 @@
# Some lines below are commented out because the timer is handled by systemd service file
# If you don't use provided systemd service file, re-enable the relevant lines below
function checkWANIP( ) {
function checkWANIP {
# Command to resolve the current IPv4 WAN address
local WANIP_CURRENT = "dig +short myip.opendns.com @resolver1.opendns.com"
# Log file timestamp format
local TIMESTAMP = $( date '+%d-%m-%Y, %X' )
############################
# Email sender
local EMAIL_SENDER = "mailsender@foo.com"
# Emails to send notification to
local EMAIL_RECIPIENTS = (
"whogetsthemail_1@foo.com"
"whogetsthemail_2@bar.com"
)
############################
# Email send function
function mailSend {
echo -e " To: ${ 1 } \nFrom: ${ EMAIL_SENDER } \nSubject: ${ SUBJECT_EMAIL } \n\n ${ MESSAGE_EMAIL } " | sendmail -v " ${ 1 } "
}
############################
# If we are connected to internet...
# There's no point to do WAN IP check if we can't establish connection to WAN/Internet at all
# In addition, do not generate any network related variables if the connection
# can't be established. Therefore, include variable defitions inside this if statement.
if [ [ $( printf $( eval ${ WANIP_CURRENT } & > /dev/null) $? ) -eq 0 ] ] ; then
# Check interval in minutes
# local CHECK_INTERVAL=5
if [ [ $( printf $( eval " ${ WANIP_CURRENT } " & > /dev/null) $? ) -eq 0 ] ] ; then
############################
# Cache/Log directory of the script
local WANIP_DIR = " $HOME "
# Log file for checked/resolved IPv4 WAN addresses:
# Log file for checked/resolved IPv4 WAN addresses
local WANIP_LOG = " $WANIP_DIR /.ip_wan.log "
if [ [ ! -d ${ WANIP_DIR } ] ] ; then
mkdir -p ${ WANIP_DIR }
if [ [ ! -d " ${ WANIP_DIR } " ] ] ; then
mkdir -p " ${ WANIP_DIR } "
fi
if [ [ ! -f ${ WANIP_LOG } ] ] ; then
printf 'Time\t\t\t\tWAN IPv4\n' > ${ WANIP_LOG }
if [ [ ! -f " ${ WANIP_LOG } " ] ] ; then
printf 'Time\t\t\t\tWAN IPv4\n' > " ${ WANIP_LOG } "
fi
############################
# Log file timestamp format
local TIMESTAMP = $( date '+%d-%m-%Y, %X' )
############################
# Email to send notify to
local EMAIL_RECIPIENT = "mymail@hotmail.com"
# Email subject/title
local SUBJECT_EMAIL = " WAN IP address change (Helsinki, $( tail -1 ${ WANIP_LOG } | awk '{print $NF}' ) -> $( eval ${ WANIP_CURRENT } ) ) "
local SUBJECT_EMAIL = " WAN IP address changed (Helsinki, $( tail -1 ${ WANIP_LOG } | awk '{print $NF}' ) -> $( eval ${ WANIP_CURRENT } ) ) "
# Email message/body contents
local MESSAGE_EMAIL = " ${ TIMESTAMP } : WAN address of the server (Helsinki) has been changed from $( tail -1 ${ WANIP_LOG } | awk '{print $NF}' ) to $( eval ${ WANIP_CURRENT } ) "
local MESSAGE_EMAIL = " ${ TIMESTAMP } : WAN address of location (Helsinki) has been changed from $( tail -1 ${ WANIP_LOG } | awk '{print $NF}' ) to $( eval ${ WANIP_CURRENT } ) "
# Message to server stdout
local MESSAGE_STDOUT = " $( echo ${ TIMESTAMP } ) - WAN address of this server has been changed from $( tail -1 ${ WANIP_LOG } | awk '{print $NF}' ) to $( eval ${ WANIP_CURRENT } ) "
############################
# Email send command
local MAIL_SEND = " echo -e \"To: ${ EMAIL_RECIPIENT } \nFrom: ${ EMAIL_RECIPIENT } \nSubject: ${ SUBJECT_EMAIL } \n\n ${ MESSAGE_EMAIL } \" | sendmail -v ${ EMAIL_RECIPIENT } "
# Log write command
local LOG_WRITE = " printf '%s %s\t\t%s\n' $( echo $TIMESTAMP ) $( eval $WANIP_CURRENT ) >> $WANIP_LOG "
local LOG_WRITE = $( printf '%s %s\t\t%s\n' $( echo " ${ TIMESTAMP } " ) $( eval " ${ WANIP_CURRENT } " ) >> " ${ WANIP_LOG } " )
############################
# If the log file has no previous IPv4 entries
if [ [ $( cat $WANIP_LOG | wc -l) -le 1 ] ] ; then
eval ${ LOG_WRITE }
fi
# local i=0
# while true; do
if [ [ $( tail -1 " ${ WANIP_LOG } " | awk '{print $NF}' ) != $( printf '%s' $( eval " ${ WANIP_CURRENT } " ) ) ] ] || \
[ [ $( cat " ${ WANIP_LOG } " | wc -l) -le 2 ] ] ; then
# if [[ $i -ne 0 ]]; then
# sleep $(( ${CHECK_INTERVAL} * 60 ))
# fi
echo -e " ${ MESSAGE_STDOUT } "
# if [[ -f $WANIP_LOG ]]; then
for i in " ${ EMAIL_RECIPIENTS [@] } " ; do
mailSend " ${ i } "
$LOG_WRITE
done
# The log file must include more than just the header line
if [ [ $( cat $WANIP_LOG | wc -l) -gt 1 ] ] ; then
if [ [ $( tail -1 $WANIP_LOG | awk '{print $NF}' ) != $( printf '%s' $( eval $WANIP_CURRENT ) ) ] ] ; then
echo -e ${ MESSAGE_STDOUT }
eval ${ MAIL_SEND }
eval ${ LOG_WRITE }
fi
fi
fi
# let i++
# done
}
############################