Sunday, September 11, 2016

Update Dynamic DNS menggunakan cron pada Linux

Berikut ini script yang saya gunakan untuk mengupdate Dynamic DNS menggunakan cron pada OS Linux.
Silahkan di-co-pas dan disesuaikan dengan keperluan anda, tempatkan  pada : /u/wjdynDNS/wjdynDNS.sh
Selamat mencoba

#!/bin/bash
#
# Declare host variable as the input variable
unset  WJ_IPCURR WJ_IPLAST
export WJ_FILETMP=/tmp/wjtelnet.tmp
export WJ_LASTLOG=/u/wjdynDNS/log/wjtelnet.log.`date +%m` ; [ ! -f $WJ_LASTLOG ] && touch $WJ_LASTLOG

WJ_LOG()
{
#Menggunakan print agar keluarannya dapat diatur posisinya
  printf "%-15s %s %-25s %s\n" $WJ_IPCURR "`date '+%a %Y-%m-%d %H:%M:%S'`" $1 >> $WJ_LASTLOG
}

export WJ_TARGET=192.168.1.1
export WJ_USER=root
export WJ_PASS=Zte521
export WJ_INTR=ppp0

# Start the expect script
(expect -c "
set timeout 10
# Start the session with the input variable and the rest of the hostname
spawn telnet $WJ_TARGET
expect \"*ogin:\"
send   \"$WJ_USER\r\"
expect \"*assword:\"
send   \"$WJ_PASS\r\"
expect \"*#\"
send   \"ifconfig $WJ_INTR | grep addr\r\"
expect \"*#\"
send   \"exit\r\"
#
# stop the expect script once the telnet session is closed
exit
") | grep 'inet addr' > $WJ_FILETMP

WJ_IPCURR="`sed -e 's/:/ /g' $WJ_FILETMP | awk '{print $3}'`"

WJ_LASTLOGx=$WJ_LASTLOG ; [ ! -s $WJ_LASTLOGx ] && WJ_LASTLOGx=${WJ_LASTLOG%.*}.`date +%m -d 'yesterday' `
WJ_IPLAST="`tail -1 $WJ_LASTLOGx | awk '{print $1}'`"


if [[ $WJ_IPCURR =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
then
   if [ "$WJ_IPCURR" = "$WJ_IPLAST" ]
   then
       WJ_LOG "_skipped_"
   else
       #Silahkan masukkan script/Dynamic DNS anda disini.
       #Contoh untuk www.noip.com
       #/u/noip/noip2 -c /u/noip/noip2.conf

       WJ_LOG "_executed_"
   fi
else
   WJ_LOG "-get_IP_failed-"
fi

Dan ini crontab-nya :

# Minute  Hour  Day of Month      Month          Day of Week      Command
# (0-59) (0-23)    (1-31)    (1-12 or Jan-Dec) (0-6 or Sun-Sat) (full path)
#-------+------+------------+-----------------+----------------+------------------------------------------------------------+
 0-59/15      *      *              *                 *        /u/wjdynDNS/wjdynDNS.sh

No comments:

Post a Comment