Actualización de proyecto
This commit is contained in:
@ -26,7 +26,7 @@ for index, device_a in enumerate(devices):
|
|||||||
print(f"Found match! {device_a['name']} for both {device_a['ip']} and {device_b['ip']}")
|
print(f"Found match! {device_a['name']} for both {device_a['ip']} and {device_b['ip']}")
|
||||||
print("----- Comparison of device names completed")
|
print("----- Comparison of device names completed")
|
||||||
|
|
||||||
print("\n----- Create table of arbitrary 'standard' versions for each vendor:os --------------------")
|
print("\n----- Create table of arbitrary 'standard' versions for each vendor:os - Resumen de versiones --------------------")
|
||||||
standard_versions = dict()
|
standard_versions = dict()
|
||||||
for device in devices:
|
for device in devices:
|
||||||
vendor_os = device["vendor"] + ":" + device["os"]
|
vendor_os = device["vendor"] + ":" + device["os"]
|
||||||
|
|||||||
@ -1,6 +1,9 @@
|
|||||||
from ipaddress import IPv4Address
|
from ipaddress import IPv4Address
|
||||||
|
|
||||||
# REMOVE CAPITALIZATION ISSUES
|
print("\nNormalization tests\n")
|
||||||
|
|
||||||
|
# ----- STRING NORMALIZATION --------------------
|
||||||
|
|
||||||
device_1 = {
|
device_1 = {
|
||||||
"name": "sbx-n9kv-ao",
|
"name": "sbx-n9kv-ao",
|
||||||
"vendor": "cisco",
|
"vendor": "cisco",
|
||||||
@ -25,11 +28,21 @@ if (
|
|||||||
and device_1["model"].lower() == device_2["model"].lower()
|
and device_1["model"].lower() == device_2["model"].lower()
|
||||||
and device_1["os"].lower() == device_2["os"].lower()
|
and device_1["os"].lower() == device_2["os"].lower()
|
||||||
):
|
):
|
||||||
print("--\nString lower() normalization works")
|
print("--String lower() normalization works")
|
||||||
else:
|
else:
|
||||||
print("--\nString lower() normalization failed")
|
print("--String lower() normalization failed")
|
||||||
|
|
||||||
# MAC ADDRESS NORMALIZATION
|
if (
|
||||||
|
device_1["name"].casefold() == device_2["name"].casefold()
|
||||||
|
and device_1["vendor"].casefold() == device_2["vendor"].casefold()
|
||||||
|
and device_1["model"].casefold() == device_2["model"].casefold()
|
||||||
|
and device_1["os"].casefold() == device_2["os"].casefold()
|
||||||
|
):
|
||||||
|
print("--String casefold() normalization works")
|
||||||
|
else:
|
||||||
|
print("--String casefold() normalization failed")
|
||||||
|
|
||||||
|
# ----- MAC ADDRESS NORMALIZATION --------------------
|
||||||
|
|
||||||
mac_addr_colons = "a0:b1:c2:d3:e4:f5"
|
mac_addr_colons = "a0:b1:c2:d3:e4:f5"
|
||||||
mac_addr_caps = "A0:B1:C2:D3:E4:F5"
|
mac_addr_caps = "A0:B1:C2:D3:E4:F5"
|
||||||
@ -45,12 +58,6 @@ def normalize(mac):
|
|||||||
return mac.lower().replace(":", "").replace(".", "").replace("-", "")
|
return mac.lower().replace(":", "").replace(".", "").replace("-", "")
|
||||||
|
|
||||||
|
|
||||||
# print("MAC address with colons: ", normalize(mac_addr_colons))
|
|
||||||
# print("MAC address with caps: ", normalize(mac_addr_caps))
|
|
||||||
# print("MAC address with dots: ", normalize(mac_addr_dots))
|
|
||||||
# print("MAC address with hyphens: ", normalize(mac_addr_hyphens))
|
|
||||||
# print("MAC address with wacky: ", normalize(mac_addr_wacky))
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
normalize(mac_addr_colons)
|
normalize(mac_addr_colons)
|
||||||
== normalize(mac_addr_caps)
|
== normalize(mac_addr_caps)
|
||||||
@ -59,16 +66,20 @@ if (
|
|||||||
== normalize(mac_addr_wacky)
|
== normalize(mac_addr_wacky)
|
||||||
== mac_addr_norm
|
== mac_addr_norm
|
||||||
):
|
):
|
||||||
print("--\nMAC address normalization works")
|
print("--MAC address normalization works")
|
||||||
else:
|
else:
|
||||||
print("--\nMAC address normalization failed")
|
print("--MAC address normalization failed")
|
||||||
|
|
||||||
# IP ADDRESS NORMALIZATION
|
# ----- IP ADDRESS NORMALIZATION --------------------
|
||||||
|
|
||||||
ip_addr_1 = "10.0.1.1"
|
ip_addr_1 = "10.0.1.1"
|
||||||
ip_addr_2 = "10.000.001.001"
|
ip_addr_2 = "10.000.001.001"
|
||||||
|
ip_addr_3 = "010.00.01.001"
|
||||||
|
|
||||||
if IPv4Address(ip_addr_1) == IPv4Address(ip_addr_2):
|
if IPv4Address(ip_addr_1) == IPv4Address(ip_addr_2) == IPv4Address(ip_addr_3):
|
||||||
print("--\nIP address normalization works")
|
print("--IP address normalization works")
|
||||||
else:
|
else:
|
||||||
print("--\nIP address normalization failed")
|
print("--IP address normalization failed")
|
||||||
|
|
||||||
|
# ----- DEVICE DATA NORMALIZATION --------------------
|
||||||
|
# (see NAPALM examples)
|
||||||
@ -1,21 +1,49 @@
|
|||||||
from util.create_utils import create_devices
|
from util.create_utils import create_devices
|
||||||
from tabulate import tabulate
|
|
||||||
from operator import itemgetter
|
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
# --- Main program --------------------------------------------
|
# --- Main program --------------------------------------------
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
t = (1, 2, 3)
|
devices = tuple(create_devices(num_devices=4, num_subnets=1))
|
||||||
t += (4, 5)
|
|
||||||
print(t)
|
|
||||||
|
|
||||||
devices = create_devices(num_devices=4, num_subnets=1)
|
|
||||||
devices_tuple = tuple(devices)
|
|
||||||
|
|
||||||
print("\n----- LIST OF DEVICES --------------------")
|
|
||||||
pprint(devices)
|
|
||||||
|
|
||||||
print("\n----- TUPLE OF DEVICES --------------------")
|
print("\n----- TUPLE OF DEVICES --------------------")
|
||||||
pprint(devices_tuple)
|
pprint(devices)
|
||||||
|
|
||||||
|
print("\n----- DEVICE AS TUPLE --------------------")
|
||||||
|
device = ("sbx-n9kv-ao", "cisco", "Nexus9000 C9300v Chassis", "nxos", "10.0.1.1")
|
||||||
|
|
||||||
|
print(" name:", device[0])
|
||||||
|
print("vendor:", device[1])
|
||||||
|
print(" model:", device[2])
|
||||||
|
print(" os:", device[3])
|
||||||
|
print(" ip:", device[4])
|
||||||
|
|
||||||
|
print("\n----- DEVICE AS NAMED TUPLE --------------------")
|
||||||
|
Device = namedtuple('Device', ['name', 'vendor', 'model', 'os', 'ip'])
|
||||||
|
device = Device("sbx-n9kv-ao", "cisco", "Nexus9000 C9300v Chassis", "nxos", "10.0.1.1")
|
||||||
|
|
||||||
|
print(" name:", device.name)
|
||||||
|
print("vendor:", device.vendor)
|
||||||
|
print(" model:", device.model)
|
||||||
|
print(" os:", device.os)
|
||||||
|
print(" ip:", device.ip)
|
||||||
|
|
||||||
|
print("\n----- PPRINT OF DEVICE NAMED TUPLE --------------------")
|
||||||
|
pprint(device)
|
||||||
|
|
||||||
|
print("\n----- CONVERT DEVICES TO NAMED TUPLES --------------------")
|
||||||
|
devices = create_devices(num_devices=10, num_subnets=2, random_ip=True)
|
||||||
|
devices_as_namedtuples = list()
|
||||||
|
for device in devices:
|
||||||
|
Device = namedtuple("Device", device.keys())
|
||||||
|
devices_as_namedtuples.append(Device(**device))
|
||||||
|
|
||||||
|
print("\n----- PPRINT VERSION OF DEVICES AS NAMED TUPLES --------------------")
|
||||||
|
pprint(devices_as_namedtuples)
|
||||||
|
|
||||||
|
print("\n----- NICELY FORMATTED --------------------\n")
|
||||||
|
print(" NAME VENDOR : OS IP ADDRESS")
|
||||||
|
print(" ----- ------- ----- --------------")
|
||||||
|
for device in devices_as_namedtuples:
|
||||||
|
print(f'{device.name:>7} {device.vendor:>10} : {device.os:<6} {device.ip:<15}')
|
||||||
@ -0,0 +1,46 @@
|
|||||||
|
available_ips = set()
|
||||||
|
used_ips = set()
|
||||||
|
|
||||||
|
|
||||||
|
def print_ips():
|
||||||
|
|
||||||
|
available_ips_list = list(available_ips)
|
||||||
|
used_ips_list = list(used_ips)
|
||||||
|
|
||||||
|
if len(available_ips_list) > len(used_ips_list):
|
||||||
|
for _ in range(0, len(available_ips_list) - len(used_ips_list)):
|
||||||
|
used_ips_list.append("")
|
||||||
|
elif len(available_ips_list) < len(used_ips_list):
|
||||||
|
for _ in range(0, len(used_ips_list) - len(available_ips_list)):
|
||||||
|
available_ips_list.append("")
|
||||||
|
|
||||||
|
print()
|
||||||
|
print(" available used")
|
||||||
|
print(" ---------------- -----------------")
|
||||||
|
for available_ip, used_ip in zip(available_ips_list, used_ips_list):
|
||||||
|
print(f" {available_ip:>16} {used_ip:<16}")
|
||||||
|
|
||||||
|
|
||||||
|
for index in range(180, 200):
|
||||||
|
available_ips.add("10.0.1." + str(index))
|
||||||
|
|
||||||
|
while True:
|
||||||
|
print_ips()
|
||||||
|
ip_address = input("\nEnter IP address to allocate: ")
|
||||||
|
if not ip_address:
|
||||||
|
print("\nExiting 'sets' application")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
if ip_address in available_ips:
|
||||||
|
|
||||||
|
print(f"-- allocated IP address: {ip_address}")
|
||||||
|
available_ips.remove(ip_address)
|
||||||
|
used_ips.add(ip_address)
|
||||||
|
|
||||||
|
print_ips()
|
||||||
|
|
||||||
|
if len(available_ips.intersection(used_ips)) > 0:
|
||||||
|
print("\n-- ERROR! one or more IPs in both sets")
|
||||||
|
|
||||||
|
else:
|
||||||
|
print("-- IP address not found in available IPs\n")
|
||||||
Reference in New Issue
Block a user