Python package – “netmiko” … Part 3

In the previous sections, if you observe carefully, we have been mentioning about the device type while defining connection dictionary. How about imagine a situation where we have a set of network devices from different OEMs and need to execute the same functions without mentioning the device type? – In this section, we will discuss about two important classes which can be used to auto-discover the OS type of device.

  1. SSHDetect
  2. SNMPDetect

To achieve this; we can use the auto-detect functionality of “netmiko” – It uses a combination of SNMP discovery OIDS and executes several show commands on the remote console to detect the router operating system and type, based on the output string. Then netmiko will load the appropriate driver into the ConnectHandler() class:

SSHDetect

from netmiko import ConnectHandler
from netmiko import SSHDetect

# Define a connection Handler i.e. Device dictionary
CISCO_3850 = {
    'device_type': 'autodetect',
    'ip': '10.24.7.3',
    'username': 'admin',
    'password': 'admin',
    'port': '22',          # optional, defaults to 22
    'secret': '',          # optional, defaults to ''
}

DETECT_DEVICE = SSHDetect(**CISCO_3850)
DEVICE_TYPE = DETECT_DEVICE.autodetect()
if DEVICE_TYPE is None:
    print("Error: No auto device detection for specified device.")
    exit()
else:
    print(DEVICE_TYPE)
    print(DETECT_DEVICE.potential_matches)
    CISCO_3850['device_type'] = DEVICE_TYPE

    # Establish an SSH connection to the device by passing in the device dictionary.
    CONNECT = ConnectHandler(**CISCO_3850)

    # Execute show commands.
    OUTPUT = CONNECT.send_command('show run int GigabitEthernet0/0/0')
    print(OUTPUT)

SNMPDetect

from netmiko import ConnectHandler
from netmiko.snmp_autodetect import SNMPDetect
import pysnmp
# Define a connection Handler i.e. Device dictionary
CISCO_3850 = {
    'device_type': 'autodetect',
    'ip': '10.24.7.3',
    'username': 'admin',
    'password': 'admin',
    'port': '22',          # optional, defaults to 22
    'secret': '',          # optional, defaults to ''
}

# SNMP Detection
SNMP_COMMUNITY = 'test'
DETECT_DEVICE = SNMPDetect(CISCO_3850['ip'], snmp_version='v2c', community=SNMP_COMMUNITY)

DEVICE_TYPE = DETECT_DEVICE.autodetect()

if DEVICE_TYPE is None:
    print("Error: No auto device detection for specified device.")
    exit()
else:
    print(DEVICE_TYPE)
    print(DETECT_DEVICE.potential_matches)
    CISCO_3850['device_type'] = DEVICE_TYPE

    # Establish an SSH connection to the device by passing in the device dictionary.
    CONNECT = ConnectHandler(**CISCO_3850)

    # Execute show commands.
    OUTPUT = CONNECT.send_command('show run int GigabitEthernet0/0/0')
    print(OUTPUT)

Let’s execute this –

$ python demo_netmiko_autodetect.py
cisco_ios
{'cisco_ios': 99}
Building configuration...

Current configuration : 99 bytes
!
interface GigabitEthernet0/0/0
 description TEST
 no ip address
 shutdown
 negotiation auto
end

Please note, above program require an additional library to be installed i.e. “pysnmp” which will be discussed later in more details.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.