Sobes.tech
Intern

1. Scrivere un oggetto Python "switches" in formato YAML 2. Trasformare "switches" in modo che rimangano solo gli switch "cisco" 3. Nella comando standardise_macaddr ottenere l'ultimo indirizzo host nella rete "172.16.0.0/28" 4. Analizzare "dhcp_snooping_table" in una lista di dizionari nel seguente formato: ```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. Scrivere una classe "NetworkSubnet" che abbia 2 metodi: - get_subnet - restituisce la prima sottorete disponibile di lunghezza specificata come oggetto della classe IPv4Network - show_network_pool - restituisce una lista di sottoreti disponibili

sobes.tech AI

Risposta dell'AI

yaml interruttori: cisco1: fornitore: cisco modello: 2960 ip: 10.0.0.1 cisco2: fornitore: cisco modello: 3750 ip: 10.0.0.2 hp1: fornitore: hp modello: 1920 ip: 10.0.0.3


---

# Filtraggio solo switch cisco

```python
import yaml

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

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

Ottenere l'indirizzo host finale nella rete 172.16.0.0/28

import ipaddress

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

Analisi della tabella dhcp_snooping in una lista di dizionari

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
'''

linee = dhcp_snooping_table.strip().split('\n')
dhcp_snooping_table_parsed = []
per linea in linee:
    mac, ip, interface = linea.split()
    dhcp_snooping_table_parsed.append({
        'mac': mac,
        'ip': ip,
        'interface': interface
    })

Classe NetworkSubnet

import ipaddress

class NetworkSubnet:
    def __init__(self, rete):
        self.rete = ipaddress.IPv4Network(rete)
        sottoreti_usate = []

    def get_subnet(self, prefisso):
        for sottorete in self.rete.subnets(new_prefix=prefisso):
            if sottorete not in sottoreti_usate:
                sottoreti_usate.append(sottorete)
                return sottorete
        return None

    def show_network_pool(self):
        tutte_le_sottoreti = list(self.rete.subnets())
        disponibili = [s for s in tutte_le_sottoreti if s not in sottoreti_usate]
        return disponibili

# Esempio di utilizzo
net = NetworkSubnet('192.168.0.0/24')
sottorete = net.get_subnet(28)
print(sottorete)  # 192.168.0.0/28
print(net.show_network_pool())