Skip to main content

Python tools for managing ranges of VLANs, TCP/UDP ports, IP protocols, Interfaces

Project description

https://img.shields.io/pypi/v/netports.svg https://img.shields.io/pypi/pyversions/netports.svg

netports

Python tools for managing ranges of VLANs, TCP/UDP ports, IP protocols, Interfaces Recommended for scripting in telecommunications networks.

Requirements

Python >=3.8

Installation

Install the package from pypi.org release

pip install netports

or install the package from github.com release

pip install https://github.com/vladimirs-git/netports/archive/refs/tags/0.6.1.tar.gz

or install the package from github.com repository

pip install git+https://github.com/vladimirs-git/netports@0.6.1

TCP/UDP ports

itcp()

itcp(items, verbose, all) Integer TCP/UDP Ports. Sorting TCP/UDP ports and removing duplicates

Parameter

Type

Description

items

str, List[int], List[str]

Range of TCP/UDP ports, can be unsorted and with duplicates

verbose

bool

True - all ports in verbose mode: [1, 2, …, 65535], False - all ports in brief mode: [-1], to reduces RAM usage (default)

all

bool

True - Returns all TCP/UDP ports: [1, 2, …, 65535], or [-1] for verbose=False

Return

List[int] of unique sorted TCP/UDP ports

Raises

ValueError if TCP/UDP ports are outside valid range 1…65535

stcp()

stcp(items, verbose, all) String TCP/UDP ports. Sorting TCP/UDP ports and removing duplicates

Parameter

Type

Description

items

str, List[int], List[str]

Range of TCP/UDP ports, can be unsorted and with duplicates

verbose

bool

True - all ports in verbose mode: [1, 2, …, 65535], False - all ports in brief mode: [-1], to reduces RAM usage (default)

all

bool

True - Returns all TCP/UDP ports: “1-65535”

Return

str of unique sorted TCP/UDP ports

Raises

ValueError if TCP/UDP ports are outside valid range 1…65535

Examples

./examples/tcp_udp.py

VLAN IDs

ivlan()

ivlan(items, verbose, all, splitter, range_splitter, platform) Sorting integer VLAN IDs and removing duplicates

Parameter

Type

Description

items

str, List[int], List[str]

Range of VLANs, can be unsorted and with duplicates

verbose

bool

True - all VLAN IDs in verbose mode: [1, 2, …, 65535], False - all VLAN IDs in brief mode: [-1], to reduces RAM usage (default)

all

bool

True - Returns all VLAN IDs: [1, 2, …, 4094], or [-1] for verbose=False

splitter

str

Separator character between items, by default “,”

range_splitter

str

Separator between min and max numbers in range, by default “-”

platform

str

Set splitter and range_splitter to platform specific values. Defined: “cisco” (Cisco IOS), “hpe” (Hewlett Packard Enterprise).

Return

List[int] of unique sorted VLANs

Raises

ValueError if VLANs are outside valid range 1…4094

svlan()

svlan(items, verbose, all, splitter, range_splitter, platform) Sorting string VLANs and removing duplicates

Parameter

Type

Description

items

str, List[int], List[str]

Range of VLANs, can be unsorted and with duplicates

verbose

bool

True - all VLAN IDs in verbose mode: [1, 2, …, 65535], False - all VLAN IDs in brief mode: [-1], to reduces RAM usage (default)

all

bool

True - Returns all VLAN IDs: “1-4094”

splitter

str

Separator character between items, by default “,”

range_splitter

str

Separator between min and max numbers in range, by default “-”

platform

str

Set splitter and range_splitter to platform specific values. Defined: “cisco” (Cisco IOS), “hpe” (Hewlett Packard Enterprise).

Return

str of unique sorted VLANs

Raises

ValueError if VLANs are outside valid range 1…4094

Examples

./examples/vlan.py

IP protocols

IP_NAMES, IP_NUMBERS

Dictionary with known IP protocol names and IDs listed in https://en.wikipedia.org/wiki/List_of_IP_protocol_numbers

iip()

iip(items, verbose, all, strict) Sorting IP protocol numbers and removing duplicates

Parameter

Type

Description

items

str, List[int], List[str]

Range of IP protocol numbers, can be unsorted and with duplicates, “ip” - Return all IP protocol numbers: [0, 1, …, 255]

verbose

bool

True - all protocols in verbose mode: [0, 1, …, 255], False - all protocols in brief mode: [-1], to reduces RAM usage (default)

strict

bool

True - Raises ValueError, if the protocol is unknown (default), False - Skips unknown protocols

all

bool

True - Return all IP protocol numbers: [0, 1, …, 255]

Return

List[int] of unique sorted IP protocol numbers

Raises

ValueError if IP protocol numbers are outside valid range 0…255

sip()

sip(items, verbose, all) Soring string IP protocol numbers and removing duplicates

Parameter

Type

Description

items

str, List[int], List[str]

Range of IP protocol numbers, can be unsorted and with duplicates. “ip” - mean all numbers in range 0…255.

verbose

bool

True - all protocols in verbose mode: [0, 1, …, 255], False - all protocols in brief mode: [-1], to reduces RAM usage (default)

strict

bool

True - Raises ValueError, if the protocol is unknown (default), False - Skips unknown protocols

all

bool

True - Return all IP protocol numbers: “0-255”

Return

str of unique sorted IP protocol numbers

Raises

ValueError if IP protocol numbers are outside valid range 0…255

ip_pairs()

ip_pairs(items, strict) Splits items to IP protocol Number, Name and undefined-invalid protocols

Parameter

Type

Description

items

str, List[int], List[str]

Range of IP protocol names and numbers, can be unsorted and with duplicates

verbose

bool

True - all protocols in verbose mode: [0, 1, …, 255], False - all protocols in brief mode: [-1], to reduces RAM usage (default)

Return
List[Tuple[int, str]] Pairs of IP protocol number and name,

List[str] Undefined protocol names and invalid numbers

Examples

./examples/ip.py

Objects

Range()

Range(items, splitter, range_splitter, strict) An object that represents ports range as str and as List[int] Object implements most of the set and list methods that handle the Range.numbers attribute.

Parameter

Type

Description

items

str, List[int]

Range of numbers. Numbers can be unsorted and duplicated.

splitter

str

Separator character between items, by default “,”

range_splitter

str

Separator between min and max numbers in range, by default “-”

strict

bool

True - Raise ValueError, if in items is invalid item. False - Make Range without invalid items. By default True.

Attributes demonstration

Range operators

Range object implements:

  • Arithmetic operators: +, -

  • Reference to numbers in range by index

Operator

Return

Description

Range(“1,4”) + Range(“3,5”)

Range(“1,3-5”)

Add two objects

Range(“1-5”) - Range(“2”)

Range(“1,3-5”)

Subtract two objects

Range(“1,3-5”)[1]

3

Get number by index

Range(“1,3-5”)[1:3]

[3, 4]

Get numbers by slice

Range methods

Range object implements most of set and list methods.

Method

Description

add(other)

Adds other Range object to self

append(number)

Appends number to self

clear()

Removes all numbers from self

copy()

Returns a copy of self Range object

difference(other)

Returns the Range object of the difference between self and other Range

difference_update(other)

Removes other Range from self

discard(number)

Removes the specified number from self Range

extend(numbers)

Adds List[int] numbers to self

index(number)

Returns index of number, raises ValueError if the number is not present in range

intersection(other)

Returns Range which is the intersection of self and other Range

intersection_update(other)

Removes numbers of other Range in self, that are not present in other

isdisjoint(other)

Returns whether self numbers and other Range numbers have intersection or not

issubset(other)

Returns whether other Range numbers contains self numbers or not

issuperset(other)

Returns whether self Range numbers contains other Range numbers set or not

pop()

Removes and returns last number in Range, raises IndexError if list is empty or index is out of range

remove(number)

Removes the specified number from self Range, raises ValueError if the numbers is not present

symmetric_difference(other)

Returns Range object with the symmetric differences of self and other Range

symmetric_difference_update(other)

Inserts the symmetric differences from self Range and other Range

update(other)

Returns Range of the union of self Range and other Range

Examples

  • Attributes demonstration

  • Sorts numbers and removes duplicates

  • Range with custom splitters

./examples/range.py

Numbers

parse_range()

parse_range(line, splitter, range_splitter) Parses range from line. Removes white spaces considering splitters. Sort numbers and removes duplicates.

Parameter

Type

Description

line

str

Range of numbers, can be unsorted and with duplicates

splitter

str

Separator character between items, by default “,”

range_splitter

str

Separator between min and max numbers in range, by default “-”

Return

Range object

inumbers()

inumbers(items, splitter, range_splitter) Sort integer numbers and removes duplicates

Parameter

Type

Description

items

str, List[int], List[str]

Range of numbers, can be unsorted and with duplicates

splitter

str

Separator character between items, by default “,”

range_splitter

str

Separator between min and max numbers in range, by default “-”

Return

List[int] of unique sorted numbers

snumbers()

snumbers(items, splitter, range_splitter) Sort string numbers and removes duplicates

Parameter

Type

Description

items

str, List[int], List[str]

Range of numbers, can be unsorted and with duplicates

splitter

str

Separator character between items, by default “,”

range_splitter

str

Separator between min and max numbers in range, by default “-”

Return

str of unique sorted numbers

Examples

  • Sorts numbers and removes duplicates

  • Range with custom splitter and range_splitter

  • Converts unsorted range to sorted List[int] without duplicates

  • Converts unsorted range to List[int] with custom splitters

  • Converts unsorted range to sorted str without duplicates

  • Converts unsorted range to str with custom splitters

./examples/numbers.py

Interfaces

intfrange()

intfrange(items, fmt) Convert interfaces names to shorted range notation

Parameter

Type

Description

items

List[str]

List of interfaces

fmt

str

Format option: “long” - Long names: [“interface Ethernet1/1-3”], “short” - Short names: [“Eth1/1/1-3”]

Return

List[str] Interface ranges

Intf()

Intf(line, splitter) An object of interface name, that can contain up to 4 indexes. Sorts the interfaces by indexes (not by alphabetic).

Parameter

Type

Description

line

str

Interface name that can contain up to 4 indexes

splitter

str

Separator characters between indexes. By default “,./:”

Attributes

Attributes

Type

Description

delimiters

Interface all delimiters

id0

str

Interface name. Line without IDs

id1

int

Interface 1st ID

id2

int

Interface 2nd ID

id3

int

Interface 3rd ID

id4

int

Interface 4th ID

ids

Interface all IDs

line

str

Interface line

name

str

Interface name with IDs

last_idx()

last_idx() Index of last ID in interface line

name_short()

name_short() Interface short name with IDs

part()

part(idx) Interface part before interested ID

Examples

  • Attributes demonstration

  • Interface with custom splitter between indexes. Splitter is ignored when comparing

  • Sorting by indexes

  • Grouping interfaces by 3rd index

./examples/intfs.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

netports-0.6.1.tar.gz (24.9 kB view details)

Uploaded Source

File details

Details for the file netports-0.6.1.tar.gz.

File metadata

  • Download URL: netports-0.6.1.tar.gz
  • Upload date:
  • Size: 24.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for netports-0.6.1.tar.gz
Algorithm Hash digest
SHA256 6d626aab37e1acc5446b748d54cd879c7be95db7299c9b76d07a6fabba619f14
MD5 8d849e574b53da073940d6c023cfe731
BLAKE2b-256 61794dc8882bfae6df9e920eb48e4e37d329c67515b95abe8de76353ca60aa37

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page