more robust address detection
This commit is contained in:
parent
6639b7f0fd
commit
ab0fcba048
1 changed files with 26 additions and 8 deletions
|
@ -42,6 +42,17 @@ def main():
|
|||
else:
|
||||
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)
|
||||
for service in services:
|
||||
if service.name.lower() in ["ssh", "telnet"]:
|
||||
|
@ -50,9 +61,7 @@ def main():
|
|||
"name": name,
|
||||
"type": service.name.lower(),
|
||||
"comment": " ".join(name.split("-")),
|
||||
"address": str(
|
||||
ipaddress.ip_interface(device.primary_ip4.address).ip
|
||||
),
|
||||
"address": address,
|
||||
"port": port,
|
||||
"username": "jcollie",
|
||||
"manufacturer": device.device_type.manufacturer.name,
|
||||
|
@ -61,7 +70,7 @@ def main():
|
|||
"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")
|
||||
if schneider is None:
|
||||
|
@ -80,6 +89,17 @@ def main():
|
|||
):
|
||||
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)
|
||||
for service in services:
|
||||
if service.name.lower() in ["ssh", "telnet"]:
|
||||
|
@ -88,9 +108,7 @@ def main():
|
|||
"name": name,
|
||||
"type": service.name.lower(),
|
||||
"comment": " ".join(name.split("-")),
|
||||
"address": str(
|
||||
ipaddress.ip_interface(device.primary_ip4.address).ip
|
||||
),
|
||||
"address": address,
|
||||
"port": port,
|
||||
"username": "apc",
|
||||
"manufacturer": device.device_type.manufacturer.name,
|
||||
|
@ -99,7 +117,7 @@ def main():
|
|||
"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))
|
||||
|
||||
|
|
Loading…
Reference in a new issue