#!/bin/bash
# Author: royoy
# Desc: add ddns ip for nginx
ddns_ips_file=/data/shell/ddns_ips/ddns_ips_file
nginx_bin=/usr/sbin/nginx
nginx_conf=/etc/nginx/conf.d/xxx.conf
remote_ddns=xxx.com
new_remote_ip=$(host $remote_ddns | awk '{print $NF}')
old_remote_ip=$(cat $ddns_ips_file)
flag=0
/usr/bin/ipcheck $new_remote_ip $old_remote_ip || exit 6
if [ "$new_remote_ip" != "$old_remote_ip" ];then
if [ -z "$old_remote_ip" ];then
if ! `grep -q "$new_remote_ip" $nginx_conf`;then
sed -i -e '/#LosAngeles/d' -e '/deny all/i\\tallow '"$new_remote_ip"'; #LosAngeles' $nginx_conf
flag=1
fi
else
if ! `grep -q "$new_remote_ip" $nginx_conf` && `grep -q "$old_remote_ip" $nginx_conf`;then
sed -i 's/'"$old_remote_ip"'/'"$new_remote_ip"'/' $nginx_conf
flag=1
fi
fi
echo $new_remote_ip > $ddns_ips_file
fi
if [ $flag -eq 1 ];then
$nginx_bin -t -q && $nginx_bin -s reload
fi