VLAN¶
netutils.vlan
¶
Functions for working with VLANs.
vlanconfig_to_list(vlan_config)
¶
Given an IOS-like vlan list of configurations, return the list of VLANs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vlan_config |
str
|
IOS-like vlan list of configurations. |
required |
Returns:
Type | Description |
---|---|
List[int]
|
Sorted string list of integers according to IOS-like vlan list rules |
Examples:
>>> vlan_config = '''switchport trunk allowed vlan 1025,1069-1072,1114,1173-1181,1501,1502'''
>>> vlanconfig_to_list(vlan_config)
[1025, 1069, 1070, 1071, 1072, 1114, 1173, 1174, 1175, 1176, 1177, 1178, 1179, 1180, 1181, 1501, 1502]
>>>
Source code in netutils/vlan.py
vlanlist_to_config(vlan_list, first_line_len=48, other_line_len=44, min_grouping_size=3, return_empty=False)
¶
Given a List of VLANs, build the IOS-like vlan list of configurations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vlan_list |
List[int]
|
Unsorted list of vlan integers. |
required |
first_line_len |
int
|
The maximum length of the line of the first element of within the return list. Defaults to 48. |
48
|
other_line_len |
int
|
The maximum length of the line of all other elements of within the return list. Defaults to 44. |
44
|
min_grouping_size |
int
|
The minimum consecutive VLANs to aggregate with a hyphen. Defaults to Cisco's minimum grouping size of 3. |
3
|
return_empty |
bool
|
Whether or not to return an empty list instead of an |
False
|
Returns:
Type | Description |
---|---|
List[str]
|
Sorted string list of integers according to IOS-like vlan list rules |
Examples:
>>> from netutils.vlan import vlanlist_to_config
>>> vlanlist_to_config([1, 2, 3, 5, 6, 1000, 1002, 1004, 1006, 1008, 1010, 1012, 1014, 1016, 1018])
['1-3,5,6,1000,1002,1004,1006,1008,1010,1012,1014', '1016,1018']
>>> vlanlist_to_config([1,3,5,6,100,101,102,103,104,105,107,109], min_grouping_size=2)
['1,3,5-6,100-105,107,109']
>>> vlanlist_to_config([1,3,5,6,100,101,102,103,104,105,107,109], min_grouping_size=1)
['1,3,5,6,100,101,102,103,104,105,107,109']
>>> vlan_list = [1, 2, 3, 5, 6, 1000, 1002, 1004, 1006, 1008, 1010, 1012, 1014, 1016, 1018]
>>> for index, vlan in enumerate(vlanlist_to_config(vlan_list)):
... if index == 0:
... print(f"switchport trunk allowed vlan {vlan}")
... else:
... print(f"switchport trunk allowed vlan add {vlan}")
...
switchport trunk allowed vlan 1-3,5,6,1000,1002,1004,1006,1008,1010,1012,1014
switchport trunk allowed vlan add 1016,1018