Module netmiko.utilities
Miscellaneous utility functions.
Functions
def calc_old_timeout(max_loops: Optional[int] = None, delay_factor: Optional[float] = None, loop_delay: float = 0.2, old_timeout: int = 100) ‑> float
-
loop_delay is .2 in Netmiko 3.x delay_factor would multiple the loop delay Number of loops was typically 500
Thus each loop would sleep (loop_delay * delay_factor) seconds That sleep would happen max_loops time
Formula is (loop_delay * delay_factor) * max_loops
There was a way Netmiko's self.timeout could override the default settings and essentially be used to adjust max_loops (this was probably rarely used).
def check_serial_port(name: str) ‑> str
-
returns valid COM Port.
def clitable_to_dict(cli_table: textfsm.clitable.CliTable) ‑> List[Dict[str, str]]
-
Converts TextFSM cli_table object to list of dictionaries.
def display_inventory(my_devices: Dict[str, Union[List[str], Dict[str, Any]]]) ‑> None
-
Print out inventory devices and groups.
def ensure_dir_exists(verify_dir: str) ‑> None
-
Ensure directory exists. Create if necessary.
def f_exec_time(func: ~F) ‑> ~F
def find_cfg_file(file_name: Union[str, bytes, ForwardRef('PathLike[Any]'), ForwardRef(None)] = None)
-
Search for netmiko_tools inventory file in the following order: NETMIKO_TOOLS_CFG environment variable Current directory Home directory Look for file named: .netmiko.yml or netmiko.yml Also allow NETMIKO_TOOLS_CFG to point directly at a file
def find_netmiko_dir() ‑> Tuple[str, str]
-
Check environment first, then default dir
def get_structured_data(raw_output: str, platform: Optional[str] = None, command: Optional[str] = None, template: Optional[str] = None, raise_parsing_error: bool = False) ‑> Union[str, List[Dict[str, str]]]
-
Convert raw CLI output to structured data using TextFSM template.
You can use a straight TextFSM file i.e. specify "template". If no template is specified, then you must use an CliTable index file.
def get_structured_data_genie(raw_output: str, platform: str, command: str, raise_parsing_error: bool = False) ‑> Union[str, Dict[str, Any]]
def get_structured_data_textfsm(raw_output: str, platform: Optional[str] = None, command: Optional[str] = None, template: Optional[str] = None, raise_parsing_error: bool = False) ‑> Union[str, List[Dict[str, str]]]
-
Convert raw CLI output to structured data using TextFSM template.
You can use a straight TextFSM file i.e. specify "template". If no template is specified, then you must use an CliTable index file.
def get_structured_data_ttp(raw_output: str, template: str, raise_parsing_error: bool = False) ‑> Union[str, List[Any]]
-
Convert raw CLI output to structured data using TTP template.
You can use a straight TextFSM file i.e. specify "template"
def get_template_dir() ‑> str
-
Find and return the directory containing the TextFSM index file.
Order of preference is: 1) Find directory in
NET_TEXTFSM
Environment Variable. 2) Check for pip installedntc-templates
location in this environment. 3) ~/ntc-templates/ntc_templates/templates.If
index
file is not found in any of these locations, raise ValueError:return: directory containing the TextFSM index file
def load_devices(file_name: Union[str, bytes, ForwardRef('PathLike[Any]'), ForwardRef(None)] = None)
-
Find and load .netmiko.yml file.
def load_netmiko_yml(file_name: Union[str, bytes, ForwardRef('PathLike[Any]'), ForwardRef(None)] = None)
-
Load and parse the .netmiko.yml as determined by 'find_cfg_file'.
Parsing
Retrieve and extract 'config' parameters: meta field Determine if encryption is being used and decrypt any encrypted fields
def load_yaml_file(yaml_file: Union[str, bytes, ForwardRef('PathLike[Any]')])
-
Read YAML file.
def m_exec_time(func: ~F) ‑> ~F
def nokia_context_filter(data: str, re_flags: int = re.MULTILINE) ‑> str
-
Nokia context from string. Examples:
(ro)[]
(ex)[configure router "Base" bgp]
Converted over to a standalone function for easier unit testing.
def obtain_all_devices(my_devices: Dict[str, Union[List[str], Dict[str, Any]]]) ‑> Dict[str, Dict[str, Any]]
-
Dynamically create 'all' group.
def obtain_netmiko_filename(device_name: str) ‑> str
-
Create file name based on device_name.
def run_ttp_template(connection: BaseConnection, template: Union[str, bytes, ForwardRef('PathLike[Any]')], res_kwargs: Dict[str, Any], **kwargs: Any)
-
Helper function to run TTP template parsing.
:param connection: Netmiko connection object
:param template: TTP template
:param res_kwargs:
**res_kwargs
arguments for TTP result method:param kwargs:
**kwargs
for TTP object instantiation def select_cmd_verify(func: ~F) ‑> ~F
-
Override function cmd_verify argument with global setting.
def structured_data_converter(raw_data: str, command: str, platform: str, use_textfsm: bool = False, use_ttp: bool = False, use_genie: bool = False, textfsm_template: Optional[str] = None, ttp_template: Optional[str] = None, raise_parsing_error: bool = False) ‑> Union[str, List[Any], Dict[str, Any]]
-
Try structured data converters in the following order: TextFSM, TTP, Genie.
Return the first structured data found, else return the raw_data as-is unless
raise_parsing_error
is True, then bubble up the exception to the caller. def write_bytes(out_data: ~AnyStr, encoding: str = 'utf-8') ‑> bytes
-
Ensure output is properly encoded bytes.
def write_tmp_file(device_name: str, output: str) ‑> str