#!/bin/sh

# check_ip.sh
#
# Script designed to be run as a cron job. Each time this script runs, it:
#
# - Checks to see if the IP address of the host (as seen by the
# Internet) has changed.
#
# - Updates the ip_history.tsv table if a change is detected.
#
# - Posts a HTML version back to the Internet (if a ${LDIR}/.ncftpput
# config file is found).
#
# To make this script work:
#
# - Create a ${LDIR}/.ncftpput config file (if you want results posted
# back to a web server). Don't forget to set the permissions to
# 600. Contents of file will have three lines in the form:
#
# host MYFTPSERVER
# user MYID
# pass MYPASS
#
# - Optionally update the post_ip() function to construct the HTML
# page the way you want.
#
# - Update the following environment variables to your liking:
#
# LDIR      - Where to create local files after each check
# FTP_DIR   - Directory to FTP results to
# TAG       - Name to identify this system by (no spaces)
# POST_ON_CHANGE - Set to true if you only want to post updates, set to false
#                  to always post results (allows you to see time changes)

LDIR="${PWD}"
FTP_DIR=/web/info/ip
NAME=home
POST_ON_CHANGE=false

#
# You should NOT need to adjust these
#

CUR_IP_FILE="${LDIR}/current_ip.txt"
LAST_IP_FILE="${LDIR}/last_ip.txt"
IP_HISTORY_FILE="${LDIR}/ip_history.tsv"
HTML_FILE="${LDIR}/${NAME}.html"
NEED_POST=false
 
#
# Sets the environment variable TIME_STAMP to:
#
#   yyyy-MM-dd HH:mm:ss
#
# Only does this one time (subsequent invocations are ignored)
 
set_time_stamp() {
  if [ "$TIME_STAMP" = "" ]; then
    TIME_STAMP="$(date +'%Y-%m-%d %H:%M:%S')"
  fi
}

#
# Get current IP of home LAN
#
#   Returns 0 and updates ${CUR_IP_FILE} on success, returns 1 on failure

get_ip() {
   if ( wget -O "${CUR_IP_FILE}" http://nst.sourceforge.net/nst/tools/ip.php > /dev/null 2>&1 ); then
     if ( grep '^[0-9]*.[0-9]*.[0-9]*.[0-9]*$' "${CUR_IP_FILE}" 2>&1 > /dev/null); then
       return 0
     fi
   fi
   return 1
}

# check_ip
#
#   check for change in IP address (returns 0 if check was successful)

check_ip() {
  set_time_stamp

  if ! ( get_ip ); then
    return 1;
  fi

  if [ -f "$LAST_IP_FILE" ]; then
    if ( diff "${LAST_IP_FILE}" "${CUR_IP_FILE}" > /dev/null 2>&1 ); then
      return 0;
    fi
  fi

  NEED_POST=true;
  cp "${CUR_IP_FILE}" "${LAST_IP_FILE}"
  if [ ! -f "${IP_HISTORY_FILE}" ]; then
    printf "IP\tDATE\n" > "${IP_HISTORY_FILE}"
  fi
  REMOTE_ADDR="$(cat "${CUR_IP_FILE}")"
  printf "${REMOTE_ADDR}\t${TIME_STAMP}\n" >> "${IP_HISTORY_FILE}"
}

#
# Post information on-line
#

post_ip() {
  set_time_stamp

  cat > ${HTML_FILE} <<EOF
<html><head><title>Remote Access To: ${NAME}</title></head>
<link href="http://www.networksecuritytoolkit.org/nst/css/site.css" rel="stylesheet" type="text/css"/>
<body>

<h1>Remote Access To: ${NAME}</h1>

<p>As of <b>${TIME_STAMP}</b> remote access can be found at
<b>${REMOTE_ADDR}</b>. Current services available include:</p>

<table border="1">

<tr><th>IP</th><th>Port</th><th>Description</th></tr>

<tr><td>${REMOTE_ADDR}</td><td>20022</td><td>Secure shell access to
${NAME}.</td></tr>

</table>

<h1>IP History</h1>

<p>The following shows the history of IP address changes since we
started running this script.</p>

<table border="1" bgcolor="white">
$(awk '-F\t' -- '{ if ( FNR == 1 ) printf("<tr><th>%s</th><th>%s</th></tr>",$1,$2); else printf("<tr><td>%s</td><td>%s</td></tr>",$1,$2); }' < ${IP_HISTORY_FILE})
</table>

</body></html>
EOF

  if [ -f "${LDIR}/.ncftpput" ]; then
    ncftpput -f ${LDIR}/.ncftpput ${FTP_DIR} ${HTML_FILE} >/dev/null 2>&1
  fi
}

check_ip

if [ "${POST_ON_CHANGE}" != "true" -o "${NEED_POST}" = "true" ]; then
  REMOTE_ADDR="$(cat "${CUR_IP_FILE}")"
  post_ip
fi
