Created
July 20, 2020 12:33
-
-
Save pd12bbf7608ae1/0fa79eb5740f91be2694035cb37789d2 to your computer and use it in GitHub Desktop.
Windows 10 dynv6 bash script with git-bash, curl and powershell.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
tokens="123456789" | |
URL="https://dynv6.com/api/v2" | |
historyFile="$HOME/.dynv6.addr6" | |
zone="helloworld.dynv6.net" | |
# name="example" #用于zone内的记录 | |
adapterName="Ethernet" | |
if [ -r "$historyFile" ]; then | |
ipv4History=$(cat "${historyFile}" | cut -d " " -f 1) | |
ipv6History=$(cat "${historyFile}" | cut -d " " -f 2) | |
fi | |
echo "获取公网ipv4..." | |
ipv4Address=$(curl "http://ip-api.com/json/?fields=query" 2>/dev/null | cut -d ":" -f 2 | sed -e "s/[\"}]//g") # 通过网络请求获取ipv4 | |
echo "获取ipv6..." | |
info=$(powershell "Get-NetIPAddress -AddressFamily IPv6 -InterfaceAlias ${adapterName} -PrefixOrigin RouterAdvertisement -SuffixOrigin Random") # 选用隐私地址 | |
# info=$(powershell "Get-NetIPAddress -AddressFamily IPv6 -InterfaceAlias ${adapterName} -PrefixOrigin RouterAdvertisement -SuffixOrigin Link") # 选用Mac生成地址 | |
# info=$(powershell "Get-NetIPAddress -AddressFamily IPv6 -InterfaceAlias ${adapterName} -PrefixOrigin DHCP") #选用DHCP地址 | |
if [ "$?" != "0" ]; then | |
echo "没有指定类型的ipv6地址" | |
unset info | |
fi | |
echo "$info" | |
ipv6Address=$(echo "${info}" | grep "IPAddress " | sed -e "s/^[[:alpha:][:blank:]]*://g" -e "s/[[:blank:]]*//g") # 本机获取ipv6 | |
# export https_proxy=socks5://127.0.0.1 | |
# export http_proxy=socks5://127.0.0.1 | |
if [[ -n "$ipv4Address" && "$ipv4Address" != "$ipv4History" ]]; then | |
echo "IPv4地址更新" | |
if [ -z "$zoneId" ]; then | |
echo "获取zoneId..." | |
zoneId=$(curl -H "Authorization: Bearer ${tokens}" -H "Accept: application/json" -X "GET" "${URL}/zones/by-name/{${zone}}" | sed -e "s/,/\n/g" -e "s/{//g" -e "s/}//g" | grep "\"id\"" | cut -d ":" -f 2) | |
fi | |
if [ -z "$zoneId" ]; then | |
echo "没有找到指定zone" | |
else | |
# 使用zone的情况 | |
echo "更新zone地址..." | |
curl -H "Authorization: Bearer ${tokens}" -H "Accept: application/json" -X "PATCH" -d "ipv4address=${ipv4Address}" "${URL}/zones/{${zoneId}}" > /dev/null | |
if [ "$?" -eq "0" ]; then | |
ipv4History="$ipv4Address" | |
fi | |
# # 使用zone下host的情况 | |
# if [ -z "$recordsList" ]; then | |
# echo "查询recordsList..." | |
# recordsList=$(curl -H "Authorization: Bearer ${tokens}" -H "Accept: application/json" -X "GET" "${URL}/zones/{${zoneId}}/records" | sed -e "s/},{/\n/g" -e "s/{//g" -e "s/}//g" -e "s/\[//g" -e "s/\]//g") | |
# fi | |
# echo "查询recordsId_A..." | |
# recordsId_A=$(echo "$recordsList" | grep "\"name\":\"${name}\"" | grep "\"type\":\"A\"" | sed -e "s/,/\n/g" | grep "\"id\"" | cut -d ":" -f 2 | sed -n "1p") | |
# if [ -z "$recordsId_A" ]; then | |
# echo "添加新的A记录..." | |
# curl -H "Authorization: Bearer ${tokens}" -H "Accept: application/json" -X "POST" -d "name=${name}&data=${ipv4Address}&type=A" "${URL}/zones/{${zoneId}}/records" > /dev/null | |
# if [ "$?" -eq "0" ]; then | |
# ipv4History="$ipv4Address" | |
# fi | |
# else | |
# echo "修改现有A记录..." | |
# curl -H "Authorization: Bearer ${tokens}" -H "Accept: application/json" -X "PATCH" -d "name=${name}&data=${ipv4Address}&type=A" "${URL}/zones/{${zoneId}}/records/{${recordsId_A}}" > /dev/null | |
# if [ "$?" -eq "0" ]; then | |
# ipv4History="$ipv4Address" | |
# fi | |
# fi | |
fi | |
fi | |
if [[ -n "$ipv6Address" && "$ipv6Address" != "$ipv6History" ]]; then | |
echo "IPv6地址更新" | |
if [ -z "$zoneId" ]; then | |
echo "获取zoneId..." | |
zoneId=$(curl -H "Authorization: Bearer ${tokens}" -H "Accept: application/json" -X "GET" "${URL}/zones/by-name/{${zone}}" | sed -e "s/,/\n/g" -e "s/{//g" -e "s/}//g" | grep "\"id\"" | cut -d ":" -f 2) | |
fi | |
if [ -z "$zoneId" ]; then | |
echo "没有找到指定zone" | |
else | |
# 使用zone的情况 | |
echo "更新zone地址..." | |
curl -H "Authorization: Bearer ${tokens}" -H "Accept: application/json" -X "PATCH" -d "ipv6prefix=${ipv6Address}" "${URL}/zones/{${zoneId}}" > /dev/null | |
if [ "$?" -eq "0" ]; then | |
ipv6History="$ipv6Address" | |
fi | |
# ## 使用zone下host的情况 | |
# if [ -z "$recordsList" ]; then | |
# echo "查询recordsList..." | |
# recordsList=$(curl -H "Authorization: Bearer ${tokens}" -H "Accept: application/json" -X "GET" "${URL}/zones/{${zoneId}}/records" | sed -e "s/},{/\n/g" -e "s/{//g" -e "s/}//g" -e "s/\[//g" -e "s/\]//g") | |
# fi | |
# echo "查询recordsId_AAAA..." | |
# recordsId_AAAA=$(echo "$recordsList" | grep "\"name\":\"${name}\"" | grep "\"type\":\"AAAA\"" | sed -e "s/,/\n/g" | grep "\"id\"" | cut -d ":" -f 2 | sed -n "1p") | |
# if [ -z "$recordsId_AAAA" ]; then | |
# echo "添加新的AAAA记录..." | |
# curl -H "Authorization: Bearer ${tokens}" -H "Accept: application/json" -X "POST" -d "name=${name}&data=${ipv6Address}&type=AAAA" "${URL}/zones/{${zoneId}}/records" > /dev/null | |
# if [ "$?" -eq "0" ]; then | |
# ipv6History="$ipv6Address" | |
# fi | |
# else | |
# echo "修改现有AAAA记录..." | |
# curl -H "Authorization: Bearer ${tokens}" -H "Accept: application/json" -X "PATCH" -d "name=${name}&data=${ipv6Address}&type=AAAA" "${URL}/zones/{${zoneId}}/records/{${recordsId_AAAA}}" > /dev/null | |
# if [ "$?" -eq "0" ]; then | |
# ipv6History="$ipv6Address" | |
# fi | |
# fi | |
fi | |
fi | |
echo -n "${ipv4History} ${ipv6History}" > "$historyFile" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment