更新文件

#!/bin/bash
sp="/home/script"
spt=$sp/tmp
sptt=$sp/tmp/
sps=$sp/src
if [ -d ${sp} ];then
        if [ -d ${sps} ]&&[ -d ${spt} ];then
                echo "${sps}&&${spt} is exist."
        else
                mkdir -p ${sp}/tmp
                mkdir -p ${sp}/src
sp="/home/script"
spt=$sp/tmp
sptt=$sp/tmp/
sps=$sp/src
if [ -d ${sp} ];then
        if [ -d ${sps} ]&&[ -d ${spt} ];then
                echo "${sps}&&${spt} is exist."
        else
                mkdir -p ${sp}/tmp
                mkdir -p ${sp}/src
        fi
else
        mkdir -p ${sp}
        mkdir -p ${sp}/tmp
        mkdir -p ${sp}/src
fi
cd $sps
wget http://www.lbase.net/Download/IPInfo-CN-UNICOM"("CNC")".txt
wget http://www.lbase.net/Download/IPInfo-CN-ChinaTelcom.txt
wget http://www.lbase.net/Download/IPInfo-CN-CTT.txt
wget http://www.lbase.net/Download/IPInfo-CN-ChinaMobile.txt
file=$(ls $sps)
for f in $file;do
        x=$(ls $f | awk -F '-' '{print $3}' | awk -F '.' '{print $1}')
        case $x in
        ChinaMobile)
                sed '/^\;/d' $f > $spt/mobile
                ;;
        ChinaTelcom)
                sed '/^\;/d' $f > $spt/telcom
                ;;
        CTT)
                sed '/^\;/d' $f > $spt/ctt
                ;;
        UNICOM\(CNC\))
                sed '/^\;/d' $f > $spt/unicom
                ;;
        esac
done
para=$(rpm -qa | grep dos2unix)
echo $para
if [ ! -n $para ]; then
echo "dos2unix hasn't installed on this server."
yum -y install dos2unix
else
echo "dos2unix has installed on this server."
fi
file1=$(ls $spt)
cd $spt
echo "converting started"
for x in $file1;do
        echo "convert ${sptt}${x}"
        dos2unix $sptt$x
done
echo "converting finished"
data="/usr/local/named/data/ip"
if [ ! -d $data ];then
        mkdir -p $data
fi
cd $data
file2=$(ls $spt)
y=$(date -d "today" +"%Y")
m=$(date -d "today" +"%m")
d=$(date -d "today" +"%d")
time=$(date |awk '{print $5}')
for i in $file2;do
        ipfile="${i}.ip"
        if [ -f $ipfile ];then
                echo "${ipfile} is exist."
                emts=$(cat $sptt$i)
                for e in $emts;do
                        t=$(cat $ipfile | grep $e)
                        if [[ -z $t ]];then
                                sed -i '/};/ i\'$e'\;' $ipfile
                                echo "${y}-${m}-${d} ${time} ipaddress $e inserted in ${ipfile}" >> /var/log/named-update.log
                                echo "${y}-${m}-${d} ${time} ipaddress $e inserted in ${ipfile}" >> $sp/mail
#                        else
#                                echo "${e} had in ${ipfile} list"
                        fi
                done
        else
                touch "${ipfile}"
                echo "acl ${i}{" > $ipfile
                echo "};" >> $ipfile
                emts=$(cat $sptt$i)
                for e in $emts;do
                        sed -i '/};/ i\'$e'\;' $ipfile
                        echo "${y}-${m}-${d} ${time} ipaddress $e inserted in ${ipfile}" >> /var/log/named-update.log
                        #echo "ipaddress $e inserted in ${ipfile}"
                        echo "${y}-${m}-${d} ${time} ipaddress $e inserted in ${ipfile}" >> $sp/mail
                done
        fi
done

if [ -f $sp/mail ];then
con=$(cat $sp/mail)
/usr/sbin/sendmail -t <<EOF
        From: zone update <root@localhost>
        To:[email protected];[email protected]
        Subject: zone update
                $con
EOF
        rm -rf $sp/mail
fi

rm -rf $spt $sps
rndc reload

猜你喜欢

转载自leovlys.iteye.com/blog/1961448