admin';?>

首页 / php code

update_ddns.sh for namesilo.com

By admin  •  2020-05-17 17:28:23  •  151次点击
永久外链: https://i.otherhill.com/static/c1709d14982011ea9de7809b202ce664.html

update_ddns.sh用来动态更新域名,

xmllint not found:
sudo apt install libxml2-utils


update_ddns.sh代码如下: 
#!/bin/bash

#http://blog.pztop.com/2016/04/25/Namesilo-with-DDNS/
DOMAIN="yourdomain.com"

##Host name.
##If you want manage host "myhost.mydomain.tld", then
HOST="ddns"
CURL=`which curl`
APIKEY="填写namesio.com分配给你的apikey"

NAMESILO_TMP_DIRECTORY=/tmp/.namesilo/
mkdir -p $NAMESILO_TMP_DIRECTORY

##Saved history pubic IP from last check
IP_FILE=$NAMESILO_TMP_DIRECTORY"MyPubIP"
RESPONSE=$NAMESILO_TMP_DIRECTORY"namesilo_response.xml"
#echo $IP_FILE
##Get the current public IP
#https://ifconfig.me/ip   国外线路,访问会很快.
#http://whatismyip.akamai.com/
#http://icanhazip.com/ can get ipv6
#http://myip.dnsomatic.com/
#wget -q -O - checkip.dyndns.com | grep -Po "[\d\.]+"
CUR_IP=$($CURL -s http://whatismyip.akamai.com/)

if [ $? -ne 0 ] || [ x$CUR_IP == 'x' ]; then
     CUR_IP=$($CURL -s http://myip.dnsomatic.com/)
fi

##Exit if curl failed
if [ $? -ne 0 ]; then
     echo "$CURL -s api.online-service.vip/ip/me faild!I can't get your public ip"
fi
echo "current public IPv4 is: "$CUR_IP

if [[ $CUR_IP =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
     echo $CUR_IP " is good"
else
     echo $CUR_IP " is invalid!!"
exit
fi

##Check file for previous IP address
if [ -f $IP_FILE ]; then
     #取首行ipv4地址
     KNOWN_IP=$(head $IP_FILE -n 1|tail -n 1)
else
     KNOWN_IP=
fi
echo $KNOWN_IP

#86400 one day
#可以放很多自定义的子域名,也支持 "*"
array_hosts=("i" "otfiles" "other_child_domain" "other_child_domain" "other_child_domain" "other_child_domain")
array_v6hosts=()
#array_v6hosts=("local" "ipv6")

if [ "$CUR_IP" != "$KNOWN_IP" ]; then
     echo "$CUR_IP != $KNOWN_IP"
else
     echo "$CUR_IP == $KNOWN_IP"
fi


##See if the IP has changed
if [ "$CUR_IP" != "$KNOWN_IP" ]; then
CUR_IP6=$($CURL -s http://ip6only.me/api/ |awk -F, '{print $2}')

if [ $? -ne 0 ]; then
     CUR_IP6=$($CURL -s http://v6.ipv6-test.com/api/myip.php)
fi
echo "current public IPv6 is: "$CUR_IP6
##Update DNS record in Namesilo:
$CURL -s "https://www.namesilo.com/api/dnsListRecords?version=1&type=xml&key=$APIKEY&domain=$DOMAIN" > $DOMAIN.xml
RECORD_ID=`xmllint --xpath "//namesilo/reply/resource_record/record_id[../host/text() = '$HOST.$DOMAIN' ]" $DOMAIN.xml | grep -oP '(?<=<record_id>).*?(?=</record_id>)'`
$CURL -s "https://www.namesilo.com/api/dnsUpdateRecord?version=1&type=xml&key=$APIKEY&domain=$DOMAIN&rrid=$RECORD_ID&rrhost=$HOST&rrvalue=$CUR_IP&rrttl=3600" > $RESPONSE
RESPONSE_CODE=`xmllint --xpath "//namesilo/reply/code/text()" $RESPONSE`
case $RESPONSE_CODE in
300)
     echo $CUR_IP > $IP_FILE
     echo $CUR_IP6 >> $IP_FILE
     for i in "${array_hosts[@]}"
     do
          HOST=$i
          RECORD_ID=`xmllint --xpath "//namesilo/reply/resource_record/record_id[../host/text() = '$HOST.$DOMAIN' ]" $DOMAIN.xml | grep -oP '(?<=<record_id>).*?(?=</record_id>)'`

          echo "$HOST".$DOMAIN"'s Ip have been changed to "$CUR_IP" successfully!"
     done

     for i in "${array_v6hosts[@]}"
     do
          HOST=$i
          RECORD_ID=`xmllint --xpath "//namesilo/reply/resource_record/record_id[../host/text() = '$HOST.$DOMAIN' ]" $DOMAIN.xml | grep -oP '(?<=<record_id>).*?(?=</record_id>)'`
          $CURL -s "https://www.namesilo.com/api/dnsUpdateRecord?version=1&type=xml&key=$APIKEY&domain=$DOMAIN&rrid=$RECORD_ID&rrhost=$HOST&rrvalue=$CUR_IP6&rrttl=3600" > $RESPONSE
          echo "$HOST".$DOMAIN"'s Ip6 have been changed to "$CUR_IP6" successfully!"
     done
     logger -t IP.Check -- Update success. Now $HOST.$DOMAIN IP address is $CUR_IP;;
280)
     logger -t IP.Check -- Duplicate record exist. No update necessary;;
*)
     logger -t IP.Check -- DDNS update failed!;;
esac

logger -t IP.Check -- Public IP changed to $CUR_IP and $CUR_IP6
else
logger -t IP.Check -- NO IP change
fi

#sudo tail /var/log/messages -c 50000|grep IP
#echo > /home/xxxx/.namesilo/MyPubIP
#dig child.yourdomain.com @ns1.dnsowl.com
#dig child.yourdomain.com AAAA @ns1.dnsowl.com
#service dnsmasq restart
#https://crontab.guru/
#crontab -e
#*/30 * * * * /run/media/xxxxx/6T_20181101_1/proj/shell/namesilo/update_ddns.sh
exit 0

可以使用 crontab -e 来添加计划任务, 以下命令是每十分钟执行一遍update_ddns.sh脚本.
*/10 * * * * /run/media/xxx/6T_20181101_1/proj/shell/namesilo/update_ddns.sh








0 回复 | 直到2022-01-29 18:41添加回复

回复

最新贴子

生成图片 生成二维码 私信给我
生成密码 清空