Created
October 11, 2018 11:21
-
-
Save nairsgithub/1508da688835fbba4293532627b67548 to your computer and use it in GitHub Desktop.
Change A Name Record on Aws Route53 automatically detecting IP address of device you are running the code on. Use this as a DDNS feature if you have a dynamic IP and want to Host a website and do Port Forwarding.
This file contains hidden or 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
import boto3 | |
import socket | |
import datetime,time | |
ACCESS_KEY_ID = 'YOUR_ACCESS_KEY' | |
ACCESS_SECRET_KEY = 'YOUR_SECRET_KEY' | |
HOSTED_ZONE_ID = "Find this in route53" | |
def get_ip_address(): | |
import urllib2 | |
req = urllib2.Request('http://icanhazip.com', data=None) | |
response = urllib2.urlopen(req, timeout=5) | |
IP = str(response.read()) | |
IP = IP.split('\n') | |
print(IP[0]) | |
print("----") | |
return str(IP[0]) | |
def log(STATUS): | |
LOG = "log.txt" | |
STR = str(datetime.datetime.now()) + " | "+ str(STATUS) | |
with open (LOG, 'a') as f: f.write (STR + '\n') | |
print(STR) | |
client = boto3.client('route53',aws_access_key_id=ACCESS_KEY_ID,aws_secret_access_key=ACCESS_SECRET_KEY) | |
def add_cname_record(source, target): | |
try: | |
response = client.change_resource_record_sets( | |
HostedZoneId= HOSTED_ZONE_ID, | |
ChangeBatch= { | |
'Comment': 'add %s -> %s' % (source, target), | |
'Changes': [ | |
{ | |
'Action': 'UPSERT', | |
'ResourceRecordSet': { | |
'Name': source, | |
'Type': 'A', | |
'TTL': 300, | |
'ResourceRecords': [{'Value': target}] | |
} | |
}] | |
}) | |
except Exception as e: | |
log(e) | |
while True: | |
add_cname_record("www.yourwebsite.com", get_ip_address()) | |
add_cname_record("yourwebsite.com", get_ip_address()) | |
log("UPDATED DOMAIN IP") | |
time.sleep(1800) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment