Skip to content

Instantly share code, notes, and snippets.

@pd12bbf7608ae1
Created August 5, 2020 13:55
Show Gist options
  • Save pd12bbf7608ae1/b98a51b2ba5e63c157f09214b3bbd463 to your computer and use it in GitHub Desktop.
Save pd12bbf7608ae1/b98a51b2ba5e63c157f09214b3bbd463 to your computer and use it in GitHub Desktop.
ESXi 远程动态域名解析脚本 搭配dynv6域名
#!/bin/bash
tokens=""
URL="https://dynv6.com/api/v2"
zone=""
name=""
historyFile=".iphistory"
ESXiServer="192.168.1.0"
ESXiPort="22"
ESXiDevice="vmk0"
user="root"
keyPath="$HOME/.ssh/id_esxi"
ipv4History=$(cat "$historyFile" | sed -n '1p')
ipv6History=$(cat "$historyFile" | sed -n '2p')
# export http_proxy="socks5://localhost"
# export https_proxy="socks5://localhost"
# curl https://google.com
ipv4Address=$(ssh -i "$keyPath" -p "$ESXiPort" "$user"@"$ESXiServer" "esxcli.py network ip interface ipv4 address list" | grep "$ESXiDevice" | awk '{printf $2}') ## 远程获得ESXi ipv4地址
if [[ -n "$ipv4Address" && "$ipv4Address" != "$ipv4History" ]]; then
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 更改ipv4地址..."
if [ -z "$zoneId" ]; then
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 查询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 "[$(date "+%Y-%m-%d %H:%M:%S")] 没有找到指定zone。"
else
if [ -z "$recordsList" ]; then
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 查询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
if [ -z "$recordsList" ]; then
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 查询recordsList失败。"
else
ipv4Update=1
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 查询recordsId A Part 1..."
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 "[$(date "+%Y-%m-%d %H:%M:%S")] 添加新的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 [ "$?" -ne "0" ]; then
ipv4Update=0
# ipv4History=${ipv4Address}
fi
else
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 修改现有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 [ "$?" -ne "0" ]; then
ipv4Update=0
# ipv4History=${ipv4Address}
fi
fi
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 查询recordsId A Part 2..."
recordsId_A=$(echo "$recordsList" | grep "\"name\":\"ipv4.${name}\"" | grep "\"type\":\"A\"" | sed -e "s/,/\n/g" | grep "\"id\"" | cut -d ":" -f 2 | sed -n "1p")
if [ -z "$recordsId_A" ]; then
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 添加新的A记录..."
curl -H "Authorization: Bearer ${tokens}" -H "Accept: application/json" -X "POST" -d "name=ipv4.${name}&data=${ipv4Address}&type=A" "${URL}/zones/{${zoneId}}/records" > /dev/null
if [ "$?" -ne "0" ]; then
ipv4Update=0
# ipv4History=${ipv4Address}
fi
else
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 修改现有A记录..."
curl -H "Authorization: Bearer ${tokens}" -H "Accept: application/json" -X "PATCH" -d "name=ipv4.${name}&data=${ipv4Address}&type=A" "${URL}/zones/{${zoneId}}/records/{${recordsId_A}}" > /dev/null
if [ "$?" -ne "0" ]; then
ipv4Update=0
# ipv4History=${ipv4Address}
fi
fi
if [ "$ipv4Update" == "1" ]; then
ipv4History=${ipv4Address}
fi
fi
fi
else
echo "[$(date "+%Y-%m-%d %H:%M:%S")] ipv4未更改"
fi
ipv6Address=$(ssh -i "$keyPath" -p "$ESXiPort" "$user"@"$ESXiServer" "esxcli.py network ip interface ipv6 address list" | grep "$ESXiDevice" | grep -v " fe80" | tail -n 1 | awk '{printf $2}') #### 远程获得ESXi ipv6地址
if [[ -n "$ipv6Address" && "$ipv6Address" != "$ipv6History" ]]; then
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 更改ipv6地址..."
if [ -z "$zoneId" ]; then
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 查询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 "[$(date "+%Y-%m-%d %H:%M:%S")] 没有找到指定zone。"
else
if [ -z "$recordsList" ]; then
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 查询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
if [ -z "$recordsList" ]; then
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 查询recordsList失败。"
else
ipv6Update=1
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 查询recordsId A Part 1..."
recordsId_A=$(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_A" ]; then
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 添加新的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 [ "$?" -ne "0" ]; then
ipv6Update=0
# ipv6History=${ipv6Address}
fi
else
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 修改现有AAAA记录..."
curl -H "Authorization: Bearer ${tokens}" -H "Accept: application/json" -X "PATCH" -d "name=${name}&data=${ipv6Address}&type=AAAA" "${URL}/zones/{${zoneId}}/records/{${recordsId_A}}" > /dev/null
if [ "$?" -ne "0" ]; then
ipv6Update=0
# ipv6History=${ipv6Address}
fi
fi
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 查询recordsId A Part 2..."
recordsId_A=$(echo "$recordsList" | grep "\"name\":\"ipv6.${name}\"" | grep "\"type\":\"AAAA\"" | sed -e "s/,/\n/g" | grep "\"id\"" | cut -d ":" -f 2 | sed -n "1p")
if [ -z "$recordsId_A" ]; then
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 添加新的AAAA记录..."
curl -H "Authorization: Bearer ${tokens}" -H "Accept: application/json" -X "POST" -d "name=ipv6.${name}&data=${ipv6Address}&type=AAAA" "${URL}/zones/{${zoneId}}/records" > /dev/null
if [ "$?" -ne "0" ]; then
ipv6Update=0
# ipv6History=${ipv6Address}
fi
else
echo "[$(date "+%Y-%m-%d %H:%M:%S")] 修改现有AAAA记录..."
curl -H "Authorization: Bearer ${tokens}" -H "Accept: application/json" -X "PATCH" -d "name=ipv6.${name}&data=${ipv6Address}&type=AAAA" "${URL}/zones/{${zoneId}}/records/{${recordsId_A}}" > /dev/null
if [ "$?" -ne "0" ]; then
ipv6Update=0
# ipv6History=${ipv6Address}
fi
fi
if [ "$ipv6Update" == "1" ]; then
ipv6History=${ipv6Address}
fi
fi
fi
else
echo "[$(date "+%Y-%m-%d %H:%M:%S")] ipv6未更改"
fi
printf "${ipv4History}\n${ipv6History}\n${ipv4Address}\n${ipv6Address}\n"
printf "${ipv4History}\n${ipv6History}" > "$historyFile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment