BaseConnection

class netmiko.base_connection.BaseConnection(ip=u'', host=u'', username=u'', password=u'', secret=u'', port=None, device_type=u'', verbose=False, global_delay_factor=1, use_keys=False, key_file=None, allow_agent=False, ssh_strict=False, system_host_keys=False, alt_host_keys=False, alt_key_file=u'', ssh_config_file=None, timeout=8)

Defines vendor independent methods.

Otherwise method left as a stub method.

__enter__()

Enter runtime context

__exit__(exc_type, exc_value, traceback)

Gracefully close connection on context manager exit

__init__(ip=u'', host=u'', username=u'', password=u'', secret=u'', port=None, device_type=u'', verbose=False, global_delay_factor=1, use_keys=False, key_file=None, allow_agent=False, ssh_strict=False, system_host_keys=False, alt_host_keys=False, alt_key_file=u'', ssh_config_file=None, timeout=8)

Initialize attributes for establishing connection to target device.

Parameters:
  • ip (str) – IP address of target device. Not required if host is provided.
  • host (str) – Hostname of target device. Not required if ip is provided.
  • username (str) – Username to authenticate against target device if required.
  • password (str) – Password to authenticate against target device if required.
  • secret (str) – The enable password if target device requires one.
  • port (int or None) – The destination port used to connect to the target device.
  • device_type (str) – Class selection based on device type.
  • verbose (bool) – If True enables more verbose logging.
  • global_delay_factor (int) – Controls global delay factor value.
  • use_keys (bool) – If true, Paramiko will attempt to connect to target device using SSH keys.
  • key_file (str) – Name of the SSH key file to use for Paramiko SSH connection authentication.
  • allow_agent (bool) – Set to True to enable connect to the SSH agent
  • ssh_strict (bool) – If True Paramiko will automatically reject unknown hostname and keys. If ‘False’ Paramiko will automatically add the hostname and new host key.
  • system_host_keys (bool) – If True Paramiko will load host keys from the user’s local ‘known hosts’ file.
  • alt_host_keys (bool) – If True host keys will be loaded from a local host-key file.
  • alt_key_file (str) – If alt_host_keys is set to True, provide the filename of the local host-key file to load.
  • ssh_config_file (str) – File name of a OpenSSH configuration file to load SSH connection parameters from.
  • timeout (float) – Set a timeout on blocking read/write operations.
__weakref__

list of weak references to the object (if defined)

check_config_mode(check_string=u'', pattern=u'')

Checks if the device is in configuration mode or not.

check_enable_mode(check_string=u'')

Check if in enable mode. Return boolean.

cleanup()

Any needed cleanup before closing connection.

clear_buffer()

Read any data available in the channel.

commit()

Commit method for platforms that support this.

config_mode(config_command=u'', pattern=u'')

Enter into config_mode.

disable_paging(command=u'terminal length 0', delay_factor=1)

Disable paging default to a Cisco CLI method.

disconnect()

Gracefully close the SSH connection.

enable(cmd=u'', pattern=u'password', re_flags=2)

Enter enable mode.

establish_connection(width=None, height=None)

Establish SSH connection to the network device

Timeout will generate a NetMikoTimeoutException Authentication failure will generate a NetMikoAuthenticationException

width and height are needed for Fortinet paging setting.

exit_config_mode(exit_config=u'', pattern=u'')

Exit from configuration mode.

exit_enable_mode(exit_command=u'')

Exit enable mode.

find_prompt(delay_factor=1)

Finds the current network device prompt, last line only.

static normalize_cmd(command)

Normalize CLI commands to have a single trailing newline.

static normalize_linefeeds(a_string)

Convert ‘

‘,’ ‘, ‘

‘ to ‘ .

read_channel()

Generic handler that will read all the data from an SSH or telnet channel.

read_until_pattern(*args, **kwargs)

Read channel until pattern detected. Return ALL data available.

read_until_prompt(*args, **kwargs)

Read channel until self.base_prompt detected. Return ALL data available.

read_until_prompt_or_pattern(pattern=u'', re_flags=0)

Read until either self.base_prompt or pattern is detected. Return ALL data available.

select_delay_factor(delay_factor)

Choose the greater of delay_factor or self.global_delay_factor.

send_command(command_string, expect_string=None, delay_factor=1, max_loops=500, auto_find_prompt=True, strip_prompt=True, strip_command=True)

Send command to network device retrieve output until router_prompt or expect_string

By default this method will keep waiting to receive data until the network device prompt is detected. The current network device prompt will be determined automatically.

command_string = command to execute expect_string = pattern to search for uses re.search (use raw strings) delay_factor = decrease the initial delay before we start looking for data max_loops = number of iterations before we give up and raise an exception strip_prompt = strip the trailing prompt from the output strip_command = strip the leading command from the output

send_command_expect(*args, **kwargs)

Support previous name of send_command method.

send_command_timing(command_string, delay_factor=1, max_loops=150, strip_prompt=True, strip_command=True)

Execute command_string on the SSH channel.

Use delay based mechanism to obtain output. Strips echoed characters and router prompt.

delay_factor can be used to increase the delays.

max_loops can be used to increase the number of times it reads the data buffer

Returns the output of the command.

send_config_from_file(config_file=None, **kwargs)

Send configuration commands down the SSH channel from a file.

The file is processed line-by-line and each command is sent down the SSH channel.

**kwargs are passed to send_config_set method.

send_config_set(config_commands=None, exit_config_mode=True, delay_factor=1, max_loops=150, strip_prompt=False, strip_command=False)

Send configuration commands down the SSH channel.

config_commands is an iterable containing all of the configuration commands. The commands will be executed one after the other.

Automatically exits/enters configuration mode.

session_preparation()

Prepare the session after the connection has been established

This method handles some of vagaries that occur between various devices early on in the session.

In general, it should include: self.set_base_prompt() self.disable_paging() self.set_terminal_width()

set_base_prompt(pri_prompt_terminator=u'#', alt_prompt_terminator=u'>', delay_factor=1)

Sets self.base_prompt

Used as delimiter for stripping of trailing prompt in output.

Should be set to something that is general and applies in multiple contexts. For Cisco devices this will be set to router hostname (i.e. prompt without ‘>’ or ‘#’).

This will be set on entering user exec or privileged exec on Cisco, but not when entering/exiting config mode.

set_terminal_width(command=u'', delay_factor=1)

CLI terminals try to automatically adjust the line based on the width of the terminal. This causes the output to get distorted when accessed programmatically.

Set terminal width to 511 which works on a broad set of devices.

special_login_handler(delay_factor=1)

Handler for devices like WLC, Avaya ERS that throw up characters prior to login.

static strip_ansi_escape_codes(string_buffer)

Remove any ANSI (VT100) ESC codes from the output

http://en.wikipedia.org/wiki/ANSI_escape_code

Note: this does not capture ALL possible ANSI Escape Codes only the ones I have encountered

Current codes that are filtered: ESC = ‘’ or chr(27) ESC = is the escape character [^ in hex (‘’) ESC[24;27H Position cursor ESC[?25h Show the cursor ESC[E Next line (HP does ESC-E) ESC[2K Erase line ESC[1;24r Enable scrolling from start to row end

HP ProCurve’s, Cisco SG300, and F5 LTM’s require this (possible others)

static strip_backspaces(output)

Strip any backspace characters out of the output.

static strip_command(command_string, output)

Strip command_string from output string

Cisco IOS adds backspaces into output for long commands (i.e. for commands that line wrap)

strip_prompt(a_string)

Strip the trailing router prompt from the output.

telnet_login(pri_prompt_terminator=u'#', alt_prompt_terminator=u'>', delay_factor=1, max_loops=60)

Telnet login. Can be username/password or just password.

write_channel(out_data)

Generic handler that will write to both SSH and telnet channel.