securecrt_tools.utilities

securecrt_tools.utilities.expand_number_range(num_string)

A function that will accept a text number range (such as 1,3,5-7) and convert it into a list of integers such as [1, 3, 5, 6, 7]

Parameters:num_string – <str> A string that is in the format of a number range (e.g. 1,3,5-7)
Returns:<list> A list of all integers in that range (e.g. [1,3,5,6,7])
securecrt_tools.utilities.extract_system_name(device_id, strip_list=[])

In the CDP output some systems return a Hostname(Serial Number) format, while others return Serial(Hostname) output. This function tries to extract the system name from the CDP output and ignore the serial number.

Parameters:
  • device_id – The device_id as learned from CDP.
  • strip_list – A list of strings that should be removed from the hostname, if found
Returns:

securecrt_tools.utilities.human_sort_key(s)

A key function to sort alpha-numerically, not by string

From http://nedbatchelder.com/blog/200712/human_sorting.html This function can be used as the key for a sort algorithm to give it an understanding of numbers, i.e. [a1, a2, a10], instead of the default (ASCII) sorting, i.e. [a1, a10, a2].

Parameters:s
Returns:
securecrt_tools.utilities.list_of_dicts_to_csv(data, filename, header, add_header=True)
Parameters:
  • data
  • filename
  • header
Returns:

securecrt_tools.utilities.list_of_lists_to_csv(data, filename)

Takes a list of lists and writes it to a csv file.

This function takes a list of lists, such as:

[ [“IP”, “Desc”], [“1.1.1.1”, “Vlan 1”], [“2.2.2.2”, “Vlan 2”] ]

and writes it into a CSV file with the filename supplied. Each sub-list in the outer list will be written as a row. If you want a header row, it must be the first sub-list in the outer list.

Parameters:
  • data – <2d-list> A list of lists data structure (one row per line of the CSV)
  • filename – <str> The output filename for the CSV file, that will be placed in the ‘save path’ directory under the global settings.
securecrt_tools.utilities.long_int_name(short_name)

This function expands a short interface name to the full name

Parameters:short_name – The input string (short interface name)
Returns:The shortened interface name
securecrt_tools.utilities.normalize_protocol(raw_protocol)

A function to normalize protocol names between IOS and NXOS. For example, IOS uses ‘C’ and NXOS uses ‘direct” for connected routes. This function will return ‘connected’ in both cases.

Parameters:raw_protocol – <str> The protocol value found in the route table output
Returns:A normalized name for that type of route.
securecrt_tools.utilities.path_safe_name(input_string)

This function will remove or replace characters in the input string so that the output is suitable to be used as a file or directory name.

Parameters:input_string (str) – The string that should be converted into a filename safe version.
Returns:The filename safe version of the input string
securecrt_tools.utilities.remove_empty_or_invalid_file(l_filename)

Check if file is empty or if we captured an error in the command. If so, delete the file.

Parameters:l_filename – Name of file to check
securecrt_tools.utilities.short_int_name(long_name)

This function shortens the interface name for easier reading

Parameters:long_name – The input string (long interface name)
Returns:The shortened interface name
securecrt_tools.utilities.textfsm_parse_to_dict(input_data, template_filename)

Use TextFSM to parse the input text (from a command output) against the specified TextFSM template. Convert each list from the output to a dictionary, where each key in the TextFSM Value name from the template file.

Parameters:
  • input_data – Path to the input file that TextFSM will parse.
  • template_filename – Path to the template file that will be used to parse the above data.
Returns:

A list, with each entry being a dictionary that maps TextFSM variable name to corresponding value.

securecrt_tools.utilities.textfsm_parse_to_list(input_data, template_name, add_header=False)

Use TextFSM to parse the input text (from a command output) against the specified TextFSM template. Use the default TextFSM output which is a list, with each entry of the list being a list with the values parsed. Use add_header=True if the header row with value names should be prepended to the start of the list.

Parameters:
  • input_data – Path to the input file that TextFSM will parse.
  • template_name – Path to the template file that will be used to parse the above data.
  • add_header – When True, will return a header row in the list. This is useful for directly outputting to CSV.
Returns:

The TextFSM output (A list with each entry being a list of values parsed from the input)