Intro

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

blog/netmiko-getting-started/netmiko-basic.svg

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

cmd
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

cmd
sudo pip install -U pip setuptools

Create a virtual environment and install Netmiko

cmd
# 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.

python
# 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.

python
# the ** unpacks a dictionary

csr_session = ConnectHandler(**csr)

asa_session = ConnectHandler(**asa)

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

python
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.

python
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.

python
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#

Outro

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.