CliBaseConnection¶
-
class
CliBaseConnection
(ip=None, username=None, password=None, parser=None, secret=None, enable=False, store_outputs=False, DEBUG=False, verbosity=3, netmiko_params={})¶ Bases:
object
This class represents the base object, from which other (vendor specific classes) inherit. This class is basically a wrapper class around Kirk Byers’ excellent library, netmiko. Even though the netmiko library already provides pretty straightforward and easy way to access network devices, the CliBaseConnection tries to handle multiple events which can arise, such as:
- Device is unreachable
- Fallback to Telnet if SSH is not supported by device (and vice-versa)
- Handles errors in outputs
Apart from the ‘send command, receive output’ this class also performs the parsing and storing outputs.
Parameters: - ip – (str) IP address or FQDN of the device you’re trying to connect to
- username – (str) Username used for login to device
- password – (str) Password used for login to device
- parser – (ParserModule) Instance of ParserModule class which will be used for parsing of text outputs.
By default, new instance of ParserModule is created. :param secret: (str) Enable secret for accessing Privileged EXEC Mode :param enable: (bool) Whether or not enable Privileged EXEC Mode on device :param store_outputs: (bool) Whether or not store text outputs of sent commands :param DEBUG: (bool) Enable debugging logging
-
_check_enable_level
(device)¶ This function is called at the end of
self._connect()
to ensure that the connection is actually alive and that the proper privilege level is set.Parameters: device – ( Netmiko.ConnectHandler
) Instance ofnetmiko.ConnectHandler
. If the connection is working, this will be set asself.device
Returns: None
-
_command_handler
(commands=None, action=None, out_filter=None, return_raw=False)¶ This function tries to send multiple ‘types’ of given command and waits for correct output. This should solve the problem with different command syntax, such as ‘show mac address-table’ vs ‘show mac-address-table’ on different versions of Cisco IOS. When correct output is returned, it is then parsed and the result is returned.
Parameters: - action (str) – Action to perform - has to be key of self.command_mappings
- commands (list) – List of command string to try, such as [‘show mac-address-table’, ‘show mac address-table’]
- out_filter – Instance of Filter class
- return_raw (bool) – If set to True, raw output will be returned.
Returns: JSON representation of command output
-
_connect
()¶ This function handles connection to device, if primary method fails, it will try to connect using secondary method.
Returns: None
-
_connect_ssh
()¶ This function tries to establish connection with device via SSH
Returns: ( netmiko.ConnectHandler
) device
-
_connect_telnet
()¶ This function tries to establish connection with device via Telnet
Returns: ( netmiko.ConnectHandler
) device
-
_get_provider
()¶ Creates provider dictionary for Netmiko connection
Returns: None
-
_send_command
(command, expect_string=None)¶ Parameters: command (str) – Command to send to device Returns: Plaintext output of command from device
-
_send_commands
(commands)¶ Sends multiple commands to device.
Parameters: commands (list) – List of commands to run Returns: Dictionary with key=command, value=output_of_the_command
-
check_connection
()¶ This function can be used to check state of the connection. Returns True if the connection is active and False if it isn’t.
Returns: Bool value representing the connection state.
-
config_mode
()¶
-
disconnect
()¶ This function handles graceful disconnect from the device.
Returns: None
-
get_arp
()¶ Returns content of device ARP table in JSON format. In Cisco terms, this represents the command show ip arp.
Returns: List of dictionaries.
-
get_interfaces
()¶ This function returns JSON representation of all physical and virtual interfaces of the device, containing all available info about each interface. In Cisco terms, this represents usage of command show interfaces.
Returns: List of dictionaries.
-
get_inventory
()¶ This function return JSON representation of all installed modules and HW parts of the device. In Cisco terms, this represents the command show inventory.
Returns: List of dictionaries.
-
get_license
()¶ This function return JSON representation of licenses activated or installed on the device. In Cisco terms, this represents the show license command.
Returns: List of dictionaries.
-
get_mac_address_table
()¶ Returns content of device MAC address table in JSON format. In Cisco terms, this represents the command show mac address-table.
Returns: List of dictionaries.
-
get_portchannels
()¶ This function returns JSON representation of all logical bind interfaces (etherchannels, portchannels). In Cisco terms, this represents the show etherchannel summary command.
Returns: List of dictionaries.
-
get_version
()¶ Returns JSON representation of basic device information, such as vendor, device platform, software version etc. In Cisco terms, this represents the command show version.
Returns: List of dictionaries.
-
get_vlans
()¶ This function returns JSON representation of all VLANs enabled on the device, together with list of assigned interfaces. In Cisco terms, this represents the show vlan brief command.
Returns: List of dictionaries.
-
save_output
(filename, data)¶
-
store_raw_output
(command, raw_output, ext='txt')¶ This function is used for storing the plaintext output of the commands called on the device in separate files. Used mainly for debugging and development purposes. This function is only called if the store_outputs parameter is set to True.
Parameters: - command (str) – Command string executed on the device.
- raw_output (str) – Plaintext output of the command.
- ext (str) – Extension of the file, “.txt” by default.
Returns: None