Within this next section, we will look at how we can:

  • Debug our Netmiko executions, globally or on a per-session basis.
  • Improve performance when running Netmiko when running across multiple devices via the use of multithreading.

When debugging Netmiko logging can be extremely useful. There are 2 ways in which logging can be enabled, at a global level or a per session level.

Per Session

To enable Netmiko logging to a file on a per connection level provide "session_log" : <filename> to your ConnectHandler like so:

net_connect_ios = {
    ...
    "session_log": 'netmiko_session.log',
}
   
net_connect_ios = ConnectHandler(**device)

This will write only the output for the current session to the logfile. At the point, a new session is created; the file will be overwritten.

Global

To enable logging for all connections to a file, we can import and use the logging module, as shown below. In this example, we enable debug level logging and direct all the logs to the file named netmiko.log.

import logging

logging.basicConfig(filename='netmiko_global.log', level=logging.DEBUG)
logger = logging.getLogger("netmiko")

Once you have added the above and run Netmiko, you will see logs much like these:

DEBUG:netmiko:read_channel: 
ios-004>
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:[find_prompt()]: prompt is ios-004>
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:Pattern is: ios\-004
DEBUG:netmiko:_read_channel_expect read_data: 
DEBUG:netmiko:Pattern found: ios\-004 
ios-004>
DEBUG:netmiko:write_channel: b'enable\n'
DEBUG:netmiko:Pattern is: (ios\-004|ssword)
DEBUG:netmiko:_read_channel_expect read_data: 
ios-004>
DEBUG:netmiko:Pattern found: (ios\-004|ssword) 
ios-004>
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:Pattern is: ios\-004
DEBUG:netmiko:_read_channel_expect read_data: 
ios-004>
DEBUG:netmiko:Pattern found: ios\-004 
ios-004>
DEBUG:netmiko:write_channel: b'\n'
DEBUG:netmiko:Pattern is: ios\-004
DEBUG:netmiko:_read_channel_expect read_data: enable
Password: 
DEBUG:netmiko:_read_channel_expect read_data: 
ios-004#
DEBUG:netmiko:Pattern found: ios\-004 enable
Become a Network Automation Jedi
Join our membership and enjoy:
  • Deep Dive Library
  • Community Support
  • Hands-on Examples
  • Multivendor Labs (beta)

100% Satisfaction Guaranteed!
Cancel your membership at any time.


Join our Community