Netmiko Quickstart

  • April 28, 2016

In this tutorial, you will learn how to easily connect and interact with network devices using Netmiko, a Python library that simplifies SSH management. You will learn why Netmiko is useful, how to install it, how to instantiate device objects, and how to send show and config commands to network devices.

It supports several platforms including:

  • Cisco IOS
  • Cisco IOS-XE
  • Cisco ASA
  • Cisco NX-OS
  • Cisco IOS-XR
  • Cisco WLC (limited testing)
  • Arista vEOS
  • HP ProCurve
  • HP Comware (limited testing)
  • Juniper Junos
  • Brocade VDX (limited testing)
  • F5 LTM (experimental)
  • Huawei (limited testing)
  • A10 (limited testing)
  • Avaya ERS (limited testing)
  • Avaya VSP (limited testing)
  • Dell-Force10 DNOS9 (limited testing)
  • OVS (experimental)
  • Enterasys (experimental)
  • Extreme (experimental)
  • Fortinet (experimental)
  • Alcatel-Lucent SR-OS (experimental)

Installing

Netmiko can be found on GitHub at https://github.com/ktbyers/netmiko.

You can install it from source:

$ git clone https://github.com/ktbyers/netmiko.git
$ cd netmiko
$ python setup.py install

You can also use pip:

$ pip install netmiko

Opening an SSH session

Once you've installed it, you're ready to start.

On your terminal, enter the Python shell:

$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Now, in order to build a device object we need first to import a connection handler from netmiko:

>>> from netmiko import ConnectHandler
>>>

As an example, let's connect to csr1 in the lab environment.

  • Hostname: csr1 (10.0.0.51)
  • Username: ntc
  • Password: ntc123
  • Platform: Cisco IOS

Now, let's establish an SSH session with this device:

>>> platform = 'cisco_ios'
>>> host = 'csr1'
>>> username = 'ntc'
>>> password = 'ntc123'
>>> device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
SSH connection established to csr1:22
Interactive SSH session established
>>>

NOTE: host can either be the IP address or the hostname of your remote device as configured on your machine.

Sending show commands

Now we're ready to interact with our device. For example, let's send a show version command, store its output on a variable and print it:

>>> output = device.send_command('show version')
>>>
>>> print output
Cisco IOS Software, 3600 Software (C3640-JK9S-M), Version 12.4(16), RELEASE SOFTWARE (fc1)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2007 by Cisco Systems, Inc.
Compiled Wed 20-Jun-07 11:43 by prod_rel_team

ROM: ROMMON Emulation Microcode
ROM: 3600 Software (C3640-JK9S-M), Version 12.4(16), RELEASE SOFTWARE (fc1)

Router1 uptime is 51 minutes
System returned to ROM by unknown reload cause - suspect boot_data[BOOT_COUNT] 0x0, BOOT_COUNT 0, BOOTDATA 19
System image file is "tftp://255.255.255.255/unknown"


This product contains cryptographic features and is subject to United
States and local country laws governing import, export, transfer and
use. Delivery of Cisco cryptographic products does not imply
third-party authority to import, export, distribute or use encryption.
Importers, exporters, distributors and users are responsible for
compliance with U.S. and local country laws. By using this product you
agree to comply with applicable laws and regulations. If you are unable
to comply with U.S. and local laws, return this product immediately.

A summary of U.S. laws governing Cisco cryptographic products may be found at:
http://www.cisco.com/wwl/export/crypto/tool/stqrg.html

If you require further assistance please contact us by sending email to
export@cisco.com.

Cisco 3640 (R4700) processor (revision 0xFF) with 187392K/9216K bytes of memory.
Processor board ID FF1045C5
R4700 CPU at 100MHz, Implementation 33, Rev 1.2
3 FastEthernet interfaces
DRAM configuration is 64 bits wide with parity enabled.
253K bytes of NVRAM.
8192K bytes of processor board System flash (Read/Write)
0K bytes of  PCMCIA Slot0 flash (Read/Write)

Configuration register is 0x2102
>>>

Sending Configuration Commands

We can also send configuration commands as well. We can send a command at time or a list of commands.

>>> device.config_mode()
u'config term\nEnter configuration commands, one per line.  End with CNTL/Z.\nrouter(config)#'
>>>
>>> device.send_command('interface fast1/0')
u''
>>> device.send_command('ip addr 10.10.10.10 255.255.255.0')
u''
>>> device.exit_config_mode()
u'end\nrouter#'
>>>
>>> commands = ['interface fa1/0', 'no shut']
>>> device.send_config_set(commands)
u'config term\nEnter configuration commands, one per line.  End with CNTL/Z.\nrouter(config)#interface fa1/0\nrouter(config-if)#no shut\nrouter(config-if)#end\nrouter#'
>>>

Closing the SSH session

Once you're done, you can terminate the SSH session:

>>> device.disconnect()
>>>

For a complete list of all the methods available with Netmiko, you can use the dir function.

>>> dir(device)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_connect_params_dict', '_use_ssh_config', 'alt_host_keys', 'alt_key_file', 'ansi_escape_codes', 'base_prompt', 'check_config_mode', 'check_enable_mode', 'cleanup', 'clear_buffer', 'commit', 'config_mode', 'device_type', 'disable_paging', 'disconnect', 'enable', 'establish_connection', 'exit_config_mode', 'exit_enable_mode', 'find_prompt', 'global_delay_factor', 'ip', 'key_policy', 'normalize_linefeeds', 'password', 'port', 'remote_conn', 'remote_conn_pre', 'secret', 'select_delay_factor', 'send_command', 'send_command_expect', 'send_config_from_file', 'send_config_set', 'session_preparation', 'set_base_prompt', 'special_login_handler', 'ssh_config_file', 'strip_ansi_escape_codes', 'strip_command', 'strip_prompt', 'system_host_keys', 'username', 'wait_for_recv_ready']
>>>
  • Raunak Anand

    HI thanks for awesome stuff. Can you please also suggest how can we run this code for multiple devices from a .txt or .csv file and save the output to a file.