Within this lesson, we will cover:

  • How to configure global logging.
  • How to configure per session logging.

Introduction to Netmiko Logging

When debugging Netmiko, logging can be extremely useful. There are two 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
Master Network Automation
Join today and get access to:
  • Full deep-dive course library (inc. Batfish, pyATS, Netmiko)
  • Code repositories inc. full course code, scripts and examples
  • 24x7 Multi-vendor labs (Arista, Cisco, Juniper)
  • Private online community
  • Monthly online meetups

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


Join Today