Interface¶
netutils.interface
¶
Functions for working with interface.
CCInt
¶
Bases: CharacterClass
Ints must be sorted canonically because '11' < '5'.
Source code in netutils/interface.py
CCSeparator
¶
Bases: CharacterClass
Separators require custom logic, so we sort them by arbitrary weight.
Source code in netutils/interface.py
CCString
¶
Bases: CharacterClass
Strings are sorted lexicographically.
Source code in netutils/interface.py
CharacterClass
¶
Bases: ABC
CharacterClass embodies the state needed to sort interfaces.
Source code in netutils/interface.py
abbreviated_interface_name(interface, addl_name_map=None, addl_reverse_map=None, verify=False)
¶
Function to return an abbreviated representation of the interface name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interface |
str
|
The interface you are attempting to shorten. |
required |
addl_name_map |
Optional[Dict[str, str]]
|
A dict containing key/value pairs that updates the base mapping. Used if an OS has specific differences. e.g. {"Po": "PortChannel"} vs {"Po": "Port-Channel"}. Defaults to None. |
None
|
addl_reverse_map |
Optional[Dict[str, str]]
|
A dict containing key/value pairs that updates the abbreviated mapping. Defaults to None. |
None
|
verify |
bool
|
Whether or not to verify the interface matches a known interface standard. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
str
|
The name of the interface in the abbreviated form. |
Examples:
>>> abbreviated_interface_name("GigabitEthernet1/0/1")
'Gi1/0/1'
>>> abbreviated_interface_name("Eth1")
'Et1'
>>>
Source code in netutils/interface.py
abbreviated_interface_name_list(interfaces, addl_name_map=None, addl_reverse_map=None, verify=False, order=None, reverse=False)
¶
Function to return a list of interface's abbreviated name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interfaces |
List[str]
|
List of interface names you are attempting to abbreviate. |
required |
addl_name_map |
Optional[Dict[str, str]]
|
A dict containing key/value pairs that updates the base mapping. Used if an OS has specific differences. e.g. {"Po": "PortChannel"} vs {"Po": "Port-Channel"}. Defaults to None. |
None
|
addl_reverse_map |
Optional[Dict[str, str]]
|
A dict containing key/value pairs that updates the abbreviated mapping. Defaults to None. |
None
|
verify |
bool
|
Whether or not to verify the interface matches a known interface standard. Defaults to False. |
False
|
order |
Optional[str]
|
Determines what order the list of interfaces should be returned in. Defaults to None. |
None
|
reverse |
bool
|
Specify if the order of the list should be reversed when setting an order. Defaults to None. |
False
|
Returns:
Type | Description |
---|---|
List[str]
|
List of the interfaces in their abbreviated form. |
Raises:
Type | Description |
---|---|
ValueError
|
Raised if any interface name in list cannot be converted to its abbreviated form and verify parameter is set to true. |
Examples:
>>> from netutils.interface import abbreviated_interface_name_list
>>> abbreviated_interface_name_list(["GigabitEthernet1/0/1", "GigabitEthernet1/0/2", "Ethernet1"])
['Gi1/0/1', 'Gi1/0/2', 'Et1']
>>> abbreviated_interface_name_list(['GigabitEthernet1/0/1', 'Port-channel40', 'Loopback10'])
['Gi1/0/1', 'Po40', 'Lo10']
>>>
Source code in netutils/interface.py
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
|
canonical_interface_name(interface, addl_name_map=None, verify=False)
¶
Function to return an interface's canonical name (fully expanded name).
Use of explicit matches used to indicate a clear understanding on any potential match. Regex and other looser matching methods were not implemented to avoid false positive matches. As an example, it would make sense to do "[P|p][O|o]" which would incorrectly match PO = POS and Po = Port-channel, leading to a false positive, not easily troubleshot, found, or known.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interface |
str
|
The interface you are attempting to expand. |
required |
addl_name_map |
Optional[Dict[str, str]]
|
A dict containing key/value pairs that updates the base mapping. Used if an OS has specific differences. e.g. {"Po": "PortChannel"} vs {"Po": "Port-Channel"}. Defaults to None. |
None
|
verify |
bool
|
Whether or not to verify the interface matches a known interface standard. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
str
|
The name of the interface in the long form. |
Examples:
>>> from netutils.interface import canonical_interface_name
>>> canonical_interface_name("Gi1/0/1")
'GigabitEthernet1/0/1'
>>> canonical_interface_name("Eth1")
'Ethernet1'
>>>
Source code in netutils/interface.py
canonical_interface_name_list(interfaces, addl_name_map=None, verify=False, order=None, reverse=False)
¶
Function to return a list of interface's canonical name (fully expanded name).
Use of explicit matches used to indicate a clear understanding on any potential
match. Regex and other looser matching methods were not implemented to avoid false
positive matches. As an example, it would make sense to do [P|p][O|o]
which would
incorrectly match PO = POS and Po = Port-channel, leading to a false positive, not
easily troubleshot, found, or known.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interfaces |
List[str]
|
List of interfaces you are attempting to expand. |
required |
addl_name_map |
Optional[Dict[str, str]]
|
A dict containing key/value pairs that updates the base mapping. Used if an OS has specific differences. e.g. {"Po": "PortChannel"} vs {"Po": "Port-Channel"}. Defaults to None. |
None
|
verify |
bool
|
Whether or not to verify the interface matches a known interface standard. Defaults to False. |
False
|
order |
Optional[str]
|
Determines what order the list of interfaces should be returned in. Defaults to None. |
None
|
reverse |
bool
|
Specify if the order of the list should be reversed when setting an order. Defaults to None. |
False
|
Returns:
Type | Description |
---|---|
List[str]
|
List of the interfaces in their long form. |
Raises:
Type | Description |
---|---|
ValueError
|
Raised if any interface name in list cannot be converted to its long form and verify parameter is set to true. |
Examples:
>>> from netutils.interface import canonical_interface_name_list
>>> canonical_interface_name_list(["Gi1/0/1", "Gi1/0/2", "Eth1"])
['GigabitEthernet1/0/1', 'GigabitEthernet1/0/2', 'Ethernet1']
>>> canonical_interface_name_list(["Gi1/0/1", "Po40", "Lo10"])
['GigabitEthernet1/0/1', 'Port-channel40', 'Loopback10']
>>>
Source code in netutils/interface.py
interface_range_compress(interface_list)
¶
Function which takes interfaces and return interface ranges.
Whitespace and special characters are ignored in the input. Input must contain only interfaces, there is no check against correct interface names! Also interface names must use the same abbreviation! E.g. Gi =! GigabitEthernet
Examples:
>>> interface_range_compress(["Gi1/0/1", "Gi1/0/2", "Gi1/0/3", "Gi1/0/5"])
['Gi1/0/1-3', 'Gi1/0/5']
>>> interface_range_compress(["Gi0/1", "Gi0/2", "Gi0/4", "Gi1/0", "Gi1/1"])
['Gi0/1-2', 'Gi0/4', 'Gi1/0-1']
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interface_list |
List[str]
|
list of interfaces |
required |
Returns:
Type | Description |
---|---|
List[str]
|
list of interface ranges |
Source code in netutils/interface.py
interface_range_expansion(interface_pattern)
¶
Expand interface pattern into a list of interfaces.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interface_pattern |
str
|
The string pattern that will be parsed to create the list of interfaces. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
Contains the expanded list of interfaces. |
Examples:
>>> from netutils.interface import interface_range_expansion
>>> interface_range_expansion("Gi0/[1-4]")
['Gi0/1', 'Gi0/2', 'Gi0/3', 'Gi0/4']
>>> interface_range_expansion("FastEthernet[1-2]/0/[10-15]")
['FastEthernet1/0/10', 'FastEthernet1/0/11', 'FastEthernet1/0/12', 'FastEthernet1/0/13', 'FastEthernet1/0/14', 'FastEthernet1/0/15', 'FastEthernet2/0/10', 'FastEthernet2/0/11', 'FastEthernet2/0/12', 'FastEthernet2/0/13', 'FastEthernet2/0/14', 'FastEthernet2/0/15']
Source code in netutils/interface.py
sort_interface_list(interfaces)
¶
This function sorts and cleans a list of interfaces.
Note that a new list of interfaces is returned and that duplicates nodes are removed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interfaces |
List[str]
|
A list of interfaces to be sorted. The input list is not mutated by this function. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
A new sorted, unique list elements from the input. |
Examples:
>>> sort_interface_list(["Gi1/0/1", "Gi1/0/3", "Gi1/0/3.100", "Gi1/0/2", "Gi1/0/2.50", "Gi2/0/2", "Po40", "Po160", "Lo10"])
['Gi1/0/1', 'Gi1/0/2', 'Gi1/0/2.50', 'Gi1/0/3', 'Gi1/0/3.100', 'Gi2/0/2', 'Lo10', 'Po40', 'Po160']
>>> sort_interface_list(['GigabitEthernet1/0/1', 'GigabitEthernet1/0/3', 'GigabitEthernet1/0/2', "GigabitEthernet3/0/5", 'GigabitEthernet3/0/7', 'GigabitEthernet2/0/8.5', 'Port-channel40', 'Vlan20', 'Loopback10'])
['GigabitEthernet1/0/1', 'GigabitEthernet1/0/2', 'GigabitEthernet1/0/3', 'GigabitEthernet2/0/8.5', 'GigabitEthernet3/0/5', 'GigabitEthernet3/0/7', 'Loopback10', 'Port-channel40', 'Vlan20']
Source code in netutils/interface.py
split_interface(interface)
¶
Split an interface name based on first digit, slash, or space match.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interface |
str
|
The interface you are attempting to split. |
required |
Returns:
Type | Description |
---|---|
Tuple[str, str]
|
The split between the name of the interface the value. |
Examples:
>>> from netutils.interface import split_interface
>>> split_interface("GigabitEthernet1/0/1")
('GigabitEthernet', '1/0/1')
>>> split_interface("Eth1")
('Eth', '1')
>>>