Skip to content

Instantly share code, notes, and snippets.

@withakay
Created Dec 2, 2020
Embed
What would you like to do?
Update Route53 from ISC DHCP Server when a new lease is created via python and boto3
# 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);
}
#!/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