-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathazure-ddns.sh
More file actions
101 lines (87 loc) · 3.54 KB
/
Copy pathazure-ddns.sh
File metadata and controls
101 lines (87 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
# Script to update a record in Azure DNS to match the current public IP address
# Tenant ID
tenantId="YOUR TENANT ID"
# Azure AD App ID
appId="YOUR APP ID"
# Azure AD App Secret
appSecret="YOUR APP SECRET"
# Azure resource group name where your DNS zone is configured
rgName="YOUR RESOURCE group"
# DNS zone name
zoneName="YOUR DNS ZONE TO UPDATE"
# Existing A record-set name in your DNS zone
recordsetName="YOUR RECORD TO UPDATE"
# Prefix that will be searched to verify whether you are on the right network
# Enable IPv6
enableIpv6=true
# FQDN
fqdn=$recordsetName.$zoneName
# Verify 'dig' is there
digPath=$(which dig)
if [[ -z $digPath ]]
then
echo "dig does not seem to be installed, but it is required for this script"
exit 1
fi
# Get public IPv4 from akamai
myPublicIpv4=$(curl http://whatismyip.akamai.com/)
echo "The current IPv4 IP is $myPublicIpv4"
# Get existing public IPv4 from DNS
myDnsIpv4=$(dig +short @8.8.8.8 $fqdn A)
if [ $myPublicIpv4 == $myDnsIpv4 ]
then
echo "DNS IPv4 up to date, nothing to be done"
else
echo "Current public IPv4 address $myPublicIpv4 different from DNS IPv4 address $myDnsIpv4, proceeding to update DNS"
# Login to Azure
az login --service-principal --tenant $tenantId --username $appId --password=$appSecret >/dev/null 2>&1
# Configure default resource group to rgName
az configure --defaults group=$rgName >/dev/null 2>&1
# Remove old record from record-set
az network dns record-set a remove-record -n $recordsetName -z $zoneName --ipv4-address $myDnsIpv4 >/dev/null 2>&1
# Add new record to the record-set
az network dns record-set a add-record -n $recordsetName -z $zoneName --ipv4-address $myPublicIpv4 --ttl 60 >/dev/null 2>&1
sleep 120
newDnsIpv4=$(dig +short @8.8.8.8 $fqdn A)
if [ $myPublicIpv4 == $newDnsIpv4 ]
then
echo "DNS A record correctly updated and verified"
else
echo "DNS A record updated, the verification was not successful yet, verify with the commnad 'nslookup $fqdn' in a few minutes/hours"
fi
# Bye!
az logout
fi
if [ $enableIpv6 == true ]
then
# Get public IPv6 from akamai
myPublicIpv6=$(curl http://ipv6.whatismyip.akamai.com/)
echo "The current IPv6 IP is $myPublicIpv6"
# Get existing IPv6 from DNS
myDnsIpv6=$(dig +short @8.8.8.8 $fqdn AAAA)
if [ $myPublicIpv6 == $myDnsIpv6 ]
then
echo "DNS IPv6 up to date, nothing to be done"
else
echo "Current public IPv6 address $myPublicIpv6 is different from DNS IPv6 address $myDnsIpv6. Updating DNS"
# Login to Azure
az login --service-principal --tenant $tenantId --username $appId --password=$appSecret >/dev/null 2>&1
# Configure default resource group to rgName
az configure --defaults group=$rgName >/dev/null 2>&1
# Remove old record from record-set
az network dns record-set aaaa remove-record -n $recordsetName -z $zoneName --ipv6-address $myDnsIpv6 >/dev/null 2>&1
# Add new record to the record-set
az network dns record-set aaaa add-record -n $recordsetName -z $zoneName --ipv6-address $myPublicIpv6 --ttl 60 >/dev/null 2>&1
sleep 120
newDnsIpv6=$(dig +short @8.8.8.8 $fqdn AAAA)
if [ $myPublicIpv6 == $newDnsIpv6 ]
then
echo "DNS AAAA correctly updated and verified"
else
echo "DNS AAAA updated, the verification was not successful yet, verify with the commnad 'nslookup $fqdn' in a few minutes/hours"
fi
# Bye!
az logout
fi
fi