#!/bin/bash
# Author: royoy
# Desc: add ip of ddns
ddns_ips_file=/root/.shell/ddns_ips/ddns_ips_file
nginx_bin=/usr/sbin/nginx
nginx_conf=/etc/nginx/conf.d/royoy.conf
remote_ddns=xxx.abc.com
new_remote_ip=$(host $remote_ddns | awk '{print $NF}')
old_remote_ip=$(cat $ddns_ips_file)
flag=0
#此处必须为绝对路径,否则即使导入环境变量文件cron也不执行
/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 '/remote_addr/s/)/|'"$new_remote_ip"'&/' $nginx_conf
flag=1
fi
else
if ! `grep -q "$new_remote_ip" $nginx_conf` && `grep -q "$old_remote_ip" $nginx_conf`;then
sed -i '/remote_addr/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