Last active
December 30, 2017 02:18
-
-
Save SpiraMirabilis/7fe83d743569e8c5892d21317af80f57 to your computer and use it in GitHub Desktop.
Use azure DNS as a dynamic DNS provider
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
from azure.mgmt.dns import DnsManagementClient | |
#from azure.common.credentials import UserPassCredentials | |
from azure.common.credentials import ServicePrincipalCredentials | |
from json import load | |
from urllib2 import urlopen | |
# Replace this with your subscription id | |
subscription_id = '33333333-3333-3333-3333-333333333333' | |
#credentials = UserPassCredentials( | |
# 'user@domain.com', # Your user | |
# 'my_password', # Your password | |
#) | |
# You should create a service principal so you're not using your own login | |
credentials = ServicePrincipalCredentials( | |
client_id = 'ABCDEFAB-1234-ABCD-1234-ABCDEFABCDEF', | |
secret = 'XXXXXXXXXXXXXXXXXXXXXXXX', | |
tenant = 'ABCDEFAB-1234-ABCD-1234-ABCDEFABCDEF' | |
) | |
dns_client = DnsManagementClient( | |
credentials, | |
subscription_id | |
) | |
my_ip = load(urlopen('http://jsonip.com'))['ip'] | |
# add a check here to see if my_ip has changed | |
record_set = dns_client.record_sets.create_or_update( | |
'<azure resource group>', | |
'<your domain>', | |
'HomeDDNS', | |
'A', | |
{ | |
"ttl": 60, | |
"arecords": [ | |
{ | |
"ipv4_address": my_ip | |
}, | |
] | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment