Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from __future__ import print_function, absolute_import
  3. import argparse
  4. import os
  5. import socket
  6. import subprocess
  7. import sys
  8.  
  9.  
  10. KEY_FILE = "/etc/bind/update_dns.private"
  11. BASE_STRING = "\nserver ns1.local\n"
  12.  
  13.  
  14. def parse_arguments():
  15. parser = argparse.ArgumentParser(description="Manage dns records")
  16. parser.add_argument("--name", dest="name", type=str, required=True, help="The record to create")
  17. parser.add_argument("--value", dest="value", type=str, required=True, help="The value (ip) of the record")
  18. parser.add_argument("--ttl", dest="ttl", type=int, default=300, help="The TTL of the record")
  19. parser.add_argument("--type", dest="type", type=str, default="A", help="The type of record to create")
  20. parser.add_argument("--create", dest="create", action="store_true", default=False, help="Add a DNS record")
  21. parser.add_argument("--rm", dest="remove", action="store_true", default=False, help="Remove a DNS record")
  22. parser.add_argument("--reverse", dest="reverse", action="store_true", default=False, help="Add a reversed DNS record too")
  23. arguments = parser.parse_args()
  24.  
  25. if all((arguments.create, arguments.remove)):
  26. parser.error("You can't use --rm and --create together.")
  27.  
  28. if not KEY_FILE or not os.path.isfile(KEY_FILE):
  29. parser.error('Key file {} not found!'.format(KEY_FILE))
  30.  
  31. arguments.key_file = KEY_FILE
  32. return arguments
  33.  
  34.  
  35. def run_nsupdate(command_string):
  36. try:
  37. subprocess.check_call("echo '{}' | /usr/bin/nsupdate -k {} /dev/stdin".format(command_string, KEY_FILE), shell=True)
  38. except subprocess.CalledProcessError as e:
  39. print("Failed to run nsupdate: {}".format(e), file=sys.stderr)
  40.  
  41.  
  42. def remove_record(rtype, name):
  43. command = BASE_STRING + "update delete {} {}\nsend\n".format(name, rtype)
  44. return run_nsupdate(command_string=command)
  45.  
  46.  
  47. def add_record(rtype, name, value, ttl):
  48. command = BASE_STRING + "update add {} {} {} {}\nsend\n".format(name, ttl, rtype, value)
  49. return run_nsupdate(command_string=command)
  50.  
  51.  
  52. def get_arpa(ip):
  53. try:
  54. return socket.gethostbyaddr(ip)[1][0]
  55. except (socket.error, IndexError, ValueError):
  56. return None
  57.  
  58.  
  59. if __name__ == "__main__":
  60. arguments = parse_arguments()
  61. arpa_record = get_arpa(ip=arguments.value)
  62.  
  63. remove_record(name=arguments.name, rtype=arguments.type)
  64.  
  65. if arpa_record and arguments.reverse:
  66. remove_record(name=arpa_record, rtype="PTR")
  67.  
  68. if arguments.create:
  69. add_record(rtype="A", name=arguments.name, value=arguments.value, ttl=arguments.ttl)
  70. if arguments.reverse:
  71. add_record(rtype="PTR", name=arpa_record, value=arguments.name, ttl=arguments.ttl)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement