Introduction

Netmiko is a python library created by a living legend amongst network engineers, Kirk Byers. Netmiko aims to simplify SSH connections to network devices with python by extending the paramiko library to deal with the idiosyncrasies of SSHing to our favorite network gear.

Netmiko has a huge amount of support within the network community, with over 50 contributers and a huge user base ensuring that it is well tested. There is also a large number of device vendors supported making it an excellent tool when using SSH as an access and configuration method.

Lab Environment

In this lab I will setup a Centos minimal server to install Netmiko then connect to and configure both a Cisco router and ASA.

Topology

netmiko-basic

Code Versions
  • Centos/7 - 1702.01
  • ASAv - 9.8.1
  • CSR1000v - 3.15.00.S.155-2.S
  • Python - 2.7.5

Installation

Install requied YUM packages


sudo yum install -y epel-release
sudo yum group install -y "Development tools"
sudo yum install -y gcc libffi-devel python2-pip python-devel openssl-devel sshpass python-setuptools python-virtualenv
            

Upgrade pip and setuptools


sudo pip install -U pip setuptools
            

Create a virtual environment and install Netmiko


# as normal user
mkdir envs
virtualenv envs/netmiko-test
source netmiko-test/bin/activate

# inside virtual environment
pip install netmiko
            

Usage

Connecting to a device requires you to know a few things about the device. IE the device type and the login credentials.


# import library
from netmiko import ConnectHandler

# connection properties described in a dictionary
csr = {
    'device_type': 'cisco_ios',
    'ip': '169.254.1.12',
    'username': 'vagrant',
    'password': 'vagrant',
}

asa = {
    'device_type': 'cisco_asa',
    'ip': '169.254.1.11',
    'username': 'vagrant',
    'password': 'vagrant',
}
            

Connect to the devices now the connection properties are setup.


# the ** unpacks a dictionary
csr_session = ConnectHandler(**csr)

asa_session = ConnectHandler(**asa)
            

Now that we are connected its time to run some commands.


print(csr_session.send_command('show ip int brief'))
# output
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       10.0.2.15       YES DHCP   up                    up      
GigabitEthernet2       169.254.1.12    YES manual up                    up

print(asa_session.send_command('show int ip brie'))
# output
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         169.254.1.11    YES manual up                    up  
Management0/0              10.0.2.15       YES DHCP   up                    up
          

Send a list of commands.


commands = [
    'show nameif',
    'show hostname',
]

result = [asa_session.send_command(i) for i in commands]

for i in result:
    print(i)

# output
Interface                Name                     Security
GigabitEthernet0/0       inside                   100
Management0/0            management                 0

asa
          

Send a list of configuration commands.


commands = [
    'ntp server 8.8.8.8',
    'logging host 10.10.10.10',
]

result = csr_session.send_config_set(commands)

print(result)

# output
config term
Enter configuration commands, one per line.  End with CNTL/Z.
csr(config)# ntp server 8.8.8.8
csr(config)# logging host 10.10.10.10
csr(config)# end
csr#
          

Summary

As you can see, Netmiko helps to put a programatic interface around SSHing to a network device. Its a great tool for when there is no API available but you do have an SSH interface. For more information check out the github repo and Kirk's website both linked bellow.

Links

https://github.com/ktbyers/netmiko
https://pynet.twb-tech.com/






















Published: 2017-08-14