#!/bin/bash
#
# DynamicDNSupdater - supports: DynDNS.org, NO-IP.com, DYNServ.ca and IPActive.de
#
# Es koennen auch verschiedene Dienste genutzt werden um mehrere DNS-Hosts
# zu nutzen, einfach Script kopieren, umbenennen, konfigurieren und erneut starten...
#
# Sonderzeichen im Password werden nun auch supported (danke IDDQD)
#
# credits: Didge2002, SnowHead, r00t, rj.2001, IDDQD
#
### CONFIG - START
USERNAME="xxxxxxx" # DDNS Benutzername
PASSWORD="xxxxxx" # DDNS Passwort
DDNSHOST="xxxxxxxx" # DDNS Hostname
EVERY="300" # Zeit in Sekunden zwischen den Abfragen
LOG="0" # Erstelle Logfile hiervon? (wenn moeglich aus lassen) [0=nein; 1=ja]
# Provider URL's - Es darf nur EINE benutzt werden, alle anderen auskommentieren!
# (beliebig erweiterbar)
#
# NEU: $USERNAME und $PASSWORD werden innerhalb der URL mit %USER% und %PASS% definiert!
#
# DynDNS.org
URL="
Sie müssen registriert sein, um Links zu sehen.
"
# No-IP.com
#URL="
Sie müssen registriert sein, um Links zu sehen.
"
# DYNServ.ca
#URL="
Sie müssen registriert sein, um Links zu sehen.
"
# IPActive.de
#URL="
Sie müssen registriert sein, um Links zu sehen.
"
### CONFIG - END
# -------------------------------------------------------------- #
# >>> >> > DO NOT MESS WiTH ANYTHiNG BELOW THiS LiNE! < << <<< #
# -------------------------------------------------------------- #
# required system cmd's:
# basename cat cut date echo expr killall ping rm sed sleep wget
CHECKIPURL="
Sie müssen registriert sein, um Links zu sehen.
"
CHECKIPURL2="
Sie müssen registriert sein, um Links zu sehen.
"
srcn=$(basename $0)
srcv="1.083"
srccfg="config.$srcn"
LOGFILE="/tmp/log.$srcn"
CHKFILE="/tmp/.$srcn"
PiDFILE="/tmp/.pid.$srcn"
OnlinePing="google.de"
IPCcfgdir="/var/emu/conf"
IPCfunctions="/var/emu/script/functions.sh"
IPCddnssrc="/var/emu/script/$srcn"
IPCddnslinkname="ddns"
C="\033[";Ca="${C}0;33m";Cb="${C}1;33m";Cn="${C}0m";ROT="${C}1;31m";GRUEN="${C}1;32m";BOLD="${C}1;37m"
Bo="${C}1m";Bc="${C}21m"; Ro="${C}3m";Rc="${C}23m"; Uo="${C}4m";Uc="${C}24m"
Blo="${C}5m";Blc="${C}25m"; Io="${C}7m";Ic="${C}27m"
function AddonInstalled () {
[ -f "/var/emu/conf/addons.info" ] && echo "ok" > /tmp/.AddonInstalled_$(basename $0)
}
function remove_addon () {
#onlinescript als addon entfernen
if [ -f "/var/emu/conf/addons.info" ]; then
Anum=$(sed -ne "/^$(basename $0).*/=" /var/emu/conf/addons.info)
[ ! -z "$Anum" ] && sed -i -e "$Anum d" /var/emu/conf/addons.info
fi
}
# create $srccfg...
proc_Create_Script_CFG() {
cfgVars="USERNAME PASSWORD DDNSHOST EVERY LOG URL"
NewCFG=$1
rm -f $NewCFG
echo "Erstelle ${NewCFG}.."
for cfgVar in $cfgVars; do
echo "$cfgVar=\"\$$cfgVar\"" >> $NewCFG
done
}
if [ -f "$IPCfunctions" -a ! -f "$IPCddnssrc" ]; then
echo ".. $srcn v$srcv .."
echo "Erstbenutzung dieses Scripts? IPC erkannt... kopiere Script $srcn..."
cp -f $0 $IPCddnssrc ; chmod 755 $IPCddnssrc >/dev/null 2>&1
echo -e "\nErstelle Symlink fuer ${Ro}$IPCddnslinkname${Rc} auf $IPCddnssrc"
ln -sf $IPCddnssrc /sbin/$IPCddnslinkname
echo -e "Nun muss noch der ${Ro}CONFIG${Rc} Abschnitt in dem Script $srcn angepasst werden!"
sleep 3
nano -R $IPCddnssrc
#[ ! -f "$IPCcfgdir/$srccfg" ] && proc_Create_Script_CFG "$IPCcfgdir/$srccfg"
echo -e "Fertig! Das Script kann kuenftig ueber den Befehl ${Ro}$IPCddnslinkname${Rc} ausgefuehrt werden"
AddonInstalled
$IPCddnssrc help
exit
fi
#[ ! -d "$IPCcfgdir" -a ! -f "$srccfg" ] && proc_Create_Script_CFG "$srcn.cfg"
# load srccfg?
[ -d "$IPCcfgdir" -a -f "$IPCcfgdir/$srccfg" ] && source $IPCcfgdir/$srccfg
[ ! -d "$IPCcfgdir" -a -f "$srccfg" ] && source $srccfg
# Simple URL encoding
proc_url_encode() {
out=${1//%/%25}; out=${out//\{/%7B}; out=${out//\}/%7D}; out=${out//|/%7C};
out=${out//\\/%5C}; out=${out//^/%5E}; out=${out//~/%7E}; out=${out//[/%5B};
out=${out//]/%5D}; out=${out//\`/%60}; out=${out//#/%23}; out=${out//>/%3E};
out=${out//\$/%24}; out=${out//&/%26}; out=${out//+/%2B}; out=${out//,/%2C};
out=${out//\//%2F}; out=${out//:/%3A}; out=${out//;/%3B}; out=${out//=/%3B};
out=${out//\?/%3F}; out=${out// /%20}; out=${out//</%3C};
out=${out//\!/%21}; out=${out//\"/%22}; out=${out//\@/%40}; out=${out//\'/%27};
out=${out//\(/%28}; out=${out//\)/%29}; out=${out//\*/%2A}; out=${out//\-/%2D};
out=${out//\_/%5F}; out=${out//\./%2E}; out=${out//\€/%80};
echo $out
}
# Replace %USER% and %PASS% encoded in $URL
URL=${URL//%USER%/`proc_url_encode "$USERNAME"`}
URL=${URL//%PASS%/`proc_url_encode "$PASSWORD"`}
do_update() {
wget -q -O ${CHKFILE}_answer $URL
}
LOG_update() {
if [ $LOG = "1" ]; then
_DT=$(date +"%d.%m.%Y %H:%M:%S")
if [ -z "$1" ]; then
answer=$(cat ${CHKFILE}_answer)
echo "$_DT: HOSTIP: $HOSTIP - CURRENTIP: $CURRENTIP - answer: $answer" >>$LOGFILE
else
echo "$_DT: $1" >>$LOGFILE
fi
fi
}
abuse_check() {
answer=$(cat ${CHKFILE}_answer)
MSG=""
if [ "$answer" = "abuse" ]; then
MSG="WARNING!! You abused the DNS update function! You must Unlock your hostname: $DDNSHOST !"
fi
[ "$answer" = "nohost" ] && MSG="ERROR: Unknown host $DDNSHOST !"
[ "$answer" = "badauth" ] && MSG="ERROR: Bad auth! Check USERNAME and PASSWORD setting!"
if [ ! -z "$MSG" ]; then
echo "$MSG"
[ -z "$1" -a $LOG = "1" ] && echo "$MSG" >>$LOGFILE
fi
}
get_IPs() {
HOSTIP=$(ping -c 1 "$DDNSHOST" | sed -n 1p | sed -e 's/(//g' -e 's/)//g' | cut -d " " -f3 | sed -e 's/\://g')
wget -q -O ${CHKFILE}_current_ip $CHECKIPURL
CURRENTIP=$(cat ${CHKFILE}_current_ip | cut -d ' ' -f 6 | cut -d '<' -f 1)
if [ -z "$CURRENTIP" -o "$CURRENTIP" = " *" -o "$CURRENTIP" = "*1.0 *" ]; then
if [ ! -z "$1" ]; then
echo "Hmm... $CHECKIPURL seems to be down?! Using: $CHECKIPURL2 insteed"
fi
if [ -z "$1" -a $LOG = "1" ]; then
echo "Hmm... $CHECKIPURL seems to be down?! Using: $CHECKIPURL2 insteed" >>$LOGFILE
fi
wget -q -O ${CHKFILE}_current_ip $CHECKIPURL2
CURRENTIP=`cat ${CHKFILE}_current_ip | grep "IP Address" | cut -d ' ' -f4 | sed -e 's/\$//g' -e 's/\\r//g'`
fi
}
update_interval() {
PiDs="$$ $!"
echo $PiDs > $PiDFILE
echo "Running Script in Background with PID(s): $PiDs"
while true; do
[ ! -f "$PiDFILE" ] && echo "$srcn: Interval stopped" && break
ONLINE=0
[ "$(ping -c1 "$OnlinePing" | grep -c "seq")" -eq 1 ] && ONLINE=1
[ "$ONLINE" ] && get_IPs
if [ "$ONLINE" ] && [ "$HOSTIP" != "$CURRENTIP" ]; then
do_update
abuse_check
LOG_update
fi
sleep $EVERY
done
}
kill_script() {
counter="5"
[ -f "$PiDFILE" ] && PID=$(cat $PiDFILE)
[ -z "$PID" ] && PID="$$ $!"
echo "PID(s): $PID"
echo -n "Stopping Script"
if [ ! -z "$PID" ]; then
if ( kill -TERM $PID 2> /dev/null ); then
c=1
while [ "$c" -le 300 ]; do
if ( kill -0 $PID 2> /dev/null ); then
echo -n "."
sleep 1
else
break
fi
c=$((++c))
done
fi
if ( kill -0 $PID 2> /dev/null ); then
echo "Script is not shutting down cleanly - killing"
kill -KILL $PID
else
echo " done"
fi
#for P in $PID; do
# kill -9 $P >/dev/null 2>&1
# echo "Killed PID $P"
#done
else
echo "Nothing to $1!"
fi
#
#fixme
#
# sleep 5
# if [ ! -z `pidof -x sleep` ]; then
# while [ $counter -gt 0 ]; do
# echo "...Retrying to kill script! (retrys: $counter , PID: $PID)"
# kill -9 $PID >/dev/null 2>&1
# PID=$(pidof -x $0)
# PID="$PID `pidof -x sleep`"
# counter=`expr $counter - 1`
# done
# fi
#echo "Should be killed..."
rm -f $PiDFILE
}
echo_PIDs() {
[ -f "$PiDFILE" ] && PID=$(cat $PiDFILE)
[ -z "$PID" ] && PID="$$ $!"
if [ -z "$PID" ]; then
echo "Seems script isnt running in background"
else
#PID="$PID `pidof -x sleep`"
echo "Scripts currently ProcessIDs: $PID"
fi
}
show_status() {
get_IPs
if [ "$HOSTIP" != "$CURRENTIP" ]; then
echo "The IP of $DDNSHOST ($HOSTIP) does NOT match your current Internet-IP: $CURRENTIP"
echo "You should update immediately! (with: $0 now)"
else
echo "Your host $DDNSHOST with IP $HOSTIP is still up to date: $CURRENTIP"
if [ -f "${CHKFILE}_answer" ]; then
answer=$(cat ${CHKFILE}_answer)
echo "Last Answer was: $answer"
fi
echo_PIDs
fi
}
if [ -f "$IPCfunctions" -a -f "$IPCddnssrc" ]; then
linkname=$IPCddnslinkname
else
linkname=$0
fi
case "$1" in
"")
update_interval
;;
[aA][uU][tT][oO]|[sS][tT][aA][rR][tT])
bash $0 &
;;
[kK][iI][lL][lL]|[sS][tT][oO][pP])
kill_script $1
;;
[nN][oO][wW])
get_IPs
do_update
abuse_check
echo "HOST: $DDNSHOST - HOSTIP: $HOSTIP - CURRENTIP: $CURRENTIP"
echo "Answer: $answer"
;;
[pP][iI][dD]*)
echo_PIDs
;;
[sS][tT][aA][tT][uU][sS])
show_status
;;
[hH][eE][lL][pP]|[hH][iI][lL][fF][eE]|?)
echo "Usage: $linkname [now|auto|kill|status|pid|help]"
echo "$linkname now -> Force to update $DDNSHOST immediately (Sofortiges updaten von $DDNSHOST)"
echo "$linkname auto -> Checks/updates $DDNSHOST every $EVERY seconds (Ueberprueft/updated $DDNSHOST alle $EVERY Sekunden)"
echo "$linkname kill -> Kills running script (Script wird beendet)"
echo "$linkname status -> Zeigt den aktuellen Status, ob ein update noetig ist"
echo "$linkname pid -> Zeigt die verwendeten ProzessIDs dieses Scripts"
echo "$linkname deinstall -> Deinstalls Script (Deinstalliert das Script)"
echo "$linkname help -> This help... (Zeigt diese Hilfe...)"
;;
[dD][eE][iI][nN][sS]*)
kill_script $1
rm -f $IPCddnssrc /sbin/$IPCddnslinkname
remove_addon
echo "Script deinstalled!"
;;
*)
echo "Usage: $linkname [now|auto|kill|status|pid|help]"
;;
esac
exit 0