update_dns.py 3.5 KB

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