update_dns.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 = {
  11. 'authorization': CLEUCreds.JCLARKE_BASIC,
  12. 'accept': 'application/json',
  13. 'content-type': 'application/json'
  14. }
  15. if __name__ == '__main__':
  16. parser = ArgumentParser(description='Usage:')
  17. # script arguments
  18. parser.add_argument('-i', '--input', type=str,
  19. help='Path to input CSV file')
  20. parser.add_argument('--host', type=str, help='Hostname for a single add')
  21. parser.add_argument('--ip', type=str, help='IP address for a single add')
  22. args = parser.parse_args()
  23. hosts = []
  24. if not args.input:
  25. if not args.ip or not args.host:
  26. print('Single addition requires both a hostname and an IP address.')
  27. sys.exit(1)
  28. hosts.append((args.host, args.ip))
  29. else:
  30. contents = None
  31. try:
  32. fd = open(args.input, 'r')
  33. contents = fd.read()
  34. fd.close()
  35. except Exception as e:
  36. print("Failed to open {} for reading: {}".format(args.input, e))
  37. sys.exit(1)
  38. for row in contents.split('\n'):
  39. row = row.strip()
  40. if re.search(r'^#', row):
  41. continue
  42. if row == '':
  43. continue
  44. [hostname, ip] = row.split(',')
  45. hostname = hostname.strip().upper()
  46. ip = ip.strip()
  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(
  53. '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={
  60. 'zoneOrigin': C.DNS_DOMAIN}, headers=HEADERS, verify=False)
  61. response.raise_for_status()
  62. except Exception as e:
  63. sys.stderr.write(
  64. 'Failed to remove host {}: {}'.format(hostname, e))
  65. continue
  66. try:
  67. host_obj['addrs']['stringItem'][0] = ip
  68. response = requests.request(
  69. 'PUT', url, json=host_obj, headers=HEADERS, verify=False)
  70. response.raise_for_status()
  71. except Exception as e:
  72. sys.stderr.write(
  73. 'Error adding entry for {}: {}'.format(hostname, e))
  74. else:
  75. try:
  76. host_obj = {
  77. 'addrs': {
  78. 'stringItem': [
  79. ip
  80. ]
  81. },
  82. 'name': hostname,
  83. 'zoneOrigin': C.DNS_DOMAIN
  84. }
  85. response = requests.request(
  86. 'PUT', url, headers=HEADERS, json=host_obj, verify=False)
  87. response.raise_for_status()
  88. except Exception as e:
  89. sys.stderr.write(
  90. 'Error adding entry for {}: {}\n'.format(hostname, e))