update_dns.py 3.3 KB

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