netbox-export-to-google.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env python
  2. from googleapiclient.discovery import build
  3. from google.auth.transport.requests import Request
  4. from google.oauth2.credentials import Credentials
  5. import pynetbox
  6. import os
  7. import CLEUCreds # type: ignore
  8. from cleu.config import Config as C # type: ignore
  9. CREDS_FILE = "gs_token.json"
  10. SHEET_ID = "1kKOqbK_y3l6Ume-MSkLg1nTVbw571mcq3EOQw5IWuWQ"
  11. def main() -> None:
  12. """Export NetBox IP address data to a Google Sheet"""
  13. global creds, SHEET_ID
  14. # Connect to NetBox
  15. nb = pynetbox.api(C.NETBOX_SERVER, CLEUCreds.NETBOX_API_TOKEN)
  16. # Get all IP addresses from NetBox
  17. try:
  18. ips = list(nb.ipam.ip_addresses.all())
  19. except Exception as e:
  20. print(f"ERROR: Failed to get IPs from NetBox: {e}")
  21. exit(1)
  22. new_values = []
  23. headers = [
  24. "Address",
  25. "VRF",
  26. "Status",
  27. "Role",
  28. "Tenant",
  29. "Assigned",
  30. "DNS name",
  31. "Description",
  32. "ID",
  33. "Tenant Group",
  34. "NAT (Inside)",
  35. "NAT (Outside)",
  36. "Comments",
  37. "Tags",
  38. "Created",
  39. "Last updated",
  40. "Interface",
  41. "Parent",
  42. "List of additional CNAMEs",
  43. ]
  44. new_values.append(headers)
  45. for ip in ips:
  46. tenant = None
  47. tenant_group = None
  48. if ip.tenant:
  49. ip.tenant.full_details()
  50. tenant = ip.tenant.name
  51. tenant_group = str(ip.tenant.group)
  52. parent = None
  53. if ip.assigned_object:
  54. if ip.assigned_object_type == "virtualization.vminterface":
  55. parent = ip.assigned_object.virtual_machine.name
  56. elif ip.assigned_object_type == "dcim.interface":
  57. parent = ip.assigned_object.device.name
  58. row = {
  59. "Address": ip.address,
  60. "VRF": str(ip.vrf),
  61. "Status": ip.status.label,
  62. "Role": str(ip.role),
  63. "Tenant": tenant,
  64. "Assigned": ip.assigned_object_id,
  65. "DNS name": ip.dns_name,
  66. "Description": ip.description,
  67. "ID": ip.id,
  68. "Tenant Group": tenant_group,
  69. "NAT (Inside)": str(ip.nat_inside),
  70. "NAT (Outside)": str(ip.nat_outside),
  71. "Comments": ip.comments,
  72. "Tags": str(ip.tags),
  73. "Created": str(ip.created),
  74. "Last updated": str(ip.last_updated),
  75. "Interface": str(ip.assigned_object),
  76. "Parent": parent,
  77. "List of additional CNAMEs": ip.custom_fields["CNAMEs"],
  78. }
  79. new_values.append(list(row.values()))
  80. gs_service = build("sheets", "v4", credentials=creds)
  81. ip_sheet = gs_service.spreadsheets()
  82. try:
  83. (ip_sheet.values().update(spreadsheetId=SHEET_ID, range="IPs!A1:ZZ", body={"values": new_values}, valueInputOption="RAW").execute())
  84. except Exception as e:
  85. print(f"ERROR: Failed to update sheet: {e}")
  86. if __name__ == "__main__":
  87. if not os.path.isfile(CREDS_FILE):
  88. print(f"ERROR: Token file {CREDS_FILE} does not exist! Please re-authenticate this app.")
  89. exit(1)
  90. creds = Credentials.from_authorized_user_file(CREDS_FILE, ["https://www.googleapis.com/auth/spreadsheets"])
  91. if not creds.valid:
  92. creds.refresh(Request())
  93. with open(CREDS_FILE, "w") as fd:
  94. fd.write(creds.to_json())
  95. main()