Intern
1. Write a Python object "switches" in YAML format. 2. Transform "switches" so that it contains only "cisco" switches. 3. In the command standardise_macaddr, get the last host address in the network "172.16.0.0/28". 4. Parse "dhcp_snooping_table" into a list of dictionaries of the following form: ```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. Write a class "NetworkSubnet" that has 2 methods: - get_subnet - returns the first available subnet of the specified length as an IPv4Network class object. - show_network_pool - returns a list of available subnets.
sobes.tech AI
Answer from AI
yaml switches: cisco1: vendor: cisco model: 2960 ip: 10.0.0.1 cisco2: vendor: cisco model: 3750 ip: 10.0.0.2 hp1: vendor: hp model: 1920 ip: 10.0.0.3
---
# Filtering only cisco switches
```python
import yaml
with open('switches.yaml') as f:
switches = yaml.safe_load(f)
cisco_switches = {k: v for k, v in switches['switches'].items() if v['vendor'] == 'cisco'}
Getting the last host address in the 172.16.0.0/28 network
import ipaddress
network = ipaddress.IPv4Network('172.16.0.0/28')
last_host = str(list(network.hosts())[-1])
print(last_host) # 172.16.0.14
Parsing dhcp_snooping_table into a list of 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
'''
lines = dhcp_snooping_table.strip().split('\n')
dhcp_snooping_table_parsed = []
for line in lines:
mac, ip, interface = line.split()
dhcp_snooping_table_parsed.append({
'mac': mac,
'ip': ip,
'interface': interface
})
Class NetworkSubnet
import ipaddress
class NetworkSubnet:
def __init__(self, network):
self.network = ipaddress.IPv4Network(network)
self.used_subnets = []
def get_subnet(self, prefixlen):
for subnet in self.network.subnets(new_prefix=prefixlen):
if subnet not in self.used_subnets:
self.used_subnets.append(subnet)
return subnet
return None
def show_network_pool(self):
all_subnets = list(self.network.subnets())
available = [s for s in all_subnets if s not in self.used_subnets]
return available
# Example usage
net = NetworkSubnet('192.168.0.0/24')
subnet = net.get_subnet(28)
print(subnet) # 192.168.0.0/28
print(net.show_network_pool())
sobes.tech AI
Answer from AI
- Example of a
switchesobject in YAML format:
switches:
- name: switch1
vendor: cisco
ip: 192.168.1.1
- name: switch2
vendor: juniper
ip: 192.168.1.2
- name: switch3
vendor: cisco
ip: 192.168.1.3
- Transformation to keep only Cisco switches:
import yaml
with open('switches.yaml') as f:
data = yaml.safe_load(f)
cisco_switches = [sw for sw in data['switches'] if sw['vendor'].lower() == 'cisco']
print(cisco_switches)
- Getting the last host address in the network "172.16.0.0/28":
import ipaddress
network = ipaddress.ip_network('172.16.0.0/28')
last_host = network.broadcast_address - 1
print(last_host) # 172.16.0.14
- Parsing
dhcp_snooping_tableinto a list of dictionaries:
dhcp_snooping_table = '''
MacAddress IpAddress Interface
00:E9:BC:3F:A6:50 100.1.1.6 FastEthernet0/20
00:E9:22:11:A6:50 100.1.1.7 FastEthernet0/21
'''
lines = dhcp_snooping_table.strip().split('\n')[1:] # skip header
parsed = []
for line in lines:
mac, ip, interface = line.split()
parsed.append({"mac": mac, "ip": ip, "interface": interface})
print(parsed)
NetworkSubnetclass with required methods:
from ipaddress import IPv4Network
class NetworkSubnet:
def __init__(self, aggregate_network: str):
self.aggregate = IPv4Network(aggregate_network)
self.available_subnets = [self.aggregate]
def get_subnet(self, length: int) -> IPv4Network:
for i, subnet in enumerate(self.available_subnets):
if subnet.prefixlen <= length:
# Split the subnet into smaller ones
subnets = list(subnet.subnets(new_prefix=length))
if subnets:
allocated = subnets[0]
# Update available subnets pool
remaining = subnets[1:] + self.available_subnets[:i] + self.available_subnets[i+1:]
self.available_subnets = remaining
return allocated
raise ValueError("No available subnets of the required size")
def show_network_pool(self) -> list[IPv4Network]:
return self.available_subnets
# Example usage
net = NetworkSubnet(aggregate_network="172.16.0.0/24")
net_1 = net.get_subnet(length=26)
print(f"{net_1=}") # 172.16.0.0/26
print(net.get_subnet(length=25)) # 172.16.0.64/25
print(net.get_subnet(length=30)) # 172.16.0.192/30
print(net.show_network_pool())