#!/bin/bash
i=0
changed=0
MYDIR=`dirname $0`
oldip=(`cat $MYDIR/oldip.txt`)
tmpip=()
for domain in `cat $MYDIR/domain.txt`; do
	newip=`host $domain`
	newip=${newip#*'address '}
	newip=${newip%%$'\n'*}	
	 #echo $domain - $newip
	
	if echo "$newip"|grep "[a-zA-Z]" >/dev/null || [ "$newip" == "" ]
	then 
	    tmpip[$i]=${oldip[$i]}
	    i=$[$i+1]
	    continue
	fi
	 #echo $domain - ${oldip[$i]} - $newip - $i

	if [[ $newip != ${oldip[$i]} ]]
	then
		echo $domain - $newip - ${oldip[$i]}
		changed=1
		tmpip[$i]=$newip
	else
		tmpip[$i]=${oldip[$i]}
	fi
	i=$[$i+1]
done

if [ $changed = 1 ]
then
	/etc/init.d/nginx reload
	echo ${tmpip[*]} > $MYDIR/oldip.txt
	echo -e `date +"%Y-%m-%d %H:%M:%S"`"\n"
fi
#检测nginx是否运行
nginx=`ps -ef |grep nginx|grep -v grep|wc -l`
if [ $nginx -eq 2 ]
then
/etc/init.d/nginx start
fi
#每执行一次 输出 '.'
#echo -e ".\c"