Skip to content

Instantly share code, notes, and snippets.

@Banshee1221
Created October 4, 2018 12:36
Show Gist options
  • Save Banshee1221/0b73a6c2a0f8df0f385fde6ec6719875 to your computer and use it in GitHub Desktop.
Save Banshee1221/0b73a6c2a0f8df0f385fde6ec6719875 to your computer and use it in GitHub Desktop.
Simple DDNS add/update/remove script
import argparse
import subprocess
import os
DNS_server = "x.x.x.x"
DNS_zone = "zone_name.com"
DNS_key_fullpath = "/path/to/.private_file"
parser = argparse.ArgumentParser(description='Add/Modify or remove DNS entries')
parser.add_argument('-r',
'--remove',
help='Remove DNS record from the server.',
action='store_true',
required=False)
parser.add_argument('-a',
'--add',
help='Add DNS record to the server.',
action='store_true',
required=False)
parser.add_argument('-n',
'--name',
help='The FQDN of the server to add or remove.',
type=str,
required=True)
parser.add_argument('-i',
'--ip',
help='The IP address of the server to add.',
type=str,
required=False)
args = parser.parse_args()
DNS_str = ""
if (args.add and args.remove):
print("Can't add and remove")
exit(1)
if args.add:
if args.ip is None:
print("Must have IP to add.")
exit(1)
DNS_str = """
server {0}
zone {1}
update delete {2}. A
update add {2}. 3600 A {3}
send
""".format(DNS_server, DNS_zone, args.name, args.ip)
if args.remove:
DNS_str = """
server {0}
zone {1}
update delete {2}. A
send
""".format(DNS_server, DNS_zone, args.name)
DNS_str = DNS_str.lstrip()
tmpFile = open("tmp.dns", "w")
tmpFile.write(DNS_str)
tmpFile.close()
subprocess.call(["nsupdate", "-k", "DNS_key_fullpath", "tmp.dns"])
os.remove("tmp.dns")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment