Skip to content

Instantly share code, notes, and snippets.

@mlazzarotto
Forked from dgarros/duplicate_ip.py
Last active October 12, 2023 17:56
Show Gist options
  • Save mlazzarotto/1c3061957ab27706088b735c5679ce02 to your computer and use it in GitHub Desktop.
Save mlazzarotto/1c3061957ab27706088b735c5679ce02 to your computer and use it in GitHub Desktop.
Netbox Report Duplicate IP - Netbox 3.6+ compatible
from django.db.models import Q
from extras.reports import Report
from collections import defaultdict
from dcim.models import Device
from ipam.models import IPAddress
from ipam.choices import IPAddressRoleChoices
class UniqueIPReport(Report):
description = "Validate that we don't have an IP address allocated twice in the network"
def test_unique_ip(self):
already_found = []
for ip in IPAddress.objects.exclude(Q(role=IPAddressRoleChoices.ROLE_ANYCAST) | Q(role=IPAddressRoleChoices.ROLE_VIP)):
if ip.address in already_found:
continue
elif not ip.interface:
continue
duplicates = ip.get_duplicates()
real_dup = 0
for duplicate in duplicates:
if duplicate.interface:
real_dup +=1
if real_dup != 0:
already_found.append(ip.address)
msg = "has %s duplicate ips" % real_dup
self.log_failure( ip, msg )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment