Credentials: login details

credentials.py implements two classes for managing radio login credentials:

  • Credential representing a single set of credentials (IP address, username, password).

  • Credentials representing a sequence of (one or more) Credential.

Usage Examples:

Contents of ‘list_ip_addresses.txt’
username = user
password = passw
192.168.0.0/28
Code example
>>> from pathlib import Path
>>> from tgtools.utils.credentials import Credentials
>>> creds = Credentials.from_file(Path('temp.txt'))
>>> for batch in creds.get_batches(5):
        print(batch)

5 Credentials: from 192.168.0.1 to 192.168.0.5
5 Credentials: from 192.168.0.6 to 192.168.0.10
4 Credentials: from 192.168.0.11 to 192.168.0.14

>>> print(creds[-1])
Credential(192.168.0.14, 'user', 'passw')

Class Information:

class tgtools.utils.credentials.Credentials(items=None, *, text_to_parse=None)

A sequence of Credential, sorted by IP address, and without any duplicates

__init__(items=None, *, text_to_parse=None)

Initialises an instance of the class.

Parameters:

There are 2 ways to initialise Credentials:

  1. If text_to_parse is provided (usually by reading the contents of a config file) then parse the text to obtain:

    • Login credentials: username and password (example below) If omitted, the defaults are ‘admin’/’admin’.

    • A range of IP addresses. Can be any number of the following, each on a new line:
      • A single IP address

      • A range of IP addresses: start and end addresses separated by a hyphen

      • A subnet, with a forward slash denoting the number of subnet bits

    Here is an example content for the input file:

    username = my_user_name
    password = my_password
    192.168.0.100
    192.168.0.101
    10.11.12.1 - 10.11.12.200
    10.10.10.0/24
    192.168.100.0/23
    

    This would result in a total of 1+1+200+254+510 IP addresses.

  2. Alternatively, If text_to_parse is None, then items can be either a single Credential or a sequence of Credential. This option is added in order to implement __getitem__(), and hence the abc.Sequence protocol.

static from_file(filepath: Path)

Read credentials from text file.

Parameters:

filepath (pathlib.Path) – input filename

Return type:

Credentials

get_batches(batch_size=1000) Generator[list[Credential], None, None]

A generator method which yields batches at a time, where each batch is a list of Credential. In each batch, the length of the returned list is batch_size, except possibly for the last batch which may be shorter (remainder).

Parameters:

batch_size (int) – Number of Credential in each batch (defaults to 1000)

Returns:

A list of Credential

Return type:

Generator[List[Credential]]

class tgtools.utils.credentials.Credential(ip_addr: str | IPv4Address | IPv6Address, username: str = 'admin', password: str = 'admin')

Radio’s login credentials: IP address, username, and password.

__init__(ip_addr: str | IPv4Address | IPv6Address, username: str = 'admin', password: str = 'admin')
Parameters:
  • ip_addr (str | ipaddress.IPv4Address | ipaddress.IPv6Address) – IP address

  • username (str) – Username

  • password (str) – Password

padded_ip_addr()

Zero-pad each decimal number, e.g.: ‘192.168.0.1’ -> ‘192.168.000.001’. Useful for sorting.

ip_addr: str

IP Address of radio

password: str

Password to log into radio

username: str

Username to log into radio