Package: netports
netportsPython tools for managing ranges of VLANs, TCP/UDP ports, IP protocols, Interfaces
Recommended for scripting in telecommunications networks.RequirementsPython >=3.8InstallationInstall the package from pypi.org releasepipinstallnetportsor install the package from github.com releasepipinstallhttps://github.com/vladimirs-git/netports/archive/refs/tags/0.12.1.tar.gzor install the package from github.com repositorypipinstallgit+https://github.com/vladimirs-git/netports@0.12.1TCP/UDP portscheck_port()check_port(port, strict)Check TCP/UDP port in the range 1 to 65535.ParameterTypeDescriptionportintThe TCP/UDP port that needs to be checked.strictboolTrue - raise NetportsValueError if the port is invalid, False - return False if the port is invalid. Default isFalse.ReturnboolTrue - If the port is in the valid range of 1 to 65535, False - otherwise.RaisesTypeErrorIf the port is not integer.NetportsValueErrorIf strict=True and the port is outside the valid range.check_port()check_ports(ports, strict)heck TCP/UDP ports in the range 1 to 65535.ParameterTypeDescriptionportsList[int]The TCP/UDP ports that needs to be checkedstrictboolTrue - raise NetportsValueError if any in the ports is invalid, False - return False if the port is invalid. Default isFalse.ReturnboolTrue - if all ports is in the valid range of 1 to 65535, False - otherwise.RaisesTypeErrorIf any in the ports is not integer.NetportsValueErrorIf strict=True and any in the ports is outside the valid range.itcp()itcp(items, verbose, all)Integer TCP/UDP Ports. Sorting TCP/UDP ports and removing duplicatesParameterTypeDescriptionitemsstr, List[int], List[str]Range of TCP/UDP ports, can be unsorted and with duplicatesverboseboolTrue - all ports in verbose mode: [1, 2, …, 65535], False - all ports in brief mode: [-1], to save RAM (default)allboolTrue - Returns all TCP/UDP ports: [1, 2, …, 65535], or [-1] for verbose=FalseReturnList[int]of unique sorted TCP/UDP portsRaisesNetportsValueErrorif TCP/UDP ports are outside valid range 1…65535stcp()stcp(items, verbose, all)String TCP/UDP ports. Sorting TCP/UDP ports and removing duplicatesParameterTypeDescriptionitemsstr, List[int], List[str]Range of TCP/UDP ports, can be unsorted and with duplicatesverboseboolTrue - all ports in verbose mode: [1, 2, …, 65535], False - all ports in brief mode: [-1], to save RAM (default)allboolTrue - Returns all TCP/UDP ports: “1-65535”Returnstrof unique sorted TCP/UDP portsRaisesNetportsValueErrorif TCP/UDP ports are outside valid range 1…65535Examples./examples/tcp_udp.pyVLAN IDsivlan()ivlan(items, verbose, all, splitter, range_splitter, platform)Sorting integer VLAN IDs and removing duplicatesParameterTypeDescriptionitemsstr, List[int], List[str]Range of VLANs, can be unsorted and with duplicatesverboseboolTrue - all VLAN IDs in verbose mode: [1, 2, …, 65535], False - all VLAN IDs in brief mode: [-1], to save RAM (default)allboolTrue - Returns all VLAN IDs: [1, 2, …, 4094], or [-1] for verbose=FalsesplitterstrSeparator character between items, by default “,”range_splitterstrSeparator between min and max numbers in range, by default “-”platformstrSetsplitterandrange_splitterto platform specific values. Defined: “cisco” (Cisco IOS), “hpe” (Hewlett Packard Enterprise).ReturnList[int]of unique sorted VLANsRaisesNetportsValueErrorif VLANs are outside valid range 1…4094svlan()svlan(items, verbose, all, splitter, range_splitter, platform)Sorting string VLANs and removing duplicatesParameterTypeDescriptionitemsstr, List[int], List[str]Range of VLANs, can be unsorted and with duplicatesverboseboolTrue - all VLAN IDs in verbose mode: [1, 2, …, 65535], False - all VLAN IDs in brief mode: [-1], to save RAM (default)allboolTrue - Returns all VLAN IDs: “1-4094”splitterstrSeparator character between items, by default “,”range_splitterstrSeparator between min and max numbers in range, by default “-”platformstrSetsplitterandrange_splitterto platform specific values. Defined: “cisco” (Cisco IOS), “hpe” (Hewlett Packard Enterprise).Returnstrof unique sorted VLANsRaisesNetportsValueErrorif VLANs are outside valid range 1…4094Examples./examples/vlan.pyIP protocolsIP_NAMES, IP_NUMBERSDictionary with known IP protocol names and IDs listed inhttps://en.wikipedia.org/wiki/List_of_IP_protocol_numbersiip()iip(items, verbose, all, strict)Sorting IP protocol numbers and removing duplicatesParameterTypeDescriptionitemsstr, List[int], List[str]Range of IP protocol numbers, can be unsorted and with duplicates, “ip” - Return all IP protocol numbers: [0, 1, …, 255]verboseboolTrue - all protocols in verbose mode: [0, 1, …, 255], False - all protocols in brief mode: [-1], to save RAM (default)strictboolTrue - Raises NetportsValueError, if the protocol is unknown (default), False - Skips unknown protocolsallboolTrue - Return all IP protocol numbers: [0, 1, …, 255]ReturnList[int]of unique sorted IP protocol numbersRaisesNetportsValueErrorif IP protocol numbers are outside valid range 0…255sip()sip(items, verbose, all)Soring string IP protocol numbers and removing duplicatesParameterTypeDescriptionitemsstr, List[int], List[str]Range of IP protocol numbers, can be unsorted and with duplicates. “ip” - mean all numbers in range 0…255.verboseboolTrue - all protocols in verbose mode: [0, 1, …, 255], False - all protocols in brief mode: [-1], to save RAM (default)strictboolTrue - Raises NetportsValueError, if the protocol is unknown (default), False - Skips unknown protocolsallboolTrue - Return all IP protocol numbers: “0-255”Returnstrof unique sorted IP protocol numbersRaisesNetportsValueErrorif IP protocol numbers are outside valid range 0…255ip_pairs()ip_pairs(items, strict)Splits items to IP protocol Number, Name and undefined-invalid protocolsParameterTypeDescriptionitemsstr, List[int], List[str]Range of IP protocol names and numbers, can be unsorted and with duplicatesverboseboolTrue - all protocols in verbose mode: [0, 1, …, 255], False - all protocols in brief mode: [-1], to save RAM (default)ReturnList[Tuple[int, str]]Pairs of IP protocol number and name,List[str]Undefined protocol names and invalid numbersExamples./examples/ip.pyObjectsRange()Range(items, splitter, range_splitter, strict)An object that represents ports range asstrand asList[int]Object implements most of thesetandlistmethods that handle the Range.numbers attribute.ParameterTypeDescriptionitemsstr,List[int]Range of numbers. Numbers can be unsorted and duplicated.splitterstrSeparator character between items, by default “,”range_splitterstrSeparator between min and max numbers in range, by default “-”strictboolTrue - Raise NetportsValueError, if in items is invalid item. False - Make Range without invalid items. By default True.Attributes demonstrationRange operatorsRangeobject implements:Arithmetic operators:+,-Reference to numbers in range by indexOperatorReturnDescriptionRange(“1,4”) + Range(“3,5”)Range(“1,3-5”)Add two objectsRange(“1-5”) - Range(“2”)Range(“1,3-5”)Subtract two objectsRange(“1,3-5”)[1]3Get number by indexRange(“1,3-5”)[1:3][3, 4]Get numbers by sliceRange methodsRangeobject implements most ofsetandlistmethods.MethodDescriptionadd(other)Adds otherRangeobject to selfappend(number)Appends number to selfclear()Removes all numbers from selfcopy()Returns a copy of selfRangeobjectdifference(other)Returns theRangeobject of the difference between self and otherRangedifference_update(other)Removes otherRangefrom selfdiscard(number)Removes the specified number from selfRangeextend(numbers)AddsList[int]numbers to selfindex(number)Returns index of number, raises ValueError if the number is not present in rangeintersection(other)ReturnsRangewhich is the intersection of self and otherRangeintersection_update(other)Removes numbers of otherRangein self, that are not present in otherisdisjoint(other)Returns whether self numbers and otherRangenumbers have intersection or notissubset(other)Returns whether otherRangenumbers contains self numbers or notissuperset(other)Returns whether selfRangenumbers contains otherRangenumbers set or notpop()Removes and returns last number inRange, raises IndexError if list is empty or index is out of rangeremove(number)Removes the specified number from selfRange, raises ValueError if the numbers is not presentsymmetric_difference(other)ReturnsRangeobject with the symmetric differences of self and otherRangesymmetric_difference_update(other)Inserts the symmetric differences from selfRangeand otherRangeupdate(other)ReturnsRangeof the union of selfRangeand otherRangeExamplesAttributes demonstrationSorts numbers and removes duplicatesRange with custom splitters./examples/range.pyNumbersparse_range()parse_range(line, splitter, range_splitter)Parses range from line. Removes white spaces considering splitters.
Sort numbers and removes duplicates.ParameterTypeDescriptionlinestrRange of numbers, can be unsorted and with duplicatessplitterstrSeparator character between items, by default “,”range_splitterstrSeparator between min and max numbers in range, by default “-”ReturnRangeobjectinumbers()inumbers(items, splitter, range_splitter)Sort integer numbers and removes duplicatesParameterTypeDescriptionitemsstr, List[int], List[str]Range of numbers, can be unsorted and with duplicatessplitterstrSeparator character between items, by default “,”range_splitterstrSeparator between min and max numbers in range, by default “-”ReturnList[int]of unique sorted numberssnumbers()snumbers(items, splitter, range_splitter)Sort string numbers and removes duplicatesParameterTypeDescriptionitemsstr, List[int], List[str]Range of numbers, can be unsorted and with duplicatessplitterstrSeparator character between items, by default “,”range_splitterstrSeparator between min and max numbers in range, by default “-”Returnstrof unique sorted numbersExamplesSorts numbers and removes duplicatesRange with custom splitter and range_splitterConverts unsorted range to sortedList[int]without duplicatesConverts unsorted range toList[int]with custom splittersConverts unsorted range to sortedstrwithout duplicatesConverts unsorted range tostrwith custom splitters./examples/numbers.pyInterfacesintfrange()intfrange(items, fmt)Convert interfaces names to shorted range notationParameterTypeDescriptionitemsList[str]List of interfacesfmtstrFormat option: “long”  - Long names: [“interface Ethernet1/1-3”], “short” - Short names: [“Eth1/1/1-3”]ReturnList[str]Interface rangeslong_to_short()long_to_short(device_type, key_lower, value_lower)Returns Interfaces map long-to-short, device_type specificParameterTypeDescriptiondevice_typestrNetmiko device type, increase priority of device_type specific keys. “”, “cisco_asr”, “cisco_ios”, “cisco_nxos”, “hp_comware”key_lowerboolTrue - keys lower-case, False - keys upper-casevalue_lowerboolTrue - values lower-case, False - values upper-caseReturnDict[str, str]Interfaces maplong_to_long()long_to_long(device_type, key_lower, value_lower)Returns Interfaces map long-to-long, device_type specificParameterTypeDescriptiondevice_typestrNetmiko device type, increase priority of device_type specific keys. “”, “cisco_asr”, “cisco_ios”, “cisco_nxos”, “hp_comware”key_lowerboolTrue - keys lower-case, False - keys upper-casevalue_lowerboolTrue - values lower-case, False - values upper-caseReturnDict[str, str]Interfaces mapshort_to_long()short_to_long(device_type, key_lower, value_lower)Returns Interfaces map short-to-long, device_type specificParameterTypeDescriptiondevice_typestrNetmiko device type, increase priority of device_type specific keys. “”, “cisco_asr”, “cisco_ios”, “cisco_nxos”, “hp_comware”key_lowerboolTrue - keys lower-case, False - keys upper-casevalue_lowerboolTrue - values lower-case, False - values upper-caseReturnDict[str, str]Interfaces mapshort_to_short()short_to_short(device_type, key_lower, value_lower)Returns Interfaces map short-to-short, device_type specificParameterTypeDescriptiondevice_typestrNetmiko device type, increase priority of device_type specific keys. “”, “cisco_asr”, “cisco_ios”, “cisco_nxos”, “hp_comware”key_lowerboolTrue - keys lower-case, False - keys upper-casevalue_lowerboolTrue - values lower-case, False - values upper-caseReturnDict[str, str]Interfaces mapIntf()Intf(line, device_type, splitter)An object of interface name, that can contain up to 4 indexes.
Sorts the interfaces by indexes (not by alphabetic).ParameterTypeDescriptionlinestrInterface name that can contain up to 4 indexesdevice_typestrNetmiko device_type (default “”)splitterstrSeparator of characters between indexes (default “,./:”)AttributesAttributesTypeDescriptiondelimitersInterface all delimitersid0strInterface name. Line without IDsid1intInterface 1st IDid2intInterface 2nd IDid3intInterface 3rd IDid4intInterface 4th IDidsInterface all IDslinestrInterface linenamestrInterface name with IDssplitterstrSeparator of characters between indexesdevice_typestrNetmiko device_typelast_idx()last_idx()Index of last ID in interface lineall_names()all_names()All variants of names: long, short, upper-case, lover-case. Device type specificname_full()name_full()Interface long name with IDs and with interface keywordname_long()name_long()Interface long name with IDs and without interface keywordname_short()name_short(replace)Interface short name with IDs, Device type specificParameterTypeDescriptionreplaceList[Tuple[str, str]]Replace the default short name with the first one
that matches in the list of the ‘replace’ argument.ReturnstrInterface short name.part_after()part_after(idx, splitter)Interface part after interested IDParameterTypeDescriptionidxintInterface indexsplitterboolTrue - Include splitter from edge, False - Skip splitter from edgeReturnstrPart of the interface name after specified interface indexpart_before()part_before(idx, splitter)Interface part before interested IDParameterTypeDescriptionidxintInterface indexsplitterboolTrue - Include splitter from edge, False - Skip splitter from edgeReturnstrPart of the interface name before specified interface indexExamplesAttributes demonstrationInterface with custom splitter between indexes. Splitter is ignored when comparingSorting by indexesGrouping interfaces by 3rd index./examples/intfs.pySwVersion()SwVersion(text)Parse the given version string and returnSwVersionobject who can
compare (>, >=, <, <=) software versions of network devices: Cisco, FortiGate, HP, etc.importrefromnetportsimportSwVersiontext="Cisco IOS Software, C2960X Software (C2960X-UNIVERSALK9-M), Version 15.2(4)E10, ..."text=re.search(r"Version (\S+),",text)[1]version1=SwVersion(text)# 15.2(4)E10version2=SwVersion("15.2(4)E11")assertversion1<version2assertversion1<=version2assertnotversion1>version2assertnotversion1>=version2print(version1)# 15.2(4)e10print(version2)# 15.2(4)e11