9.4. nettoolkit common functions
- class nettoolkit.nettoolkit_common.common.CapturesOut(capture_log_file: str)[source]
Bases:
objectClass define common methods and properties on captured output file.
- Parameters:
capture_log_file (str) – Output capture file
- Raises:
Exception – _description_
- abs_cmd_function_map = {'Cisco': <function abs_command_cisco>, 'Juniper': <function abs_command_juniper>}
- abs_cmd_map = {'Cisco': {'show cdp neighbors', 'show interfaces description', 'show interfaces status', 'show ip arp', 'show lldp neighbors', 'show mac address-table', 'show running-config', 'show version'}, 'Juniper': {'show arp', 'show bgp summary', 'show chassis hardware', 'show configuration', 'show interfaces descriptions', 'show interfaces terse', 'show lldp neighbors', 'show version'}}
- property abs_commands
Returns absolute commands
- Returns:
absolute command map
- Return type:
dict
- capture_log_file: str
- cmd_output(cmd)[source]
provides filtered output of provided command
- Parameters:
cmd (str) – command string
- Returns:
list of outout for provided command
- Return type:
list
- property device_manufacturar
Returns device manufacturer from capture output.
- Returns:
manufacturer of device
- Return type:
str
- has(cmd)[source]
Checks if outout has provided command output or not.
- Parameters:
cmd (str) – command string
- Returns:
True / False based on match.
- Return type:
bool
- property name
Returns device hostname from capture output.
- Returns:
hostname of device
- Return type:
str
- property outputs
output-list.
- Returns:
outputs splitted in dictionary by its command as key
- Return type:
dict
- Type:
returns dictionary of commands
- nettoolkit.nettoolkit_common.common.abs_command_cisco(cmd)[source]
returns absolute full command for shorteened cmd
- Parameters:
cmd (str) – executed/ captured command ( can be trunked or full )
- Returns:
cisco command - full untrunked
- Return type:
str
- nettoolkit.nettoolkit_common.common.abs_command_juniper(cmd)[source]
returns absolute truked command if any filter applied
- Parameters:
cmd (str) – executed/ captured command ( can be trunked or full )
- Returns:
juniper command - trunked
- Return type:
str
- nettoolkit.nettoolkit_common.common.blank_line(line)[source]
checks if provided line is blank line or not.
- Parameters:
line (str) – input line
- Returns:
is line blank or not
- Return type:
bool
- nettoolkit.nettoolkit_common.common.create_folders(folders, *, silent=True)[source]
Creates Folders
- Parameters:
folders (list,str) – folder(s)
silent (bool, optional) – Create without prompt. Defaults to True.
- Returns:
Success/Fail
- Return type:
bool
- nettoolkit.nettoolkit_common.common.dataframe_generate(d)[source]
convert dicationary to dataframe. multi-level dictionary will be converted flattened first inorder to convert to DataFrame.
- Parameters:
d (dict) – input can be multi-nested dictionary.
- Returns:
pandas DataFrame
- Return type:
DataFrame
- nettoolkit.nettoolkit_common.common.flatten(d, parent_key='', sep='_')[source]
flattens the dictionary
- Parameters:
d (dict) – input can be multi-nested dictionary.
parent_key (str, optional) – key from previous dictionary to be prefixed with current keys. Defaults to ‘’.
sep (str, optional) – keys separator. Defaults to ‘_’.
- Returns:
dictionary of lists if input is dictinoary, list with input dictionary if input is anything else
- Return type:
dict, list
- nettoolkit.nettoolkit_common.common.get_device_manufacturar(file)[source]
finds out manufacturer (cisco/juniper) from given capture file. in case if not found, it will return as Unidentified.
- Parameters:
file (str) – input capture file
- Returns:
Either one from - Cisco, Juniper, Unidentified
- Return type:
str
- nettoolkit.nettoolkit_common.common.get_file_name(file, ext=False)[source]
returns file name of given file path
- Parameters:
file (str) – full string length file path
ext (bool, optional) – include extension or not. Defaults to False.
- Returns:
file name
- Return type:
str
- nettoolkit.nettoolkit_common.common.get_file_path(file)[source]
returns folder of given file path
- Parameters:
file (str) – full string length file path
- Returns:
folder location of file
- Return type:
str
- nettoolkit.nettoolkit_common.common.get_op(file, cmd)[source]
filter the command output from given captured file. Note: output should be taken from capture_it utility or it should be in the format derived by it.
- Parameters:
file (str) – capture file
cmd (str) – show command for which output to capture
- Returns:
filtered command output in list format
- Return type:
list
- nettoolkit.nettoolkit_common.common.get_ops(file, cmd_startswith)[source]
filter the command outputs from given captured file. Note: output should be taken from capture_it utility or it should be in the format derived by it.
- Parameters:
file (str) – capture file
cmd_startswith (str) – show command start string
- Returns:
filtered command output in dict format
- Return type:
dict
- nettoolkit.nettoolkit_common.common.get_string_part(line, begin, end)[source]
get the sub-string out of provided long string(line)
- Parameters:
line (str) – string line
begin (int) – sub-str start point
end (int) – sub-str end point
- Raises:
TypeError – Raise error if input is invalid or sub-string falls outside
- Returns:
sub-string
- Return type:
str
- nettoolkit.nettoolkit_common.common.get_string_trailing(line, begin_at)[source]
get the training part of sub-string starting from provided index
- Parameters:
line (str) – string line
begin_at (int) – sub-str start point
- Raises:
TypeError – Raise error if input is invalid or sub-string falls outside
- Returns:
sub-string
- Return type:
str
- nettoolkit.nettoolkit_common.common.mac_2digit_separated(mac)[source]
converts input mac to 2 digit separated mac format, separator=`:`
- Parameters:
mac (str) – mac address
- Returns:
2 digit separated format of mac address
- Return type:
str
- nettoolkit.nettoolkit_common.common.mac_4digit_separated(mac)[source]
converts input mac to 4 digit separated mac format, separator=`.`
- Parameters:
mac (str) – mac address
- Returns:
4 digit separated format of mac address
- Return type:
str
- nettoolkit.nettoolkit_common.common.open_excel_file(file)[source]
Open Excel file in MS-Excel (excel.exe)
- Parameters:
file (str) – file
- Raises:
Exception – Raise exception if unable to open excel.
- nettoolkit.nettoolkit_common.common.open_folder(folder)[source]
Open folder
- Parameters:
file (str) – file name
- nettoolkit.nettoolkit_common.common.open_text_file(file)[source]
Open Text file in Notepad.exe
- Parameters:
file (str) – file name
- nettoolkit.nettoolkit_common.common.printmsg(pre=None, *, post=None, pre_ends='\n', justify_pre=True, justification_len=80)[source]
- nettoolkit.nettoolkit_common.common.read_file(file)[source]
read the provided text file and retuns output in list format
- Parameters:
file (str) – text file name
- Returns:
output converted to list (separated by lines)
- Return type:
list
- nettoolkit.nettoolkit_common.common.remove_domain(hn)[source]
Removes domain suffix from provided hostname string
- Parameters:
hn (str) – fully qualified dns hostname
- Returns:
hostname left by removing domain suffix
- Return type:
str
- nettoolkit.nettoolkit_common.common.standardize_mac(mac)[source]
removes . or : from mac address and make it a standard
- Parameters:
mac (str) – mac address
- Returns:
standard format of mac address
- Return type:
str
- nettoolkit.nettoolkit_common.common.verifid_output(cmd_op)[source]
vefifies if command output is in valid state. Multiline string are splits with CR. and retuns as list. if input is a list, it will be returned as is. any other input will throw error.
- Parameters:
cmd_op (list, str) – Either list or Multiline string of output
- Raises:
TypeError – Raise error if input is other than string or list.
- Returns:
output in list format
- Return type:
list