NETMIKO:
Netmiko is a Python-based SSH client. Now, rather than connecting to each device separately from a GUI client and
issuing the desired commands one by one, the whole process can be automated with Python, and the commands can
be applied to all devices at once if necessary. Netmiko is actually a wrapper for Paramiko (the de facto SSH library in
Python) with some added network intelligence for sending commands, logging in to devices, and so on, which is why
Netmiko is becoming the de facto SSH client for networking devices.
Netmiko supports more than 20 vendors, most notably Cisco, with Cisco IOS, Cisco IOS XE, and Cisco Nexus Operating
System (NX-OS).
Netmiko has many functions (methods) that developers can use while interacting with the device. Because it is a very
long list, the most useful ones are discussed here with a detailed description and applicable use cases.
• ConnectHandler(): This Python class initiates a connection with the device. You will need to provide an IP
address, username, password, and device type information to successfully initiate a connection.
Example:
>>> device = ConnectHandler(host="csr1kv1", username="cisco", password="cisco", device_type="cisco_xe")
• is_alive(): This method determines if the connection with the device is alive and returns True or False.
Example:
>>> device.is_alive()
True
• establish_connection(): In cases when the device is disconnected manually or automatically due to a
connection timeout (default 60 seconds), this method can be used to reinitiate the connection.
>>> device.is_alive()
False
>>> device.establish_connection()
>>> device.is_alive()
True
• disconnect(): This method is used to manually disconnect the session.
Example:
>>> device.is_alive()
True
>>> device.disconnect()
>>> device.is_alive()
False
• send_command(<VALID_COMMAND>): Use this method to send an operational show command to the
device. It must be a single string that contains a valid command.
Example:
>>> device.send_command("show version")
'Cisco IOS XE Software, Version 16.09.03\nCisco IOS Software [Fuji], Virtual XE Software
(X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.9.3, RELEASE SOFTWARE (fc2)\nTechnical Support:
http://www.cisco.com/techsupport\nCopyright (c) 1986-2019 by Cisco Systems, Inc.\nCompiled Wed 20-
Mar-19 07:56 by mcpre\n\n\nCisco IOS-XE software, Copyright (c) 2005-2019 by cisco Systems, Inc.\nAll
rights reserved. Certain components of Cisco IOS-XE software are\nlicensed under the GNU General Public
License ("GPL")
<... output omitted ...>
• send_config_from_file(<PATH_TO_THE_FILE>): This method can be used to apply a configuration file to the
device. You must supply a full path to the configuration file in the argument.
Example:
% cat interface_conf.cfg
interface GigabitEthernet1
description SALES
>>> device.send_config_file( "/home/student/config_files/interface_conf.cfg")
'config term\nEnter configuration commands, one per line. End with CNTL/Z.\ncsr1kv1(config)#interface
GigabitEthernet1\ncsr1kv1(config-if)# description SALES\ncsr1kv1(config-if)#end\ncsr1kv1#'
• send_config_set([<COMMAND_1>, <COMMAND_2>]): This method can be used to send a list of configuration
commands to the device. The list must contain commands as a string and be in proper order.
Example:
>>> print(interface_config)
['interface GigabitEthernet1', 'description HR']
>>> device. send_config_set(interface_config)
'config term\nEnter configuration commands, one per line. End with CNTL/Z.\ncsr1 kv1(config)#interface
GigabitEthernet1\ncsr1kv1(config-if)#description HR\ncsr1kv1(config-if)#end\ncsr1kv1#'
• session_timeout: This variable defines the number of seconds after which the session should time out. It can
be defined with ConnectHandler as an extra argument or can be changed after the connection is initiated.
The default value is 60 seconds.
Example:
>>> device = ConnectHandler(host="csr1kv1", username="cisco", password="cisco", device_type="cisco_xe",
session_timeout=120)
>>> device.session_timeout
120
>>> device.session_timeout = 180
>>> device.session_timeout
180