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.



Code Versions

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


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


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': '',
    'username': 'vagrant',
    'password': 'vagrant',

asa = {
    'device_type': 'cisco_asa',
    'ip': '',
    '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       YES DHCP   up                    up
GigabitEthernet2    YES manual up                    up

print(asa_session.send_command('show int ip brie'))
# output
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0    YES manual up                    up
Management0/0           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:

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


Send a list of configuration commands.


commands = [
    'ntp server',
    'logging host',

result = csr_session.send_config_set(commands)


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


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.


python netmiko