Created
December 2, 2020 14:58
-
-
Save withakay/f2681757384692b877589e105d2451d1 to your computer and use it in GitHub Desktop.
Update Route53 from ISC DHCP Server when a new lease is created via python and boto3
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
# add to dhcpd.conf | |
# make sure the dhcpd user has permission to execute the script | |
on commit { | |
set ClientName = pick-first-value(option fqdn.hostname, option host-name, "unknown-hostname"); | |
set ClientIp = binary-to-ascii(10, 8, ".", leased-address); | |
set ClientMac = binary-to-ascii(16, 8, ":", substring(hardware, 1, 6)); | |
execute("/opt/dhcpd-hooks/dhcp_event_hook.py", "commit", ClientName, ClientIp, ClientMac); | |
} |
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
#!/usr/bin/env python | |
import json | |
import os | |
import sys | |
import boto3 | |
def main(): | |
arg_names = ['action', 'name', 'ip', 'mac'] | |
args = [] | |
if len(sys.argv) > 1: | |
args = sys.argv[1:] | |
params = dict(zip(arg_names, args)) | |
if params["action"] == "commit": | |
update_dns(params) | |
def update_dns(params): | |
if params["name"] == "unknown-hostname": | |
print("unknown-hostname, DNS update aborted") | |
return 1 | |
client = boto3.client('route53', | |
aws_access_key_id="<< key >>", | |
aws_secret_access_key="<< secret >>") | |
response = client.change_resource_record_sets( | |
HostedZoneId='<< get hosted zone ID from AWS >>', | |
ChangeBatch={ | |
'Comment': 'updated by DHCP', | |
'Changes': [ | |
{ | |
'Action': 'UPSERT', | |
'ResourceRecordSet': { | |
'Name': params["name"] + '.int.labsgroup.com', | |
'Type': 'A', | |
'TTL': 120, | |
'ResourceRecords': [ | |
{ | |
'Value': params["ip"] | |
}, | |
] | |
} | |
}, | |
] | |
} | |
) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment