# Paraments resub=1 push1="1" # Push online devices push_ddns="1" # Push ddns message serverchan_enable="1" # Enable ServerChan serverchan_sckey="" # ServerChan api key APITOKEN="" # Your API Token ZONEID="" # Your zone id, hex16 string RECORDNAME="" # Your DNS record name, e.g. sub.example.com RECORDTTL="1" # TTL in seconds (1=auto)
# Get wan IP # Check curl exist, if not, use wget getIpAddress() { curltest=`which curl` if [ -z "$curltest" ] || [ ! -s "`which curl`" ] ; then wget --no-check-certificate --quiet --output-document=- "http://members.3322.org/dyndns/getip" else curl -k -s "http://members.3322.org/dyndns/getip" fi } # load last IP lastIPAddress() { local inter="/tmp/home/root/lastIPAddress" cat $inter }
ddnsUpdate() { IP=${1} # Fetch DNS record ID RESPONSE="$( curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${ZONEID}/dns_records?page=1&#per_page=1000&order=type&direction=asc" \ -H "Authorization: Bearer ${APITOKEN}" \ -H "Content-Type:application/json" )" RECORDID="$(echo ${RESPONSE} | sed -n "s/.*\"id\":\"\([^\"]*\)\".*\"name\":\"${RECORDNAME}\".*/\1/p")"
# Update DNS record RESPONSE="$( curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/${ZONEID}/dns_records/${RECORDID}" \ -H "Authorization: Bearer ${APITOKEN}" \ -H "Content-Type: application/json" \ --data "{\"type\":\"A\",\"name\":\"${RECORDNAME}\",\"content\":\"${IP}\",\"ttl\":${RECORDTTL},\"proxied\":false}" )"
# Check whether the update was successful echo $RESPONSE | grep '"success":\ *true' >/dev/null
if [ $? -eq 0 ]; then logger -t "ddns" "Cloudflare ddns updated" return 0 else logger -t "ddns" "Cloudflare ddns updated failed" return 1 fi }
# Get online devices test(){ alias=`cat /var/lib/misc/dnsmasq.leases` arp | sed 's/(//;s/)//' | while read -r DESC IP AT MAC ETH ON IFACE do if [ $IFACE ]; then NAME=`echo "$alias" | awk '/'$MAC'\ '$IP'/{print $4}'` echo $NAME $IP >> /tmp/home/root/newhostname.txt fi done }
while [ "$serverchan_enable" = "1" ]; do curltest=`which curl` if [ -z "$curltest" ] ; then wget --continue --no-check-certificate -q -T 10 http://www.baidu.com [ "$?" == "0" ] && check=200 || check=404 else check=`curl -k -s -w "%{http_code}" "http://www.baidu.com" -o /dev/null` fi
if [ "$check" == "200" ] ; then local hostIP=$(getIpAddress) local lastIP=$(lastIPAddress)
if [ "$lastIP" != "$hostIP" ] && [ ! -z "$hostIP" ] ; then sleep 60 # Check again local hostIP=$(getIpAddress) local lastIP=$(lastIPAddress) fi
if [ "$lastIP" != "$hostIP" ] && [ ! -z "$hostIP" ] ; then logger -t "公网IP变动" "目前 IP: ${hostIP}" logger -t "公网IP变动" "上次 IP: ${lastIP}" ddnsUpdate ${hostIP} if [ "$?" == "0" ] ; then if [ "$push_ddns" = "1" ] ; then curl -s "http://sc.ftqq.com/$serverchan_sckey.send?text=AC9的DDNS更新啦" -d "&desp=${hostIP}" & logger -t "wechat push" "IP: ${hostIP} pushed" fi echo -n $hostIP > /tmp/home/root/lastIPAddress fi fi