6.1. gpl

class nettoolkit.nettoolkit_common.gpl.Container[source]

Bases: ABC

Abstract base class providing template for standard dunder methods template. Object should contain objVar property

abstract property objVar
class nettoolkit.nettoolkit_common.gpl.DB[source]

Bases: object

Collection of static methods for Database. see more…

static read_excel(file, sheet='Sheet1', **kwargs)[source]

reads a sheet from an excel

Parameters:
  • file (str) – input excel file

  • sheet (str, optional) – sheet name. Defaults to ‘Sheet1’.

Returns:

pandas Dataframe Object

Return type:

DataFrame

class nettoolkit.nettoolkit_common.gpl.DIC[source]

Bases: object

Collection of static methods for dictionary objects. see more…

static dict_to_str(dic, indention=0)[source]

convert dictionary (dic) to string. recursive dictionary increases indention.

Parameters:
  • dic (dict) – input dictionary

  • indention (int, optional) – number of spaces. Defaults to 0.

Returns:

Multiline string

Return type:

str

static merge_dict(dx, dy)[source]

Merges two dictionaries for identical keys

Parameters:
  • dx (dict) – first dictionary

  • dy (dict) – second dictionary

Returns:

merged dictionary

Return type:

dict

static recursive_dic(dic, indention=0)[source]

convert dictionary (dic) to string. recursive dictionary increases indention. (deprycated, will be replaced with dict_to_str)

Parameters:
  • dic (dict) – input dictionary

  • indention (int, optional) – number of spaces. Defaults to 0.

Returns:

Multiline string

Return type:

str

class nettoolkit.nettoolkit_common.gpl.Default[source]

Bases: object

Default class representing class docString template

class nettoolkit.nettoolkit_common.gpl.DictMethods[source]

Bases: object

PAPA DUNDER EXTENSIONS FOR DICTIONARY OBJECTS

[self.dic is abstract property which gets iterates over]

append(item, value)[source]

appends value to self[item] dictionary. create new list if no value found for item, appends to list if available.

Parameters:
  • item (str, int) – key of dictionary

  • value (list) – value to be added to dictionary item

Raises:

Exception – WrongInput

class nettoolkit.nettoolkit_common.gpl.DifferenceDict(d)[source]

Bases: dict

Template class to get difference in two dictionary objects. use dunder +/- for adds/removes.

additive = '+ '
get_change(d, change)[source]

compare current object/dict with provided new object/dict (ie: d) and return differences based on change required (”- “/”+ “)

Parameters:
  • d (dict) – dictionary of differences

  • change (str) – change type from (”- “/”+ “) i.e. removed/added

Returns:

_description_

Return type:

_type_

missing = '- '
class nettoolkit.nettoolkit_common.gpl.IO[source]

Bases: object

Collection of static methods for IO objects. see more…

static add_to_file(filename, matter, cr=True)[source]

Writes List/text to output filename.

Parameters:
  • filename (str) – input file

  • matter (str, tuple, list) – matter to write to new created file.

  • cr (bool, optional) – carriage return to add at end of each string/line. Defaults to True.

Returns:

_description_

Return type:

_type_

static copy_text_file(file)[source]

copy file.txt to file-copy.txt

Parameters:

file (str) – file name

static csv_to_tuple(csv)[source]

Returns tuple from the provided comma separated text values

Parameters:

csv (str) – input string

Returns:

comma separated values

Return type:

tuple

static devices_on_log_files(folder, splitter='_@_')[source]

collection of files from given folder where file extensions are .log

Parameters:
  • folder (str) – folder path

  • splitter (str, optional) – splitter. Defaults to “_@_”.

Returns:

devices names from log files

Return type:

set

static file_list_for_time_stamp(hn, ts, folder, splitter='_@_')[source]

collection of files from given folder where hostname (hn) and timestamp (ts) found in the file name.

Parameters:
  • hn (str) – hostname

  • ts (str) – time-stamp

  • folder (str) – folder ptah

  • splitter (str, optional) – splitter. Defaults to “_@_”.

Returns:

files from log files

Return type:

set

static file_to_list(file)[source]

Returns list output content of the provided file

Parameters:

file (str) – input file

Returns:

content of file

Return type:

list

static file_to_str(file)[source]

Returns string output content of the provided file

Parameters:

file (str) – input file

Returns:

content of file

Return type:

str

static jinja_verification(folder)[source]

check all text files from provided folder for verification of jinja strings descrepencies

Parameters:

folder (str) – folder to check all jinja files from

Returns:

verification outcomes

Return type:

str

static timestamps_for_device(devname, folder, splitter='_@_')[source]

collection of time stamps of files from given folder for given hostnames.

Parameters:
  • devname (str) – hostname

  • folder (str) – folder path

  • splitter (str, optional) – splitter. Defaults to “_@_”.

Returns:

time stampls from log files.

Return type:

set

static to_file(filename, matter)[source]

Creates a file with matter

Parameters:
  • filename (str) – filename with path to be creaed.

  • matter (str, list, tuple) – matter to write to new created file.

static update(file, find_item, replace_item)[source]

Find and Replace on provided file and saves file

Parameters:
  • file (str) – input file

  • find_item (str) – search item

  • replace_item (str) – replacement item

class nettoolkit.nettoolkit_common.gpl.IP[source]

Bases: object

Collection of static methods for Networking on (IP). see more…

static bin2dec(binmask)[source]

convert binary mask to decimal mask

Parameters:

binmask (str) – binary mask value

Returns:

Decimal mask

Return type:

int

static inv2dec(invmask)[source]

convert inverse mask to decimal mask

Parameters:

invmask (str) – inverse mask value

Returns:

Decimal mask

Return type:

int

static ping_average(ip)[source]

return average ping responce for provided ip

Parameters:

ip (str) – ip address string

Returns:

responce time or None

Return type:

int, None

class nettoolkit.nettoolkit_common.gpl.LOG[source]

Bases: object

Collection of static methods for logging. see more…

static time_stamp()[source]

current time stamp (for log purpose)

Returns:

current datetime string

Return type:

str

class nettoolkit.nettoolkit_common.gpl.LST[source]

Bases: object

Collection of static methods for list objects. see more…

static convert_vlans_list_to_range_of_vlans_list(vlan_list)[source]

converts list of individual vlans to a list of range of vlans

Parameters:

vlan_list (str) – input list

Returns:

list of vlans (with ranges)

Return type:

list

static expand_vlan_list(vlan_list)[source]

takes input vlan list, expands it’s ranges if any within

Parameters:

vlan_list (list) – input vlans list

Raises:

Exception – Invalid vlan number

Returns:

list of vlans (individual)

Return type:

list

static list_of_devices(list_of_files)[source]

get hostnames (first index item) from list of files.

Parameters:

list_of_files (list) – input file list

Returns:

devices from file list

Return type:

set

static list_to_octet(lst)[source]

joins and return string with provided list with ‘.’, helpful in created ipv4 string with list of 4 numeric items. Doesn’t verify correctness of ip. any number can be concatenated with any numbers of instances.

Parameters:

lst (list) – input ip address splitted by “.”

Returns:

concatenated ip address

Return type:

str

static list_variants(input_list)[source]

list of vlans in different format list of vlans, space separated string, comma separated string,

Parameters:

input_list (list) – input list

Returns:

list variants (str, csv, ssv)

Return type:

dict

static remove_empty_members(lst)[source]

house keeping of list, removes empty members from list

Parameters:

lst (list) – input list

Returns:

updated list

Return type:

list

static split(lst, n)[source]

yield provided list with group of n number of items

Parameters:
  • lst (list) – list of items

  • n (int) – items per group

Yields:

list generator – group of items

class nettoolkit.nettoolkit_common.gpl.Multi_Execution(items=None)[source]

Bases: Default

Template methods for multi-threaded executions. [self.items items are eligible threaded candidates]

end()[source]

Closure process

abstract classmethod execute(hn)[source]

abstract class method, to be executed for each item in self.items

Parameters:

hn (str, int, float) – individual items to be executed for each

execute_mt()[source]

threaded execution in groups (self.max_connections defines max threaded processes)

execute_sequencial()[source]

sequencial execution of items

execute_steps(multi_thread=True)[source]

steps defining executions

Parameters:

multi_thread (bool, optional) – Multithread or Sequencial. Defaults to True.

execute_threads_max(item_list)[source]

threaded execution of a group

Parameters:

item_list (list, set, tuple) – items to be iterated on

get_devices()[source]

get devices names from list of files

max_connections = 100
start(multi_thread=True)[source]

starting up executins either threaded/sequencial

Parameters:

multi_thread (bool, optional) – Multithread or Sequencial. Defaults to True.

Returns:

None

Return type:

None

class nettoolkit.nettoolkit_common.gpl.Numeric[source]

Bases: object

Support Numberic objects

class nettoolkit.nettoolkit_common.gpl.STR[source]

Bases: Container

Collection of static methods for string objects. see more…

static delete_trailing_remarks(s)[source]

Deletes trailing remarks from Juniper config line/string

Parameters:

s (str) – number of characters from right

Returns:

updated string

Return type:

str

static ending(line, c)[source]

check if line ends with c or not, same as native string.endswith() addition is it first strips the line and then checks

Parameters:
  • line (str) – input string

  • c (str) – condition string

Returns:

boolean value for result

Return type:

bool

static find_all(s, sub, start=0, count=None, beginwith=False)[source]

search for multiple substrings ‘sub’ within string ‘s’ Usage: find_all(s, sub, [start=n, [count=c]])

Parameters:
  • s (str) – main string

  • sub (str) – sub string ( to be search within main string )

  • start (int, optional) – Optional: substring to be start search from index. Defaults to 0.

  • count (int, optional) – Optional: count of character from start index. Defaults to None.

  • beginwith (bool, optional) – Optional: check if substring is at beginning. . Defaults to False.

Returns:

all matches or not

Return type:

bool

static find_any(s, sub, start=0, count=None, beginwith=False)[source]

search for multiple substrings ‘sub’ within string ‘s’ Usage: find_any(s, sub, [start=n, [count=c]])

Parameters:
  • s (str) – main string

  • sub (str) – sub string ( to be search within main string )

  • start (int, optional) – Optional: substring to be start search from index. Defaults to 0.

  • count (int, optional) – Optional: count of character from start index. Defaults to None.

  • beginwith (bool, optional) – Optional: check if substring is at beginning. . Defaults to False.

Returns:

for atleast one matches

Return type:

bool

static find_multi(s, sub, start=0, count=None, index=True, beginwith=False)[source]

search for multiple substrings ‘sub’ within string ‘s’. Usage: find_multi(s, sub, [start=n, [count=c], index=True])

Parameters:
  • s (str) – main string

  • sub (str, tuple, list) – sub string ( to be search within main string )

  • start (int, optional) – Optional: substring to be start search from index . Defaults to 0.

  • count (int, optional) – Optional: count of character from start index. Defaults to None.

  • index (bool, optional) – Optional: return index or boolean values. Defaults to True.

  • beginwith (bool, optional) – Optional: check if substring is at beginning. Defaults to False.

Returns:

list of indexes/bool

Return type:

list

static find_within(s, prefix, suffix=None, pos=0)[source]

finds characters between prefix and suffix substrings from string

Parameters:
  • s (str) – main string to be search within

  • prefix (str) – starting substring

  • suffix (str, optional) – ending substring. Defaults to None.

  • pos (int, optional) – position index, search to be start from. Defaults to 0.

Returns:

(str, int) (returned string, position of returned suffix position)

Return type:

tuple

static finddualnreplacesingle(s, duo=' ', strip=None)[source]

Finds subsequent characters in string and replace those with single.

Parameters:
  • s (str) – Source string

  • duo (str, optional) – characters which requires reductions if susequent. Defaults to ‘ ‘.

  • strip (int, optional) – values (-1=lstrip ,0=strip ,1=rstrip). Defaults to None.

Returns:

_description_

Return type:

_type_

static found(s, sub, pos=0)[source]

Search for substring in string and return Boolean result

Parameters:
  • s (str) – main string to be search within

  • sub (str) – substring which is to be search in to main string

  • pos (int, optional) – position index, search to be start from. Defaults to 0.

Returns:

find or not

Return type:

bool

static foundPos(s, sub, pos=0)[source]

Search for substring in string and return index value result

Parameters:
  • s (str) – main string to be search within

  • sub (str) – substring which is to be search in to main string

  • pos (int, optional) – position index, search to be start from. Defaults to 0.

Returns:

find index value

Return type:

int

static get_logfile_name(folder, hn, cmd='', ts='', separator='_@_', extn='.log')[source]

return log file name for the command on device with/wo provided time_stamp

Parameters:
  • folder (str) – path to where file should be saved

  • hn (str) – file name starting with provided host-name

  • cmd (str, optional) – file name containing additional commands string. Defaults to ‘’.

  • ts (str, optional) – file name containing additional time stamp. Defaults to ‘’.

  • separator (str, optional) – hn-cmd-ts separator . Defaults to “_@_”.

  • extn (str, optional) – extension of filename. Defaults to ‘.log’.

Returns:

filename along with full path

Return type:

str

static header_indexes(line)[source]

input header string line of a text table. returns dictionary with key:value pair where keys are header string and value are string index (position) of string in line

Parameters:

line (str) – input header string

Returns:

header with its index numbers

Return type:

dict

static header_indexes_using_splitby(line, split_by='  ')[source]

input header string line of a text table. returns dictionary with key:value pair where keys are header string and value are string index (position) of string in line

Parameters:
  • line (str) – input header string

  • split_by (str) – string using which line is to be broken

Returns:

header with its index numbers

Return type:

dict

static hostname(net_connect)[source]

input paramiko netconnection, returns hostname from device.

Parameters:

net_connect (connection object) – paramiko connection object

Returns:

hostname from connection

Return type:

str

static hostname_from_cli(line, command)[source]

input standard text input line, for which command was entered

Parameters:
  • line (str) – input line string

  • command (str) – command

Returns:

hostname from command line

Return type:

str

static if_prefix(intName)[source]

Interface beginning Name

Parameters:

intName (str) – interface

Returns:

interface prefix

Return type:

str

static if_standardize(intName, expand=True)[source]

standardize the interface for uneven length strings. expand will give fulllength, otherwise it will shrink it to its standard size given

Parameters:
  • intName (str) – interface

  • expand (bool, optional) – expansion of interface. Defaults to True.

Returns:

standardized interface

Return type:

str

static if_suffix(intName)[source]

Interface ending ports

Parameters:

intName (str) – interface

Returns:

interface suffix

Return type:

str

static indention(s)[source]

get string indention value

Parameters:

s (str) – input string

Returns:

number of indentants

Return type:

int

static intf_standardize_or_null(intName, intf_type=None, expand=True)[source]

standardize the interface for uneven length strings. expand will give fulllength, otherwise it will shrink it to its standard size given for incorrect interface returns blank (None)

Parameters:
  • intName (str) – interface

  • expand (bool, optional) – expansion of interface. Defaults to True.

Returns:

standardized interface or blank

Return type:

str

static is_blank_line(s)[source]

is provided string/line a blank line

Parameters:

s (str) – input string

Returns:

boolean value for result

Return type:

bool

static is_hostname_line(s, host)[source]

string/line containing hostname of device

Parameters:
  • s (str) – input string

  • host (str) – hostname to be find in provided string

Returns:

boolean value for result

Return type:

bool

static mid(strg, pos, n=0)[source]

N-number of characters from position in string; default n is till end

Parameters:
  • strg (str) – input string

  • pos (int) – position from where slice to begin

  • n (int) – number of characters from from slice(pos)

Returns:

string portion from middle

Return type:

str

static prepend_bgp_as(bgp_as, n)[source]

n number of BGP AS Number prepending string.

Parameters:
  • bgp_as (str) – bgp as number

  • n (int) – to repeat with

Returns:

as-path prepend string

Return type:

str

static replace_dual_and_split(s, duo=' ', strip=None)[source]

Finds subsequent characters in string and replace those with single, plus, splits the string using provided character (duo).

Parameters:
  • s (str) – main string

  • duo (str, optional) – characters which requires reductions if susequent. Defaults to ‘ ‘.

  • strip (int, optional) – values (-1=lstrip ,0=strip ,1=rstrip) . Defaults to None.

Returns:

split using dual characters

Return type:

list

static right(strg, n)[source]

N-number of characters from right side of string

Parameters:
  • strg (str) – input string

  • n (int) – number of characters from right

Returns:

string portion from right

Return type:

str

static shrink_if(intName, length=2)[source]

Interface Name shortening, input length will decide number of charactes to be included in shortened output

Parameters:
  • intName (str) – interface

  • length (int, optional) – interface identifier prefix length. Defaults to 2.

Returns:

short name of interface

Return type:

str

static starting(line, c)[source]

check if line starts with c or not, same as native string.startswith() addition is it first strips the line and then checks

Parameters:
  • line (str) – input string

  • c (str) – condition string

Returns:

boolean value for result

Return type:

bool

static string_concate(s, s1, conj='')[source]

Concatenate strings s and s1 with conjuctor conj

Parameters:
  • s (str) – input string

  • s1 (str) – adder string

  • conj (str, optional) – conjuctor. Defaults to ‘’.

Returns:

updated string

Return type:

str

static string_within(line, prefix, suffix=None, pos=0)[source]

finds characters between prefix and suffix substrings from string

Parameters:
  • line (str) – main string to be search within

  • prefix (str) – starting substring

  • suffix (str, optional) – ending substring. Defaults to None.

  • pos (int, optional) – position index, search to be start from. Defaults to 0.

Returns:

(str, int) (returned string, position of returned suffix position)

Return type:

tuple

static suffix_index_within(line, prefix, suffix=None, pos=0)[source]

finds characters between prefix and suffix substrings from string

Parameters:
  • line (str) – main string to be search within

  • prefix (str) – starting substring

  • suffix (str, optional) – ending substring. Defaults to None.

  • pos (int, optional) – position index, search to be start from. Defaults to 0.

Returns:

index of suffix

Return type:

int

static to_list(s)[source]

Returns list for the provided string - s, splits string by lines

Parameters:

s (str) – multiline input string

Returns:

splitted lines

Return type:

list

static to_set(s)[source]

Return set of values for the provided string - s. splits string by lines and comma

Parameters:

s (str) – multiline input string

Returns:

splitted lines/items

Return type:

set

static update(s, searchItem='', replaceItem='')[source]

find n replace string s

Parameters:
  • s (str) – main string

  • searchItem (str, optional) – search string. Defaults to ‘’.

  • replaceItem (str, optional) – replacement string. Defaults to ‘’.

Returns:

updated string

Return type:

str

static update_str(s, searchItem='', replaceItem='')[source]

Updates line for search item with replace item (Find/Repalace)

Parameters:
  • s (str) – input string

  • searchItem (str, optional) – search item. Defaults to ‘’.

  • replaceItem (str, optional) – replacement item. Defaults to ‘’.

Returns:

updated string

Return type:

str

class nettoolkit.nettoolkit_common.gpl.XL_READ(xl, shtName='Sheet1')[source]

Bases: object

Excel file read

Parameters:
  • xl (str) – Excel file name

  • shtName (str, optional) – sheet name. Defaults to ‘Sheet1’.

Returns:

object instance

Return type:

XL_READ

Yields:

tuple – sheetname, dataframe of the sheet

column_values(column, **kwarg)[source]

selected column output, after filters applied

Parameters:

column (str, list) – a single column name or , list of column names

Returns:

filtered columns

Return type:

DataFrame

filter(df=None, **kwarg)[source]

Filter Records

Parameters:

df (DataFrame, optional) – pandas DataFrame. Defaults to None.

Returns:

filtered DataFrame

Return type:

DataFrame

class nettoolkit.nettoolkit_common.gpl.XL_WRITE(hostname, folder, index=False, **sht_df)[source]

Bases: object

Excel file generate

Parameters:
  • hostname (str) – excel file

  • folder (str) – folder path

  • index (bool, optional) – write with index or not. Defaults to False.

Returns:

object instance

Return type:

XL_WRITE

nettoolkit.nettoolkit_common.gpl.dict_differences(d1, d2, change)[source]

returns differences for provided two dictionaries input d1, d2 type: string/int/float/set/dictionary input change is change type prefix (ex: “ -”, “ +”) return value type depends on input d1, d2 type.

Parameters:
  • d1 (str, int, float, set, dict) – input 1

  • d2 (str, int, float, set, dict) – input 2

  • change (str) – change type from (”- “/”+ “) i.e. removed/added

Raises:

Exception – TypeMismatch

Returns:

differences in dictionary format

Return type:

dict

nettoolkit.nettoolkit_common.gpl.get_cisco_int_type(intf)[source]

returns cisco interface type

Parameters:

intf (str) – cisco interface

Returns:

interface type (FastEthernet, GigEthernet, etc..)

Return type:

str

nettoolkit.nettoolkit_common.gpl.get_device_manu(intf)[source]

returns device type from provided interface

Parameters:

intf (str) – interface

Returns:

manufacturer (cisco, juniper)

Return type:

device type

nettoolkit.nettoolkit_common.gpl.get_juniper_int_type(intf)[source]

returns juniper interface type

Parameters:

intf (str) – juniper interface

Returns:

interface type (ge, xe, etc)

Return type:

str

nettoolkit.nettoolkit_common.gpl.get_password()[source]

input password prompt

Returns:

entered password

Return type:

str

nettoolkit.nettoolkit_common.gpl.get_username()[source]

input username prompt

Returns:

entered username

Return type:

str

nettoolkit.nettoolkit_common.gpl.interface_type(ifname)[source]

get the interface type from interface string

Parameters:

ifname (str) – interface name/string

Raises:

ValueError – raise error if input missing

Returns:

tuple with interface type (e.g PHYSICAL, VLAN…) and sub interface type (e.g FastEthernet, .. ). None if not detected

Return type:

tuple

nettoolkit.nettoolkit_common.gpl.nslookup(ip)[source]

return discovered hostname for provided ip

Parameters:

ip (str) – ip address

Returns:

domain name string

Return type:

str

nettoolkit.nettoolkit_common.gpl.standardize_if(ifname, expand=False)[source]

standardized interface naming

Parameters:
  • ifname (str) – variable length interface name

  • expand (bool, optional) – expand will make it full length name. Defaults to False.

Raises:
  • ValueError – if missing with mandatory input

  • TypeError – if invalid value detected

  • KeyError – if invalid shorthand key detected

Returns:

updated interface string

Return type:

str