more robust address detection

This commit is contained in:
Jeffrey C. Ollie 2024-05-16 10:12:31 -05:00
parent 6639b7f0fd
commit ab0fcba048
No known key found for this signature in database
GPG key ID: F936E4DCB7E25F15

View file

@ -42,6 +42,17 @@ def main():
else: else:
name = device.name name = device.name
address = None
if device.primary_ip4 is not None:
address = str(ipaddress.ip_interface(device.primary_ip4.address).ip)
if address is None and device.primary_ip6 is not None:
address = str(ipaddress.ip_interface(device.primary_ip6.address).ip)
if address is None:
print(f"{device.name} does not have a primary IP address")
continue
services = nb.ipam.services.filter(device_id=device.id) services = nb.ipam.services.filter(device_id=device.id)
for service in services: for service in services:
if service.name.lower() in ["ssh", "telnet"]: if service.name.lower() in ["ssh", "telnet"]:
@ -50,9 +61,7 @@ def main():
"name": name, "name": name,
"type": service.name.lower(), "type": service.name.lower(),
"comment": " ".join(name.split("-")), "comment": " ".join(name.split("-")),
"address": str( "address": address,
ipaddress.ip_interface(device.primary_ip4.address).ip
),
"port": port, "port": port,
"username": "jcollie", "username": "jcollie",
"manufacturer": device.device_type.manufacturer.name, "manufacturer": device.device_type.manufacturer.name,
@ -61,7 +70,7 @@ def main():
"class": re.sub(r"[^a-zA-Z0-9_]", "_", name), "class": re.sub(r"[^a-zA-Z0-9_]", "_", name),
} }
print(name, str(ipaddress.ip_interface(device.primary_ip4.address).ip)) print(name, address)
schneider = nb.dcim.manufacturers.get(name="Schneider") schneider = nb.dcim.manufacturers.get(name="Schneider")
if schneider is None: if schneider is None:
@ -80,6 +89,17 @@ def main():
): ):
name = device.name name = device.name
address = None
if device.primary_ip4 is not None:
address = str(ipaddress.ip_interface(device.primary_ip4.address).ip)
if address is None and device.primary_ip6 is not None:
address = str(ipaddress.ip_interface(device.primary_ip6.address).ip)
if address is None:
print(f"{device.name} does not have a primary IP address")
continue
services = nb.ipam.services.filter(device_id=device.id) services = nb.ipam.services.filter(device_id=device.id)
for service in services: for service in services:
if service.name.lower() in ["ssh", "telnet"]: if service.name.lower() in ["ssh", "telnet"]:
@ -88,9 +108,7 @@ def main():
"name": name, "name": name,
"type": service.name.lower(), "type": service.name.lower(),
"comment": " ".join(name.split("-")), "comment": " ".join(name.split("-")),
"address": str( "address": address,
ipaddress.ip_interface(device.primary_ip4.address).ip
),
"port": port, "port": port,
"username": "apc", "username": "apc",
"manufacturer": device.device_type.manufacturer.name, "manufacturer": device.device_type.manufacturer.name,
@ -99,7 +117,7 @@ def main():
"class": re.sub(r"[^a-zA-Z0-9_]", "_", name), "class": re.sub(r"[^a-zA-Z0-9_]", "_", name),
} }
print(name, str(ipaddress.ip_interface(device.primary_ip4.address).ip)) print(name, address)
open(appdir, "w").write(json.dumps(data, indent=2)) open(appdir, "w").write(json.dumps(data, indent=2))