Sobes.tech
Intern

1. Schrijf een Python-object "switches" in YAML-formaat 2. Transformeer "switches" zodat alleen "cisco" switches overblijven 3. In het commando standardise_macaddr de laatste hostadres in het netwerk "172.16.0.0/28" verkrijgen 4. Parseer "dhcp_snooping_table" naar een lijst van woordenboeken in het volgende formaat: ```python dhcp_snooping_table_parsed = [ { "mac": "00:E9:BC:3F:A6:50", "ip": "100.1.1.6", "interface": "FastEthernet0/20", }, { "mac": "00:E9:22:11:A6:50", "ip": "100.1.1.7", "interface": "FastEthernet0/21", } ] ``` 5. Schrijf een klasse "NetworkSubnet" met 2 methoden: - get_subnet - geeft de eerste beschikbare subnet van de gespecificeerde lengte terug als een IPv4Network-object - show_network_pool - geeft een lijst van beschikbare subnets terug

sobes.tech AI

Antwoord van AI

yaml schakelaars: cisco1: leverancier: cisco model: 2960 ip: 10.0.0.1 cisco2: leverancier: cisco model: 3750 ip: 10.0.0.2 hp1: leverancier: hp model: 1920 ip: 10.0.0.3


---

# Filter alleen cisco schakelaars

```python
import yaml

with open('switches.yaml') as f:
    switches = yaml.safe_load(f)

cisco_schakelaars = {k: v for k, v in switches['switches'].items() if v['leverancier'] == 'cisco'}

Verkrijg het laatste hostadres in het netwerk 172.16.0.0/28

import ipaddress

network = ipaddress.IPv4Network('172.16.0.0/28')
eind_host = str(list(network.hosts())[-1])
print(eind_host)  # 172.16.0.14

Parsing van dhcp_snooping_table naar een lijst van dictionaries

dhcp_snooping_table = '''
00:E9:BC:3F:A6:50 100.1.1.6 FastEthernet0/20
00:E9:22:11:A6:50 100.1.1.7 FastEthernet0/21
'''

regels = dhcp_snooping_table.strip().split('\n')
dhcp_snooping_table_parsed = []
voor regel in regels:
    mac, ip, interface = regel.split()
    dhcp_snooping_table_parsed.append({
        'mac': mac,
        'ip': ip,
        'interface': interface
    })

NetworkSubnet klasse

import ipaddress

class NetworkSubnet:
    def __init__(self, netwerk):
        self.netwerk = ipaddress.IPv4Network(netwerk)
        gebruikte_subnets = []

    def get_subnet(self, prefixlen):
        for subnet in self.netwerk.subnets(new_prefix=prefixlen):
            if subnet not in gebruikte_subnets:
                gebruikte_subnets.append(subnet)
                return subnet
        return None

    def show_network_pool(self):
        alle_subnets = list(self.netwerk.subnets())
        beschikbare = [s for s in alle_subnets if s not in gebruikte_subnets]
        return beschikbare

# Voorbeeld gebruik
net = NetworkSubnet('192.168.0.0/24')
subnet = net.get_subnet(28)
print(subnet)  # 192.168.0.0/28
print(net.show_network_pool())