udm-dns.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env python3
  2. from __future__ import print_function
  3. import re
  4. from datetime import timedelta
  5. import argparse
  6. import unificontrol
  7. import os.path
  8. from google.auth.transport.requests import Request
  9. from google.oauth2.credentials import Credentials
  10. from google_auth_oauthlib.flow import InstalledAppFlow
  11. from googleapiclient.discovery import build
  12. from googleapiclient.errors import HttpError
  13. import math
  14. SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
  15. SPREADSHEET_ID = '18v8v6hoZTjahWhvyxRldmbdVlqAyKLa2HDINJJfTgKA'
  16. class Client:
  17. def __init__(self, hostname, address, macAddress, k8s):
  18. self.hostname = hostname
  19. self.address = address
  20. self.macAddress = macAddress
  21. self.k8s = k8s
  22. def formatYamlEntry(self):
  23. entry = " {} {} # {}\n".format(self.hostname, self.address, self.macAddress)
  24. return entry
  25. def getClientsSheets(args):
  26. print("SheetID: {}".format(args.sheetid))
  27. c = Client(hostname="tesseract", address="192.168.1.87", macAddress="00:00:00:AA:BB:CC", k8s=False)
  28. return c
  29. def getClientsUnifi(args):
  30. UNIFI_USER='robot'
  31. UNIFI_PASS='fguohsfogj910A'
  32. udm = unificontrol.UnifiClient(host='192.168.1.1', username=UNIFI_USER, password=UNIFI_PASS, site='Default')
  33. for mac in udm.list_guests():
  34. print(mac)
  35. c = Client(hostname="gondor", address="192.168.1.9", macAddress="00:00:00:AA:BB:CC", k8s=False)
  36. return c
  37. def getClientsPihole(args):
  38. c = Client(hostname="morgul", address="192.168.1.1", macAddress="00:00:00:AA:BB:CC", k8s=False)
  39. return c
  40. def buildGoogleCredentials(args):
  41. creds = None
  42. if os.path.exists('token.json'):
  43. creds = Credentials.from_authorized_user_file('token.json', SCOPES)
  44. if not creds or not creds.valid:
  45. if creds and creds.expired and creds.refresh_token:
  46. creds.refresh(Request())
  47. else:
  48. flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
  49. creds = flow.run_local_server(port=0)
  50. with open('token.json', 'w') as token:
  51. token.write(creds.to_json())
  52. return creds
  53. def main():
  54. parser = argparse.ArgumentParser()
  55. parser.add_argument("--pihole", required=False, help="Render k8s configMap for pihole use", action='store_true')
  56. parser.add_argument("--pifile", required=False, help="Filename to store rendered yaml", type=str)
  57. parser.add_argument("--unifi", required=False, help="Sync client addresses to unifi controller", action='store_true')
  58. parser.add_argument("--hostname", required=False, help="Unifi Hostname", type=str, default='192.168.1.1')
  59. parser.add_argument("--username", required=False, help="Unifi Username", type=str, default='admin')
  60. parser.add_argument("--password", required=False, help="Unifi Password", type=str, default='admin')
  61. parser.add_argument("--sheet", required=False, help="Sync client address to Google Sheet", action='store_true')
  62. parser.add_argument("--sheetid", required=False, help="Google Sheet ID to read from", type=str)
  63. parser.add_argument("--readfrom", required=True, help="Which datasource to read from (unifi, pihole, sheets)", type=str, action='append')
  64. parser.add_argument("--writeto", required=True, help="Which datasource to write to (unifi, pihole, sheets, stdout)", type=str, action='append')
  65. clients = []
  66. args = parser.parse_args()
  67. for read in args.readfrom:
  68. results = []
  69. print("Reading from {}".format(read))
  70. if read == "sheets":
  71. results.append(getClientsSheets(args))
  72. if read == "unifi":
  73. results.append(getClientsUnifi(args))
  74. if read == "pihole":
  75. results.append(getClientsPihole(args))
  76. for result in results:
  77. for c in clients:
  78. print("{}".format(c.address))
  79. print("Merging {} results from {} into Clients".format(result.hostname, read))
  80. clients.append(result)
  81. print("Len: {}".format(len(clients)))
  82. for c in clients:
  83. print("Clients: {} {} {}".format(c.address, c.hostname, c.macAddress))
  84. for write in args.writeto:
  85. print("Writing to {}".format(write))
  86. for c in clients:
  87. print("Discovered Client: {}".format(c.hostname))
  88. if write == "pihole":
  89. print(c.formatYamlEntry)
  90. if __name__ == "__main__":
  91. main()