共通設定:ドメイン設定

[root@www ~]# vi /var/www/html/ip_add.php
<?php
$ip_address = $_SERVER['REMOTE_ADDR'];
echo $ip_address ;
?>

ドメインIPアドレス取得スクリプト作成.

ドメインのIPアドレス取得スクリプトを作成します。

[root@www ~]# vi /var/www/html/ip_host.php
<?php
$ip_host_address = empty($_GET['host'])?null:gethostbyname($_GET['host']);
echo $ip_host_address;
?>

ドメインIPアドレス更新スクリプト作成.

ドメインのIPアドレス更新スクリプトを作成します。

[root@www ~]# vi /usr/local/bin/dns-update.sh
#!/bin/bash

UPIPURLMODE=${0}
SCRIPT=${1}
DNSUPURL=${2}

DOMAIN_LOG=${3}
DOMAIN=${4}
if [ "${DOMAIN_LOG}" = "" ]; then
    DOMAIN_JOB=""
else
    DOMAIN_JOB="?${DOMAIN_LOG}=${DOMAIN}"
fi

PASS_LOG=${5}
PASS=${6}
if [ "${PASS_LOG}" = "" ]; then
    PASS_JOB=""
else
    PASS_JOB="&${PASS_LOG}=${PASS}"
fi

IPGET_LOG=${7}

if [ "${UPIPURLMODE}" = "0" ]; then
    HOST_LOG=${8}
    HOST=${9}
elif [ "${UPIPURLMODE}" = "1" ]; then
    OTHERS1_LOG=${8}
    OTHERS1=${9}
    HOST_LOG=${10}
    HOST=${11}
elif [ "${UPIPURLMODE}" = "2" ]; then
    OTHERS1_LOG=${8}
    OTHERS1=${9}
    OTHERS2_LOG=${10}
    OTHERS2=${11}
    HOST_LOG=${12}
    HOST=${13}
else
    HOST_LOG=${8}
    HOST=${9}
fi

if [ "${OTHERS1_LOG}" = "" ]; then
    OTHERS1_JOB=""
else
    OTHERS1_JOB="&${OTHERS1_LOG}=${OTHERS1}"
fi

if [ "${OTHERS2_LOG}" = "" ]; then
    OTHERS1_JOB=""
else
    OTHERS1_JOB="&${OTHERS1_LOG}=${OTHERS1}"
fi

if [ "${HOST_LOG}" = "" ]; then
    HOST_JOB=""
else
    HOST_JOB="&${HOST_LOG}=${HOST}"
fi

#################################################################################

if [ "${HOST}" = "" ]; then
    FQDN=${DOMAIN}
else
    FQDN="${HOST}.${DOMAIN}"
fi

# Get domain ip address
IPGET=`curl /var/www/html/ip_host.php?host=${FQDN} 2>&1`
DOMAINIP=`echo "${IPGET}"|grep "has address"|awk '{ print $NF }'|egrep ^[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+$`
if [ $? -ne 0 ]; then
    echo "${FQDN} Domain ip address detection error" | logger -t $(basename $0)
    echo ${IPGET} | logger -t $(basename $0)
    exit
fi
IPGET_JOB="&${IPGET_LOG}=${IPGET}"

# Server ip address acquisition
IPGET=`curl /var/www/html/ip_add.php 2>&1`
echo "${IPGET}" | egrep ^[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+$ > /dev/null 2>&1
if [ $? -eq 0 ]; then
    SERVERIP=`echo "${IPGET}" | tail -n 1 | awk '{ print $NF }'`
else
    echo "${DOMAIN} Server ip address detection error" | logger -t $(basename $0)
    echo "${IPGET}" | logger -t $(basename $0)
    exit
fi

# Domain ip address update
if [ "${DOMAINIP}" != "${SERVERIP}" ]; then
    IPUPDATE=`wget -q -O - "${DNSUPURL}${DOMAIN_JOB}${PASS_JOB}${HOST_JOB}${SERVERIP_JOB}${OTHERS1_JOB}${OTHERS2_JOB}${IPGET_JOB}"`
    echo ${IPUPDATE}|grep -q OK
    if [ $? -eq 0 ]; then
        echo "${FQDN} Ip address update success ${DOMAINIP} to ${SERVERIP}" | logger -t $(basename $0)
    else
        echo "${FQDN} Ip address update error" | logger -t $(basename $0)
        echo ${IPUPDATE} | logger -t $(basename $0)
        (
        echo IP ADDRESS UPDATE ERROR
        echo ${IPUPDATE}
        )|mail -s "${SCRIPT##*/} ERROR on ${FQDN}" root
        exit 1
    fi
fi
exit

[root@www ~]# chmod +x /usr/local/bin/dns-update.sh

IPアドレス更新スクリプト実行.

IPアドレス更新スクリプトを実行します。

[root@www ~]# /usr/local/bin/dns-update.sh           

IPアドレス更新スクリプト定期自動実行設定.

IPアドレス更新スクリプト定期自動設定を実行します。

[root@www ~]#  vi /etc/cron.d/dns-update
/usr/local/bin/dns-update.sh           

コメント

タイトルとURLをコピーしました