Created
February 5, 2012 11:48
-
-
Save AlexanderPavlenko/1744962 to your computer and use it in GitHub Desktop.
Monitoring IP changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### IPdetect configuration file | |
### (c) Mariusz Kaczmarczyk, 2005+ | |
### Content of this file is subject to GPL version 2 license | |
### (http://www.gnu.org/licenses/gpl.html). | |
### This is shell include file, use rules for shell script files. | |
### Values should be VARIABLE='value' (no spaces around equal sign!). | |
### See 'man 1 bash' for details on how to write shell scripts. | |
# Force PATH to this value. For security reasons, do not change unless you | |
# really need to. | |
PATH='/sbin:/bin:/usr/sbin:/usr/bin' | |
# This sets the method of IP comparision, may be 'host' or 'last'. | |
# The first means comparing detected public IP to one resolved from | |
# CHECK_DOMAIN. You need to have 'host' util (from 'host' or 'dnsutils' | |
# package). | |
# The later means comparing detected IP with previously detected one. | |
# Doesn't need external programs. | |
# It's recommended to use 'last' for every minute check, and 'host' once per | |
# 15 minutes. Command line switch -p overrides this value. | |
IP_COMPARE_METHOD='last' | |
# Resolve this hostname, check its IP, if differs from detected one run | |
# script. Used when IP_COMPARE_METHOD is 'host' or -p switch is used. Can be | |
# overriden with -d switch. | |
CHECK_DOMAIN='alerticus.ru' | |
# This sets the method of retrieving page with current IP, may be 'browser' | |
# and, since ver. 0.6 'iface'. The first method uses HTTP to read external | |
# information page, as configured in ip_source file. The second gets the IP | |
# number of chosen interface. 'iface' is faster and so recommended choice | |
# when the program is run on the Linux router with public IP number on any | |
# interface. | |
# 'iface' method needs 'ifconfig' program in PATH. There should be support for | |
# 'iproute2' program instead if ifconfig in future versions of IPdetect. | |
# You can override this setting on command line with -r switch. | |
IP_RETRIEVE_METHOD='browser' | |
# If IP_RETRIEVE_METHOD = 'ifconfig', then set this to interface name to get | |
# IP number from. | |
# If you set this, and IP_RETRIEVE_METHOD is not 'iface', then iface is | |
# tried first, and if no address is supplied then selected method is used as | |
# fallback. | |
# Can be overriden with -i switch (use NULL special value or '' to disable | |
# this setting from command line). | |
# IP_INTERFACE='ppp0' | |
# Minimum IP check interval in seconds. Protects from abuses. | |
# Recommended value for most services: 55 | |
CHECK_IP_INTERVAL=55 | |
# Minimum script run interval in seconds. Protects from abuses. | |
# Recommended value depends on rules of your dynamic DNS provider. | |
# This is only used when problems with detecting right IP occurs. | |
UPDATE_IP_INTERVAL=290 | |
# Turn on/off verbose output. May be 0 or 1. | |
# Recommended value is 0 for security reasons. | |
DEBUG_FLAG=0 | |
# Events sent to log file. | |
# Can be combination of: 'change', 'nochange', 'error', 'forced'. | |
# Separate multiple entries with space, quote entire string if there's more | |
# than one entry. | |
LOG_EVENTS='change error forced'; | |
# Whether to store or not failed IP update time (when update script returned | |
# non-zero exit code). May be 0 or 1. If not stored, IP update will probably | |
# be retried on next run of IPdetect. If stored, IP update will not be | |
# retried before end of update interval. | |
# You should know what you're doing if setting this to 0 (may then cause | |
# abusive use of dynamic DNS service). | |
STORE_FAILED_UPDATE=1 | |
# IP retrieve timeout in seconds. Applied for browser program (depending on | |
# IP_RETRIEVE_METHOD and USE_BROWSERS). Default and recommended value is 12. | |
# IP_RETRIEVE_TIMEOUT=12 | |
# Try to use HTTP browser in order below to retrieve IP in 'browser' mode. | |
# Supported programs are: curl, wget, links2, lynx. Separate multiple names with | |
# space. Programs are tried in given order. | |
# It's recommended to leave it undefined and use script's internal default list. | |
# USE_BROWSERS='curl wget links2 lynx' | |
# Max time for 'host' command to wait for response. After this timeout DNS | |
# query is considered lost and domain is considered unresolvable. This | |
# should be no more than 20 seconds if you run IPdetect once a minute, and | |
# no more than half of CHECK_IP_INTERVAL if you run less often. Default and | |
# recommended is 8. | |
# HOST_TIMEOUT=8 | |
# Use this server to query for domains specified with CHECK_DOMAIN. | |
# Only used when IP_COMPARE_METHOD is 'host'. | |
# Hint: this is used as the DNS server parameter for 'host' command. | |
# Setting this is only recommended when you get right IP very much late | |
# after domain update (this problem can cause update script being run | |
# multiple times for the same event and may be treated as net abuse by some | |
# services! | |
# Remember to examine your logs after first updates). | |
# Use -n switch to override server address. | |
HOST_DNS_SERVER='ns1.ypdns.com' | |
# You can set user agent string for browser programs. This may be necessary | |
# for some sites providing IP address. | |
# Program's default is used when not set. | |
# DETECTION_USER_AGENT='' | |
# Run this script when IP change is detected | |
# Script gets 2 parameters: #1 is net IP address, #2 is CHECK_DOMAIN value. | |
# Remember to set secure enough permission to this file, as it probably will | |
# contain passwords or secret URLs (recommended: chmod 700 file_path). | |
# Use -s switch to override script location. | |
IP_CHANGE_EXEC='/usr/local/etc/ipdetect/change_run.sh' | |
# Send programs output to this file, instead of just loosing it. Sometimes | |
# useful for debuging. Disable in production environment. | |
# DEV_NULL='' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*/2 * * * * root /usr/local/bin/ipdetect.sh | |
*/15 * * * * root /usr/local/bin/ipdetect.sh -p host -r browser -c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Place your domain update commands here | |
# IP address, the second is CHECK_DOMAIN value from IPdetect.conf | |
# Ie. http://freedns.afraid.org/ gives you update URLs for dynamic | |
# domains, so you can put them into command: | |
# curl --silent <update_URL> | |
# This is tested and works very good. | |
# This script is run by IPdetect with 2 parameters: the first is the new | |
# IP address, the second is CHECK_DOMAIN value (from config file or -d) | |
# Check http://sf.net/docman/display_doc.php?docid=30187&group_id=147527 | |
# for update code for various free dynamic DNS services. | |
# curl --silent http://freedns.afraid.org/dynamic/update.php?... | |
/usr/local/etc/ipdetect/ypdns.php $1 | |
exit 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Place your IP source pages here (full address with http://) | |
# Sources are checked in given order | |
http://checkip.dyndns.org/ | |
http://ipdetect.dnspark.com:8888/ | |
# http://www.ipchicken.com/ | |
# http://www.ip-number.com/index.asp | |
# http://www.lawrencegoetz.com/programs/ipinfo/ | |
# http://www.cloudnet.com/support/getting_Connected/system.php | |
# http://www.mediacollege.com/internet/utilities/show-ip.shtml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
### Content of this file is a subject to GPL version 2 license | |
### (http://www.gnu.org/licenses/gpl.html). | |
# help message | |
MSG_HELP=" | |
IPdetect, ver. 0.6, (c) Mariusz Kaczmarczyk <koshmar@poczta.fm>. | |
IPv4 detection script for updating dynamic DNS services. | |
Free software. GPL version 2 license. | |
Usage: | |
`basename $0` [OPTIONS] | |
Options: | |
-h|--help this help dialog | |
-f|--force-update force IP update now | |
-c|--force-check force IP check now | |
-r|--retrieve-method brower|iface override IP retrieve method | |
-p|--compare-method host|last override IP compare method | |
-i|--interface-name interface override interface used | |
-d|--domain domain_name domain to check/update | |
-m|--messages-to file send subprograms output to file (debug) | |
-s|--update-script run this script on IP change | |
-n|--dns-server nameserver use this nameserver to resolve domain | |
There's no warranty of any kind for this software, you use it for your own | |
risk! See README for help. | |
"; | |
# Displays all commands executed | |
# Uncomment for maximum debug | |
# set -x; | |
# defaults | |
# do not alter these values, use configuration file instead | |
# (except for CONFIG_FILE of course :-)) | |
CONFIG_FILE='/usr/local/etc/ipdetect/ipdetect.conf'; | |
PATH='/sbin:/bin:/usr/sbin:/usr/bin'; | |
CHECK_IP_INTERVAL=55; | |
UPDATE_IP_INTERVAL=290; | |
STORE_FAILED_UPDATE=1; | |
IP_RETRIEVE_METHOD='wget'; | |
IP_COMPARE_METHOD='last'; | |
CHECK_DOMAIN=''; | |
DOMAIN_FILE='/usr/local/etc/ipdetect/check_domain'; | |
IP_RETRIEVE_TIMEOUT=20; | |
IP_REGEXP='[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'; | |
IP_CHANGE_EXEC='/usr/local/etc/ipdetect/change_run.sh'; | |
IP_SOURCES_FILE='/usr/local/etc/ipdetect/ip_sources'; | |
LOG_DATE_FORMAT='%F,%T'; | |
LOG_FILE='/var/log/ipdetect/ipdetect.log'; | |
LOG_EVENTS='change error'; | |
TMP_DIR="`dirname \`mktemp -u\``"; | |
STORE_DIR='/var/lib/ipdetect'; | |
IP_HTML_TMP_FILE='ipdetect-ip.tmp'; | |
STORE_FILE='last_detect'; | |
COMMENT_REGEX='^[:space:]*[#;]{1}.*'; | |
# HOST_EXEC="`which host`"; | |
HOST_TIMEOUT=8; | |
DEV_NULL='/dev/null'; | |
HOST_DNS_SERVER=''; | |
DEBUG_FLAG=0; | |
DETECTION_USER_AGENT=''; | |
IP_INTERFACE=''; | |
NULL_VALUE='NULL'; | |
USE_BROWSERS='curl wget links2 lynx'; | |
PRE_CHECK_INTERFACE=1; | |
# messages | |
MSG_1='ERROR: Invalid IP_RETRIEVE_METHOD, should be one of: browser, iface'; | |
MSG_2='failed'; | |
MSG_3='ERROR: Cannot retrieve valid IP'; | |
MSG_4='NOTICE: Not checking IP - you need to wait'; | |
MSG_5='more seconds'; | |
MSG_6='ERROR: Cannot resolve domain'; | |
MSG_7='ERROR: Cannot execute script'; | |
MSG_8='ERROR: Cannot load config file'; | |
MSG_9='ERROR: Cannot create store file'; | |
MSG_10="ERROR: CHECK_DOMAIN must be set when IP_COMPARE_METHOD = 'host'"; | |
MSG_11='ERROR: Temporary file not writable'; | |
MSG_12='NOTICE: Not updating IP - you need to wait'; | |
MSG_13="ERROR: you have to set IP_INTERFACE for IP_RETRIEVE_METHOD = 'iface'"; | |
MSG_14='ERROR: Unknown parameter'; | |
MSG_15='ERROR: Invalid IP_COMPARE_METHOD, should be one of: host, last'; | |
# error codes | |
ERR_OK_NOCHANGE=0; | |
ERR_OK_CHANGE=127; | |
ERR_NOCONFFILE=1; | |
ERR_BADSTOREPATH=2; | |
ERR_BADRETRMETHOD=3; | |
ERR_NOVALIDIP=4; | |
ERR_BADCOMPMETHOD=5; | |
ERR_HOSTRESOLVERR=6; | |
ERR_EXECFAIL=7; | |
ERR_BADDOMAINNAME=8; | |
ERR_NOIFACE=10; | |
ERR_BADPARAM=11; | |
# include config file | |
if [ ! -f "${CONFIG_FILE}" -o ! -r "${CONFIG_FILE}" ]; then | |
echo "${MSG_8}: ${CONFIG_FILE}"; | |
exit ${ERR_NOCONFFILE}; | |
fi; | |
. "${CONFIG_FILE}"; | |
# startup values | |
FORCE_UPDATE_FLAG=0; | |
FORCE_CHECK_FLAG=0; | |
EXEC_FLAG=0; | |
CHANGE_FLAG=0; | |
# check command line parameters | |
for X1 in `seq 1 $#`; do | |
case "$1" in | |
'-h'|'--help') | |
echo "${MSG_HELP}"; | |
exit ${ERR_OK_NOCHANGE}; | |
;; | |
'-f'|'--force-update') | |
FORCE_UPDATE_FLAG=1; | |
shift 1; | |
;; | |
'-c'|'--force-check') | |
FORCE_CHECK_FLAG=1; | |
shift 1; | |
;; | |
'-r'|'--retrieve-method') | |
IP_RETRIEVE_METHOD="$2"; | |
case "${IP_RETRIEVE_METHOD}" in | |
'browser') | |
PRE_CHECK_INTERFACE=0; | |
;; | |
esac; | |
shift 2; | |
;; | |
'-p'|'--compare-method') | |
IP_COMPARE_METHOD="$2"; | |
shift 2; | |
;; | |
'-i'|'--interface-name') | |
IP_INTERFACE="$2"; | |
shift 2; | |
;; | |
'-d'|'--domain') | |
CHECK_DOMAIN="$2"; | |
IP_COMPARE_METHOD='host'; | |
shift 2; | |
;; | |
'-m'|'--messages-to') | |
DEV_NULL="$2"; | |
shift 2; | |
;; | |
'-s'|'--update-script') | |
IP_CHANGE_EXEC="$2"; | |
shift 2; | |
;; | |
'-n'|'--dns-server') | |
HOST_DNS_SERVER="$2"; | |
IP_COMPARE_METHOD='host'; | |
shift 2; | |
;; | |
'') | |
# ignore | |
;; | |
*) | |
echo "${MSG_14} '$1'"; | |
exit ${ERR_BADPARAM}; | |
;; | |
esac; | |
done; | |
# prepare some essential variable values | |
IP_HTML_TMP_PATH="${TMP_DIR}/${IP_HTML_TMP_FILE}"; | |
STORE_PATH="${STORE_DIR}/${STORE_FILE}"; | |
S70_NOW="`date +%s`"; | |
# reset variables that have NULL special value | |
if [ "${IP_INTERFACE}" = "${NULL_VALUE}" ]; then | |
IP_INTERFACE=''; | |
fi; | |
if [ "${HOST_DNS_SERVER}" = "${NULL_VALUE}" ]; then | |
HOST_DNS_SERVER=''; | |
fi; | |
# define functions | |
debug_msg() { | |
if [ "${DEBUG_FLAG}" -gt 0 ]; then | |
echo "DEBUG: $1" >&2; | |
fi; | |
}; | |
log_output() { | |
LOG_EVENT_ID="$1"; | |
SCRIPT_EXIT_CODE="$2"; | |
touch "${LOG_FILE}"; | |
LOG_FLAG="`echo " ${LOG_EVENTS} " | grep -o -E " ${LOG_EVENT_ID} "`"; | |
if [ -z "${LOG_FLAG}" ]; then | |
return 1; | |
fi; | |
ITEM_1="-${LOG_EVENT_ID}-"; | |
ITEM_2="`date "+${LOG_DATE_FORMAT}"`"; | |
ITEM_3="${IP_FOUND_METHOD:=$NULL_VALUE}"; | |
ITEM_4="(${IP_SOURCE_NOW:=$NULL_VALUE})"; | |
ITEM_5="${IP_NOW:=$NULL_VALUE}"; | |
ITEM_6="${IP_COMPARE_METHOD:=$NULL_VALUE}"; | |
case "${IP_COMPARE_METHOD}" in | |
'last') | |
ITEM_7=''; | |
ITEM_8="${LAST_IP:=$NULL_VALUE}"; | |
;; | |
'host') | |
ITEM_7="(${CHECK_DOMAIN})"; | |
ITEM_8="${HOST_IP:=$NULL_VALUE}"; | |
;; | |
*) | |
return 1; | |
;; | |
esac; | |
ITEM_9="result:${SCRIPT_EXIT_CODE}"; | |
case "${LOG_EVENT_ID}" in | |
'change'|'forced') | |
ITEM_10="update:${UPDATE_SCRIPT_STATUS}"; | |
;; | |
'nochange'|'error') | |
ITEM_10="update:$NULL_VALUE"; | |
;; | |
*) | |
return 1; | |
;; | |
esac; | |
TMP_LOG_ITEM="${ITEM_1} ${ITEM_2} ${ITEM_3}${ITEM_4} ${ITEM_5} ${ITEM_6}${ITEM_7} ${ITEM_8} ${ITEM_9} ${ITEM_10}"; | |
echo ${TMP_LOG_ITEM} >>"${LOG_FILE}"; | |
return 0; | |
}; | |
ip_from_host() { | |
HOST_IP=''; | |
if [ -z "$1" ]; then | |
return 1; | |
fi; | |
HOST_CMD_OUT=`host -t A -W ${HOST_TIMEOUT} "$1" ${HOST_DNS_SERVER} 2>${DEV_NULL}`; | |
HOST_IP=`echo ${HOST_CMD_OUT} |grep -o -E ${IP_REGEXP} |tail -n 1`; | |
if [ -z "${HOST_IP}" ]; then | |
HOST_CMD_OUT=`host -t A -s ${HOST_TIMEOUT} "$1" ${HOST_DNS_SERVER} 2>"${DEV_NULL}"`; | |
HOST_IP=`echo ${HOST_CMD_OUT} |grep -o -E "${IP_REGEXP}" |tail -n 1`; | |
fi; | |
return 0; | |
}; | |
retr_ip_browser() { | |
if [ -z "$1" ]; then | |
return 1; | |
fi; | |
if [ -z "${USE_BROWSERS}" ]; then | |
return 2; | |
fi; | |
for X1 in ${USE_BROWSERS}; do | |
BR_PATH="`which $X1`"; | |
debug_msg "use_browser: $X1 -> '$BR_PATH'"; | |
if [ -n "${BR_PATH}" ]; then | |
case "${X1}" in | |
'curl') | |
if [ -n "${DETECTION_USER_AGENT}" ]; then | |
PAGE_SRC=`"${BR_PATH}" --silent --connect-timeout ${IP_RETRIEVE_TIMEOUT} --user-agent "${DETECTION_USER_AGENT}" --url "$1" 2>"${DEV_NULL}"`; | |
else | |
PAGE_SRC=`"${BR_PATH}" --silent --connect-timeout ${IP_RETRIEVE_TIMEOUT} --url "$1" 2>"${DEV_NULL}"`; | |
fi; | |
;; | |
'wget') | |
if [ -n "${DETECTION_USER_AGENT}" ]; then | |
PAGE_SRC=`"${BR_PATH}" --quiet --timeout=${IP_RETRIEVE_TIMEOUT} --output-document=- --user-agent "${DETECTION_USER_AGENT}" "$1" 2>"${DEV_NULL}"`; | |
else | |
PAGE_SRC=`"${BR_PATH}" --quiet --timeout=${IP_RETRIEVE_TIMEOUT} --output-document=- "$1" 2>"${DEV_NULL}"`; | |
fi; | |
;; | |
'lynx') | |
if [ -n "${DETECTION_USER_AGENT}" ]; then | |
PAGE_SRC=`"${BR_PATH}" -dump -connect_timeout=${IP_RETRIEVE_TIMEOUT} --useragent "${DETECTION_USER_AGENT}" "$1" 2>"${DEV_NULL}"`; | |
else | |
PAGE_SRC=`"${BR_PATH}" -dump -connect_timeout=${IP_RETRIEVE_TIMEOUT} "$1" 2>"${DEV_NULL}"`; | |
fi; | |
;; | |
'links2') | |
if [ -n "${DETECTION_USER_AGENT}" ]; then | |
PAGE_SRC=`"${BR_PATH}" -dump -receive-timeout ${IP_RETRIEVE_TIMEOUT} -fake-user-agent "${DETECTION_USER_AGENT}" "$1" 2>"${DEV_NULL}"`; | |
else | |
PAGE_SRC=`"${BR_PATH}" -dump -receive-timeout ${IP_RETRIEVE_TIMEOUT} "$1" 2>"${DEV_NULL}"`; | |
fi; | |
;; | |
esac; | |
fi; | |
if [ -n "${PAGE_SRC}" ]; then | |
RETRIEVED_IP=`echo "${PAGE_SRC}" |grep -E -o "${IP_REGEXP}" |uniq`; | |
fi; | |
if [ -n "${RETRIEVED_IP}" ]; then | |
echo "${RETRIEVED_IP}"; | |
return 0; | |
fi; | |
done; | |
return 3; | |
} | |
# create store file (for last IP, detection and refresh time) | |
touch "${STORE_PATH}"; | |
if [ -f "${STORE_PATH}" ]; then | |
. "${STORE_PATH}"; | |
else | |
echo "${MSG_9}: ${STORE_PATH}"; | |
exit ${ERR_BADSTOREPATH}; | |
fi; | |
# check last detection time, abort if checking too short after last one and not forced check/update | |
# check is applied for 'browser' method | |
# check if given retrieve method is valid | |
case "${IP_RETRIEVE_METHOD}" in | |
'iface') | |
# do nothing | |
;; | |
'browser') | |
if [ -n "${LAST_DETECTION}" ]; then | |
LAST_DET_FROM_NOW=`expr ${S70_NOW} - ${LAST_DETECTION}`; | |
if [ ${LAST_DET_FROM_NOW} -lt ${CHECK_IP_INTERVAL} -a ${FORCE_UPDATE_FLAG} -le 0 -a ${FORCE_CHECK_FLAG} -le 0 ]; then | |
WAIT_FROM_NOW=`expr ${CHECK_IP_INTERVAL} - ${LAST_DET_FROM_NOW}`; | |
echo "${MSG_4} ${WAIT_FROM_NOW} ${MSG_5}"; | |
exit ${ERR_OK_NOCHANGE}; | |
fi; | |
fi; | |
;; | |
*) | |
echo "${MSG_1}"; | |
exit ${ERR_BADRETRMETHOD}; | |
;; | |
esac; | |
# IP address not yet found | |
IP_FOUND_STATUS=0; | |
# check interface IP | |
if [ -n "${IP_INTERFACE}" ]; then | |
if [ ${PRE_CHECK_INTERFACE} -gt 0 ]; then | |
TMP_IPADDR=`ifconfig "${IP_INTERFACE}" |grep -E -o "inet( addr)?:${IP_REGEXP}" |uniq |cut -f 2 -d ':' 2>"${DEV_NULL}"`; | |
if [ -n "${TMP_IPADDR}" ]; then | |
IP_FOUND_STATUS=1; | |
IP_FOUND_METHOD='iface'; | |
IP_SOURCE_NOW="${IP_INTERFACE}"; | |
debug_msg "retrieve_method: iface"; | |
debug_msg "retrieve_source: ${IP_SOURCE_NOW}"; | |
debug_msg "retrieve_result: ${TMP_IPADDR}"; | |
else | |
debug_msg "retrieve_method: iface"; | |
debug_msg "retrieve_source: ${IP_SOURCE_NOW}"; | |
debug_msg "retrieve_result: ${MSG_2}"; | |
fi; | |
fi; | |
else | |
case "${IP_RETRIEVE_METHOD}" in | |
'iface') | |
log_output 'error' ${ERR_NOIFACE}; | |
echo "${MSG_13}"; | |
exit ${ERR_NOIFACE}; | |
;; | |
esac; | |
fi; | |
# check current public IP | |
for TMP_IP_SRC in `cat "${IP_SOURCES_FILE}" | grep -v -E "${COMMENT_REGEX}" | tr ' ' '_'`; do | |
if [ ${IP_FOUND_STATUS} -le 0 ]; then | |
if [ -z "${TMP_IPADDR}" ]; then | |
debug_msg "retrieve_method: ${IP_RETRIEVE_METHOD}"; | |
case "${IP_RETRIEVE_METHOD}" in | |
'browser') | |
TMP_IPADDR=`retr_ip_browser "${TMP_IP_SRC}"`; | |
;; | |
'iface') | |
# do nothing | |
;; | |
esac; | |
fi; | |
if [ -n "${TMP_IPADDR}" ]; then | |
IP_SOURCE_NOW="${TMP_IP_SRC}"; | |
IP_FOUND_STATUS=2; | |
IP_FOUND_METHOD="${IP_RETRIEVE_METHOD}"; | |
debug_msg "retrieve_source: ${IP_SOURCE_NOW}"; | |
debug_msg "retrieve_result: ${TMP_IPADDR}"; | |
else | |
debug_msg "retrieve_source: ${IP_SOURCE_NOW}"; | |
debug_msg "retrieve_result: ${MSG_2}"; | |
fi; | |
fi; | |
done; | |
# exit when no valid IP address found | |
if [ -z "${TMP_IPADDR}" ]; then | |
log_output 'error' ${ERR_NOVALIDIP}; | |
echo "${MSG_3}"; | |
exit ${ERR_NOVALIDIP}; | |
fi; | |
IP_NOW="${TMP_IPADDR}"; | |
LAST_DETECTION="${S70_NOW}"; | |
# check compare method | |
# compare last/host and current IP | |
case "${IP_COMPARE_METHOD}" in | |
'last') | |
debug_msg "compare_method: ${IP_COMPARE_METHOD}"; | |
debug_msg "last_ip: ${LAST_IP}"; | |
if [ "${LAST_IP}" != "${IP_NOW}" ]; then | |
EXEC_FLAG=1; | |
CHANGE_FLAG=1; | |
fi; | |
;; | |
'host') | |
debug_msg "compare_method: ${IP_COMPARE_METHOD}"; | |
if [ -f "${DOMAIN_FILE}" ]; then | |
CHECK_DOMAIN_FORCE=`cat --squeeze-blank "${DOMAIN_FILE}"`; | |
fi; | |
if [ -n "${CHECK_DOMAIN_FORCE}" ]; then | |
CHECK_DOMAIN="${CHECK_DOMAIN_FORCE}"; | |
fi; | |
if [ -z "${CHECK_DOMAIN}" ]; then | |
echo "${MSG_10}"; | |
exit ${ERR_BADDOMAINNAME}; | |
fi; | |
ip_from_host "${CHECK_DOMAIN}"; | |
if [ -z "${HOST_IP}" ]; then | |
log_output 'error' ${ERR_HOSTRESOLVERR}; | |
echo "${MSG_6}: ${CHECK_DOMAIN}"; | |
exit ${ERR_HOSTRESOLVERR}; | |
fi; | |
debug_msg "host: ${CHECK_DOMAIN}"; | |
debug_msg "host_ip: ${HOST_IP}"; | |
debug_msg "current_ip: ${IP_NOW}"; | |
if [ "${HOST_IP}" != "${IP_NOW}" ]; then | |
EXEC_FLAG=1; | |
CHANGE_FLAG=1; | |
fi; | |
;; | |
*) | |
echo "${MSG_15}"; | |
exit ${ERR_BADCOMPMETHOD}; | |
;; | |
esac; | |
debug_msg "change: ${CHANGE_FLAG}"; | |
debug_msg "exec: ${EXEC_FLAG}"; | |
# execute update script | |
if [ "${FORCE_UPDATE_FLAG}" -gt 0 ]; then | |
EXEC_FLAG=1; | |
fi; | |
debug_msg "forced_update: ${FORCE_UPDATE_FLAG}"; | |
if [ "${EXEC_FLAG}" -gt 0 ]; then | |
if [ ! -x "${IP_CHANGE_EXEC}" ]; then | |
echo "${MSG_7}"; | |
log_output 'error' ${IP_CHANGE_EXEC}; | |
exit ${ERR_EXECFAIL}; | |
fi; | |
if [ -n "${LAST_UPDATE}" ]; then | |
LAST_UPD_FROM_NOW=`expr ${S70_NOW} - ${LAST_UPDATE}`; | |
if [ "${LAST_UPD_FROM_NOW}" -lt "${UPDATE_IP_INTERVAL}" -a "${FORCE_UPDATE_FLAG}" -le 0 ]; then | |
WAIT_FROM_NOW=`expr ${UPDATE_IP_INTERVAL} - ${LAST_UPD_FROM_NOW}`; | |
echo "${MSG_12} ${WAIT_FROM_NOW} ${MSG_5}"; | |
exit ${ERR_OK_CHANGE}; | |
fi; | |
fi; | |
debug_msg "run: '${IP_CHANGE_EXEC} "${IP_NOW}" "${CHECK_DOMAIN}"'"; | |
if [ "${DEBUG_FLAG}" -gt 0 ]; then | |
"${IP_CHANGE_EXEC}" "${IP_NOW}" "${CHECK_DOMAIN}"; | |
else | |
"${IP_CHANGE_EXEC}" "${IP_NOW}" "${CHECK_DOMAIN}" 2>&1 >"${DEV_NULL}"; | |
fi; | |
UPDATE_SCRIPT_STATUS=$?; | |
if [ "${CHANGE_FLAG}" -gt 0 ]; then | |
log_output 'change' ${ERR_OK_CHANGE}; | |
else | |
log_output 'forced' ${ERR_OK_NOCHANGE}; | |
fi; | |
if [ ${UPDATE_SCRIPT_STATUS} != 0 -a "${STORE_FAILED_UPDATE}" -le 0 ]; then | |
# do nothing for now | |
echo '' >"${DEV_NULL}"; | |
else | |
LAST_UPDATE="${S70_NOW}"; | |
fi; | |
else | |
log_output 'nochange'; | |
debug_msg "run: -"; | |
fi; | |
# save values to store file | |
LAST_IP="${IP_NOW}"; | |
echo "LAST_IP='${LAST_IP}'" >"${STORE_PATH}"; | |
echo "LAST_DETECTION=${LAST_DETECTION}" >>"${STORE_PATH}"; | |
echo "LAST_UPDATE=${LAST_UPDATE}" >>"${STORE_PATH}"; | |
# goodbye, no errors | |
if [ "${CHANGE_FLAG}" -gt 0 ]; then | |
exit ${ERR_OK_CHANGE}; | |
fi; | |
exit ${ERR_OK_NOCHANGE}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment