用shell脚本使用腾讯云DDNS

昨晚突然需要用家里的公网ipv6访问玩客云,奈何家里的路由器是个普通路由没有openwrt那些,人又不在家里,用frp内网穿透有时候带宽又真难受,绝了。

腾讯云DDNS的shell脚本

在github上光了一圈找到了一个shell脚本https://github.com/rehiy/dnspod-shell

里面包含了ipv4和ipv6所以直接拿来用就好了。

使用

mkdir /root/ddns
git clone https://github.com/rehiy/dnspod-shell.git /root/ddns

拉取完后cd进ddns修改ddnspod.sh按照原作者的注释修改就好,其实就是id、token、你的域名和主机记录。如果不用ipv4或ipv6就将对应的arDdnsCheck注释掉就好了。

id和token的申请就不展开说了

自己弄一个shell脚本(我为了方便)

因为我玩客云是24小时开机的但是光猫我设置的定时重启换ip,所以为了方便就缝合了一下shell

我用的ipv6所以就把ardnspod里面ipv6的部分拿了出来缝合这样两个脚本判断的ipv6就是一致的了,那么当地址发生改变了再修改域名上所记录的地址就好了。

#!/bin/bash
dirfile='/root/ddns/ip_cache'
ddns='/root/ddns/ddnspod.sh'
#判断文件是否存在
if [ ! -f "$dirfile" ]; then
  touch "$dirfile"
  echo "1.1.1.1" > $dirfile
fi
lanIps="(^$)|(^::1$)|(^[fF][eE][8-9a-fA-F])"
    case $(uname) in
        'Linux')
            hostIp=$(ip -o -6 addr list | grep -Ev '\s(docker|lo)' | awk '{print $4,substr($NF,0,length($NF)-3)}' | sed 's/fore/2592000/g' | sort -k 2 -n | cut -d/ -f1 | grep -Ev "$lanIps" | head -n 1)
        ;;
        Darwin|FreeBSD)
            hostIp=$(ifconfig | grep "inet6 " | awk '{print $2}' | grep -Ev "$lanIps" | head -n 1)
        ;;
    esac
    if [ -z "$hostIp" ]; then
        if type wget >/dev/null 2>&1; then
            hostIp=$(wget -q -O- https://v6.myip.la)
        else
            hostIp=$(curl -s https://v6.myip.la)
        fi
    fi
    if [ -z "$hostIp" ]; then
        echo "arWanIp6 - Can't get ip address"
        return 1
    fi
    if [ -z "$(echo $hostIp | grep -E '^[0-9a-fA-F:]+$')" ]; then
        echo "arWanIp6 - Invalid ip address"
        return 1
    fi
old_ip=`cat $dirfile`
if [ "$hostIp" = "$old_ip" ]; then
    echo "ip未改变"
else
    echo $hostIp > $dirfile
    bash $ddns
fi

可能直接复制用不了,也不知道为啥。我把所需的文件放到文末链接。

定时设置就:

*/1 * * * * bash /root/ddns/ipv6.sh

所需文件

https://pan.baidu.com/s/1cvwnppSzwFlSaESZGXafBg 提取码99bg

原创文章,作者:生气猫,如若转载,请注明出处:https://zuiax.com/16056.html

(0)
上一篇 2022年7月23日
下一篇 2022年7月23日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注