Sobes.tech
Intern

1. Python obyekti "switches" YAML formatida yozing 2. "switches" ni o'zgartiring, shunda faqat "cisco" switchlar qoladi 3. standardise_macaddr buyrug'ida, "172.16.0.0/28" tarmog'ida oxirgi host manzilini oling 4. "dhcp_snooping_table" ni quyidagi formatdagi lug'atlar ro'yxatiga tahlil qiling: ```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. "NetworkSubnet" nomli sinf yozing, uning 2 metodi bo'lsin: - get_subnet - belgilangan uzunlikdagi birinchi mavjud subnetni IPv4Network sinfi obyekti sifatida qaytaradi - show_network_pool - mavjud subnetlar ro'yxatini qaytaradi

sobes.tech AI

AIdan javob

yaml kuchaytlar: cisco1: ta'minotchi: cisco model: 2960 ip: 10.0.0.1 cisco2: ta'minotchi: cisco model: 3750 ip: 10.0.0.2 hp1: ta'minotchi: hp model: 1920 ip: 10.0.0.3


---

# Faqat cisco switchlarini filtrlash

```python
import yaml

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

cisco_switchlar = {k: v for k, v in switches['switches'].items() if v['ta'minotchi'] == 'cisco'}

172.16.0.0/28 tarmog'ida oxirgi host manzilini olish

import ipaddress

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

dhcp_snooping_table jadvalini lug'atlar ro'yxatiga tahlil qilish

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

qatorlar = dhcp_snooping_table.strip().split('\n')
dhcp_snooping_table_parsed = []
har qator uchun:
    mac, ip, interface = qator.split()
    dhcp_snooping_table_parsed.append({
        'mac': mac,
        'ip': ip,
        'interface': interface
    })

NetworkSubnet sinfi

import ipaddress

class NetworkSubnet:
    def __init__(self, tarmoq):
        self.tarmoq = ipaddress.IPv4Network(tarmoq)
        ishlatilgan_subnetlar = []

    def get_subnet(self, prefiks):
        for subnet in self.tarmoq.subnets(new_prefix=prefiks):
            if subnet not in ishlatilgan_subnetlar:
                ishlatilgan_subnetlar.append(subnet)
                return subnet
        return None

    def show_network_pool(self):
        barcha_subnetlar = list(self.tarmoq.subnets())
        mavjud = [s for s in barcha_subnetlar if s not in ishlatilgan_subnetlar]
        return mavjud

# Misol uchun foydalanish
net = NetworkSubnet('192.168.0.0/24')
subnet = net.get_subnet(28)
print(subnet)  # 192.168.0.0/28
print(net.show_network_pool())