#!/bin/bash
##################################################################################################
# #
# 고객사 전용 도메인 포워딩 Shellscript #
# #
# Script by CS@NAYANA #
# #
##################################################################################################
# pid 생성 중복실행을 방지
if [[ -s $0.pid ]];then exist_pid=`cat $0.pid`
if [[ -z `ps -e|grep $exist_pid` ]];then rm -f $0.pid;exec_confirm="Y"
else exec_confirm="N";echo -e "\e[1;32mShell has already running...\e[0m";fi
else exec_confirm="Y"
fi
# toDay
DateDay=`/bin/date '+%F' | sed -e 's/-//g'`
LogTime=`/bin/date '+%F %H:%m'`
# Setting file which
ServerIp=`hostname -i`
NamedConf='/etc/named.rfc1912.zones'
HttpdConf='/usr/local/apache/conf/extra/httpd-vhosts.conf'
# Setting file Backup
mkdir -p /opt/$DateDay.setting/
BackupDay="/opt/$DateDay.setting/"
cp -a $NamedConf $BackupDay
cp -a $HttpdConf $BackupDay
##################################################################################################
# Script Start
##################################################################################################
#
# Setting info input
if [[ -n $1 ]];then
id=$1
domain=$2
dir=$3
else
echo "############################### 도메인 포워딩 쉘 입니다. #########################"
echo "#\t 사용방법 #"
echo "#\t 계정아이디 도메인 디렉토리(필수아님) #"
echo "#\t account domain abc(필수아님) #"
echo "#\t [root@ free]# ./add.forwarding account domain abc(필수아님) #"
echo "##################################################################################"
read id domain dir
fi
# 변수 체크
if [[ x$id != "x" || x$domain != "x" ]];then
# Check PID
DomainNameCheck=`/bin/grep $domain $NamedConf | wc -l`
DomainHttpCheck=`/bin/grep $domain $HttpdConf | wc -l`
DomainCheck=`/usr/bin/expr $DomainNameCheck + $DomainHttpCheck`
NamedConfCheck=`/usr/sbin/named-checkconf | wc -l`
NewDomain=`/usr/bin/idn --quiet $domain`
AddHttpdPath=`/bin/grep -A1 $id/ $HttpdConf | sed -n '$p' | awk '{print $1,$2}'`
OldDomain=`sed -n '$p' $NamedConf | awk '{print $2}' | sed -e 's/\"//g'`
if [[ $dir != "" ]];then
dir=`echo $dir|sed -e 's/\/html//g;s/html\///g'`
dir="html/$dir"
else
dir="html"
fi
DirCnt=`echo $dir | cut -f1,2 -d"/" | awk '{print $2}' | wc -l`
if [[ $DomainCheck -eq 0 ]];then
# Virtual.cont 추가
if [[ $DirCnt -eq 0 ]]; then
sed -i '/'"$AddHttpdPath"'/a\ ServerAlias '"$NewDomain"' www.'"$NewDomain"'' $HttpdConf
else
echo "
<VirtualHost $ServerIp:80>
DocumentRoot /free/home/$id/$dir
ServerName $NewDomain
ServerAlias www.$NewDomain
CBandLimit 1Gi
CBandPeriod 1D
CustomLog logs/access_log combined
</VirtualHost>" >> $HttpdConf
fi
# named.conf 추가
sed -n '$p' $NamedConf | sed -e 's/'"$OldDomain"'/'"$NewDomain"'/g' >> $NamedConf
#
##################################################################################################
# Demon restart and Log
/usr/local/apache/bin/apachectl restart
/usr/local/apache/bin/apachectl configtest
if [[ $NamedConfCheck -eq 0 ]];then
/usr/sbin/rndc reload
ps -ef | grep --color=always named | grep -v "color=alway"
else
echo "named-checkconf 오류 => DNS 세팅 불가 => 확인바람!!"
fi
ps -ef | grep --color=always httpd | grep -v "color=alway"
echo -e "[36m[1m$id[0m : [32m$domain[0m : [31m세팅완료[0m"
echo "[$LogTime] $id \t $NewDomain 세팅 완료 " >> $BackupDay/setting.log
elif [[ x$id = "x" ]];then
echo "세팅 정보 입력값이 없어 종료됨."
echo "[$LogTime] $id \t $NewDomain 세팅 실패 " >> $BackupDay/setting.log
else
echo "httpd-vhost.conf에 이미 도메인($NewDomain)이 존재함 => 세팅불가!!"
echo "[$LogTime] $id \t $NewDomain 중복 세팅 실패" >> $BackupDay/setting.log
fi
else
echo "서버정보 및 세팅정보를 가져올 수 없음 => 세팅불가"
echo "[$LogTime] $id \t $NewDomain 세팅 실패 " >> $BackupDay/setting.log
fi
##################################################################################################