Using Netmiko for SSH and network device interaction

Netmiko (https://github.com/ktbyers/netmiko) is a library in Python that is used extensively an interaction with network devices. This is a multi-vendor library with support for Cisco IOS, NXOS, firewalls, and many other devices. The underlying library of this is Paramiko, which is again used extensively for SSH into various devices.

Netmiko extends the Paramiko ability of SSH to add enhancements, such as going into configuration mode in network routers, sending commands, receiving output based upon the commands, adding enhancements to wait for certain commands to finish executing, and also taking care of yes/no prompts during command execution.

Here's an example of a simple script to log in to the router and show the version:

from netmiko import ConnectHandler

device = ConnectHandler(device_type='cisco_ios', ip='192.168.255.249', username='cisco', password='cisco')
output = device.send_command("show version")
print (output)
device.disconnect()

The output of the execution of code against a router is as follows:

As we can see in the sample code, we call the ConnectHandler function from the Netmiko library, which takes four inputs (platform type, IP address of device, username, and password):

Netmiko works with a variety of vendors. Some of the supported platform types and their abbreviations to be called in Netmiko are:

 

'a10': A10SSH,
'accedian': AccedianSSH,
'alcatel_aos': AlcatelAosSSH,
'alcatel_sros': AlcatelSrosSSH,
'arista_eos': AristaSSH,
'aruba_os': ArubaSSH,
'avaya_ers': AvayaErsSSH,
'avaya_vsp': AvayaVspSSH,
'brocade_fastiron': BrocadeFastironSSH,
'brocade_netiron': BrocadeNetironSSH,
'brocade_nos': BrocadeNosSSH,
'brocade_vdx': BrocadeNosSSH,
'brocade_vyos': VyOSSSH,
'checkpoint_gaia': CheckPointGaiaSSH,
'ciena_saos': CienaSaosSSH,
'cisco_asa': CiscoAsaSSH,
'cisco_ios': CiscoIosBase,
'cisco_nxos': CiscoNxosSSH,
'cisco_s300': CiscoS300SSH,
'cisco_tp': CiscoTpTcCeSSH,
'cisco_wlc': CiscoWlcSSH,
'cisco_xe': CiscoIosBase,
'cisco_xr': CiscoXrSSH,
'dell_force10': DellForce10SSH,
'dell_powerconnect': DellPowerConnectSSH,
'eltex': EltexSSH,
'enterasys': EnterasysSSH,
'extreme': ExtremeSSH,
'extreme_wing': ExtremeWingSSH,
'f5_ltm': F5LtmSSH,
'fortinet': FortinetSSH,
'generic_termserver': TerminalServerSSH,
'hp_comware': HPComwareSSH,
'hp_procurve': HPProcurveSSH,
'huawei': HuaweiSSH,
'juniper': JuniperSSH,
'juniper_junos': JuniperSSH,
'linux': LinuxSSH,
'mellanox_ssh': MellanoxSSH,
'mrv_optiswitch': MrvOptiswitchSSH,
'ovs_linux': OvsLinuxSSH,
'paloalto_panos': PaloAltoPanosSSH,
'pluribus': PluribusSSH,
'quanta_mesh': QuantaMeshSSH,
'ubiquiti_edge': UbiquitiEdgeSSH,
'vyatta_vyos': VyOSSSH,
'vyos': VyOSSSH,

Depending upon the selection of the platform type, Netmiko can understand the returned prompt and the correct way to SSH to the specific device. Once the connection is made, we can send commands to the device using the send method.

Once we get the return value, the value stored in the output variable is displayed, which is the string output of the command that we sent to the device. The last line, which uses the disconnect function, ensures that the connection is terminated cleanly once we are done with our task.

For configuration (example: We need to provide a description to the router interface FastEthernet 0/0), we use Netmiko as shown in the following example:

from netmiko import ConnectHandler

print ("Before config push")
device = ConnectHandler(device_type='cisco_ios', ip='192.168.255.249', username='cisco', password='cisco')
output = device.send_command("show running-config interface fastEthernet 0/0")
print (output)

configcmds=["interface fastEthernet 0/0", "description my test"]
device.send_config_set(configcmds)

print ("After config push")
output = device.send_command("show running-config interface fastEthernet 0/0")
print (output)

device.disconnect()

The output of the execution of the preceding code is as follows:

  • As we can see, for config push we do not have to perform any additional configs but just specify the commands in the same order as we will send them manually to the router in a list, and pass that list as an argument to the send_config_set function.
  • The output in Before config push is a simple output of the FastEthernet0/0 interface, but the output under After config push is now with the description that we configured using the list of commands.

In a similar way, we can pass multiple commands to the router, and Netmiko will go into configuration mode, write those commands to the router, and exit config mode.

If we want to save the configuration, we use the following command after the send_config_set command:

device.send_command("write memory")

This ensures that the router writes the newly pushed config in memory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset