update_dns.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python3
  2. import requests
  3. from requests.packages.urllib3.exceptions import InsecureRequestWarning
  4. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  5. import sys
  6. import re
  7. from argparse import ArgumentParser
  8. import CLEUCreds
  9. from cleu.config import Config as C
  10. HEADERS = {"authorization": CLEUCreds.JCLARKE_BASIC, "accept": "application/json", "content-type": "application/json"}
  11. if __name__ == "__main__":
  12. parser = ArgumentParser(description="Usage:")
  13. # script arguments
  14. parser.add_argument("-i", "--input", type=str, help="Path to input CSV file")
  15. parser.add_argument("--host", type=str, help="Hostname for a single add")
  16. parser.add_argument("--ip", type=str, help="IP address for a single add")
  17. args = parser.parse_args()
  18. hosts = []
  19. if not args.input:
  20. if not args.ip or not args.host:
  21. print("Single addition requires both a hostname and an IP address.")
  22. sys.exit(1)
  23. hosts.append((args.host, args.ip))
  24. else:
  25. contents = None
  26. try:
  27. fd = open(args.input, "r")
  28. contents = fd.read()
  29. fd.close()
  30. except Exception as e:
  31. print("Failed to open {} for reading: {}".format(args.input, e))
  32. sys.exit(1)
  33. for row in contents.split("\n"):
  34. row = row.strip()
  35. if re.search(r"^#", row):
  36. continue
  37. if row == "":
  38. continue
  39. [hostname, ip] = row.split(",")
  40. hostname = hostname.strip()
  41. ip = ip.strip()
  42. if re.match(r"[\d\.]+", hostname):
  43. t = ip
  44. ip = hostname
  45. hostname = t
  46. hostname = hostname.upper()
  47. hosts.append((hostname, ip))
  48. for h in hosts:
  49. hostname = h[0]
  50. ip = h[1]
  51. url = C.DNS_BASE + "CCMHost" + "/{}".format(hostname)
  52. response = requests.request("GET", url, params={"zoneOrigin": C.DNS_DOMAIN}, headers=HEADERS, verify=False)
  53. if response.status_code != 404:
  54. host_obj = response.json()
  55. a = host_obj["addrs"]["stringItem"][0]
  56. if a != ip:
  57. try:
  58. response = requests.request("DELETE", url, params={"zoneOrigin": C.DNS_DOMAIN}, headers=HEADERS, verify=False)
  59. response.raise_for_status()
  60. except Exception as e:
  61. sys.stderr.write("Failed to remove host {}: {}".format(hostname, e))
  62. continue
  63. try:
  64. host_obj["addrs"]["stringItem"][0] = ip
  65. response = requests.request("PUT", url, json=host_obj, headers=HEADERS, verify=False)
  66. response.raise_for_status()
  67. except Exception as e:
  68. sys.stderr.write("Error adding entry for {}: {}".format(hostname, e))
  69. else:
  70. try:
  71. host_obj = {"addrs": {"stringItem": [ip]}, "name": hostname, "zoneOrigin": C.DNS_DOMAIN}
  72. response = requests.request("PUT", url, headers=HEADERS, json=host_obj, verify=False)
  73. response.raise_for_status()
  74. except Exception as e:
  75. sys.stderr.write("Error adding entry for {}: {}\n".format(hostname, e))