Netmiko

I’ve been playing around with Netmiko this weekend as I would like to try and rewrite all my old bash- & expect-scripts at work to python instead (if I ever will find the time)… 🙂

Just starting out I felt like it was incredibly slow however and was pretty disappointed when for ex. pushing out config from a file to multiple devices compared to expect. Perhaps I did something wrong but it also looks like there’s several ways to speed things up so I thought I should give it another go.

I’m using simple topology with three routers and some basic config allowing ssh-login:

  • R1 192.168.15.10
  • R2 192.168.15.11
  • R3 192.168.15.12

login_supersimple.py

We start by importing netmiko’s ConnectHandler, specify some “show commands” we’d like to run and lastly we need to specify what type of device we’re going to connect to and login credentials.

One very simple way to do this is using a dictionary for each device:

from netmiko import ConnectHandler
from getpass import getpass

R1 = {
    "host": "192.168.15.10",
    "username": "joco02",
    "password": getpass(),
    "device_type": "cisco_ios",
}

We then use the dictionary when calling our ConnectHandler to provide all necessary info for logging in and setting the correct device type.

connection = ConnectHandler(**R1)
print(connection.find_prompt())
connection.disconnect()

Results:

$ python login_supersimple.py
Password:
R1#

login_simple.py

We can also skip using the dictionary and specify connection parameters manually instead. Here i’m sending multiple commands to multiple devices using only lists. In a later post I thought we could try and use a YAML-file for our devices and connection parameters.

from netmiko import ConnectHandler
from datetime import datetime
from getpass import getpass

SH_COMMANDS = [
"show ip route", "show ip interface brief"
]

DEVICES = ["192.168.15.10", "192.168.15.11", "192.168.15.12"]
USERNAME = "joco02"
PASSWORD = getpass()

def show_commands(devices, commands):
for device in devices:
start_time = datetime.now()
connection = ConnectHandler(
device_type="cisco_ios", host=device,
username=USERNAME, password=PASSWORD)
hostname = connection.find_prompt()
device_result = ["{0} {1} {0}".format("=" * 20, hostname)]

for command in commands:
command_result = connection.send_command(command)
device_result.append("{0} {1} {0}".format("=" * 20, command))
device_result.append(command_result)

device_result_string = "\n\n".join(device_result)
connection.disconnect()
device_result_string += "\nElapsed time: " + str(datetime.now() - start_time)
yield device_result_string

def main():

results = show_commands(DEVICES, SH_COMMANDS)
for result in results:
print(result)

if __name__ == "__main__":
main()

Results:

$ python login_simple.py
Password:
==================== R1# ====================

==================== show ip route ====================

Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
       E1 - OSPF external type 1, E2 - OSPF external type 2
       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
       ia - IS-IS inter area, * - candidate default, U - per-user static route
       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
       a - application route
       + - replicated route, % - next hop override

Gateway of last resort is 192.168.15.254 to network 0.0.0.0

S*    0.0.0.0/0 [1/0] via 192.168.15.254
      10.0.0.0/8 is variably subnetted, 16 subnets, 2 masks
C        10.10.0.0/24 is directly connected, Loopback0
L        10.10.0.1/32 is directly connected, Loopback0
C        10.10.1.0/24 is directly connected, Loopback1
L        10.10.1.1/32 is directly connected, Loopback1
C        10.10.2.0/24 is directly connected, Loopback2
L        10.10.2.1/32 is directly connected, Loopback2
C        10.10.3.0/24 is directly connected, Loopback3
L        10.10.3.1/32 is directly connected, Loopback3
O        10.11.0.0/24 [110/11] via 192.168.15.11, 23:58:37, Ethernet0/0
O        10.11.1.0/24 [110/11] via 192.168.15.11, 23:58:27, Ethernet0/0
O        10.11.2.0/24 [110/11] via 192.168.15.11, 23:58:27, Ethernet0/0
O        10.11.3.0/24 [110/11] via 192.168.15.11, 23:58:27, Ethernet0/0
O        10.12.0.0/24 [110/11] via 192.168.15.12, 23:58:15, Ethernet0/0
O        10.12.1.0/24 [110/11] via 192.168.15.12, 23:58:05, Ethernet0/0
O        10.12.2.0/24 [110/11] via 192.168.15.12, 23:58:05, Ethernet0/0
O        10.12.3.0/24 [110/11] via 192.168.15.12, 23:58:05, Ethernet0/0
      192.168.15.0/24 is variably subnetted, 2 subnets, 2 masks
C        192.168.15.0/24 is directly connected, Ethernet0/0
L        192.168.15.10/32 is directly connected, Ethernet0/0

==================== show ip interface brief ====================

Interface                  IP-Address      OK? Method Status                Protocol
Ethernet0/0                192.168.15.10   YES manual up                    up
Ethernet0/1                unassigned      YES NVRAM  administratively down down
Ethernet0/2                unassigned      YES NVRAM  administratively down down
Ethernet0/3                unassigned      YES NVRAM  administratively down down
Ethernet1/0                unassigned      YES NVRAM  administratively down down
Ethernet1/1                unassigned      YES NVRAM  administratively down down
Ethernet1/2                unassigned      YES NVRAM  administratively down down
Ethernet1/3                unassigned      YES NVRAM  administratively down down
Serial2/0                  unassigned      YES NVRAM  administratively down down
Serial2/1                  unassigned      YES NVRAM  administratively down down
Serial2/2                  unassigned      YES NVRAM  administratively down down
Serial2/3                  unassigned      YES NVRAM  administratively down down
Serial3/0                  unassigned      YES NVRAM  administratively down down
Serial3/1                  unassigned      YES NVRAM  administratively down down
Serial3/2                  unassigned      YES NVRAM  administratively down down
Serial3/3                  unassigned      YES NVRAM  administratively down down
Loopback0                  10.10.0.1       YES manual up                    up
Loopback1                  10.10.1.1       YES manual up                    up
Loopback2                  10.10.2.1       YES manual up                    up
Loopback3                  10.10.3.1       YES manual up                    up
Elapsed time: 0:00:06.128156
==================== R2# ====================

==================== show ip route ====================

Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
       E1 - OSPF external type 1, E2 - OSPF external type 2
       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
       ia - IS-IS inter area, * - candidate default, U - per-user static route
       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
       a - application route
       + - replicated route, % - next hop override

Gateway of last resort is 192.168.15.254 to network 0.0.0.0

S*    0.0.0.0/0 [1/0] via 192.168.15.254
      10.0.0.0/8 is variably subnetted, 16 subnets, 2 masks
O        10.10.0.0/24 [110/11] via 192.168.15.10, 1d00h, Ethernet0/0
O        10.10.1.0/24 [110/11] via 192.168.15.10, 23:58:53, Ethernet0/0
O        10.10.2.0/24 [110/11] via 192.168.15.10, 23:58:53, Ethernet0/0
O        10.10.3.0/24 [110/11] via 192.168.15.10, 23:58:43, Ethernet0/0
C        10.11.0.0/24 is directly connected, Loopback0
L        10.11.0.1/32 is directly connected, Loopback0
C        10.11.1.0/24 is directly connected, Loopback1
L        10.11.1.1/32 is directly connected, Loopback1
C        10.11.2.0/24 is directly connected, Loopback2
L        10.11.2.1/32 is directly connected, Loopback2
C        10.11.3.0/24 is directly connected, Loopback3
L        10.11.3.1/32 is directly connected, Loopback3
O        10.12.0.0/24 [110/11] via 192.168.15.12, 23:58:21, Ethernet0/0
O        10.12.1.0/24 [110/11] via 192.168.15.12, 23:58:11, Ethernet0/0
O        10.12.2.0/24 [110/11] via 192.168.15.12, 23:58:11, Ethernet0/0
O        10.12.3.0/24 [110/11] via 192.168.15.12, 23:58:11, Ethernet0/0
      192.168.15.0/24 is variably subnetted, 2 subnets, 2 masks
C        192.168.15.0/24 is directly connected, Ethernet0/0
L        192.168.15.11/32 is directly connected, Ethernet0/0

==================== show ip interface brief ====================

Interface                  IP-Address      OK? Method Status                Protocol
Ethernet0/0                192.168.15.11   YES manual up                    up
Ethernet0/1                unassigned      YES NVRAM  administratively down down
Ethernet0/2                unassigned      YES NVRAM  administratively down down
Ethernet0/3                unassigned      YES NVRAM  administratively down down
Ethernet1/0                unassigned      YES NVRAM  administratively down down
Ethernet1/1                unassigned      YES NVRAM  administratively down down
Ethernet1/2                unassigned      YES NVRAM  administratively down down
Ethernet1/3                unassigned      YES NVRAM  administratively down down
Serial2/0                  unassigned      YES NVRAM  administratively down down
Serial2/1                  unassigned      YES NVRAM  administratively down down
Serial2/2                  unassigned      YES NVRAM  administratively down down
Serial2/3                  unassigned      YES NVRAM  administratively down down
Serial3/0                  unassigned      YES NVRAM  administratively down down
Serial3/1                  unassigned      YES NVRAM  administratively down down
Serial3/2                  unassigned      YES NVRAM  administratively down down
Serial3/3                  unassigned      YES NVRAM  administratively down down
Loopback0                  10.11.0.1       YES manual up                    up
Loopback1                  10.11.1.1       YES manual up                    up
Loopback2                  10.11.2.1       YES manual up                    up
Loopback3                  10.11.3.1       YES manual up                    up
Elapsed time: 0:00:06.205272
==================== R3# ====================

==================== show ip route ====================

Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
       E1 - OSPF external type 1, E2 - OSPF external type 2
       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
       ia - IS-IS inter area, * - candidate default, U - per-user static route
       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
       a - application route
       + - replicated route, % - next hop override

Gateway of last resort is 192.168.15.254 to network 0.0.0.0

S*    0.0.0.0/0 [1/0] via 192.168.15.254
      10.0.0.0/8 is variably subnetted, 16 subnets, 2 masks
O        10.10.0.0/24 [110/11] via 192.168.15.10, 1d00h, Ethernet0/0
O        10.10.1.0/24 [110/11] via 192.168.15.10, 23:58:59, Ethernet0/0
O        10.10.2.0/24 [110/11] via 192.168.15.10, 23:58:49, Ethernet0/0
O        10.10.3.0/24 [110/11] via 192.168.15.10, 23:58:49, Ethernet0/0
O        10.11.0.0/24 [110/11] via 192.168.15.11, 23:58:49, Ethernet0/0
O        10.11.1.0/24 [110/11] via 192.168.15.11, 23:58:39, Ethernet0/0
O        10.11.2.0/24 [110/11] via 192.168.15.11, 23:58:39, Ethernet0/0
O        10.11.3.0/24 [110/11] via 192.168.15.11, 23:58:39, Ethernet0/0
C        10.12.0.0/24 is directly connected, Loopback0
L        10.12.0.1/32 is directly connected, Loopback0
C        10.12.1.0/24 is directly connected, Loopback1
L        10.12.1.1/32 is directly connected, Loopback1
C        10.12.2.0/24 is directly connected, Loopback2
L        10.12.2.1/32 is directly connected, Loopback2
C        10.12.3.0/24 is directly connected, Loopback3
L        10.12.3.1/32 is directly connected, Loopback3
      192.168.15.0/24 is variably subnetted, 2 subnets, 2 masks
C        192.168.15.0/24 is directly connected, Ethernet0/0
L        192.168.15.12/32 is directly connected, Ethernet0/0

==================== show ip interface brief ====================

Interface                  IP-Address      OK? Method Status                Protocol
Ethernet0/0                192.168.15.12   YES manual up                    up
Ethernet0/1                unassigned      YES NVRAM  administratively down down
Ethernet0/2                unassigned      YES NVRAM  administratively down down
Ethernet0/3                unassigned      YES NVRAM  administratively down down
Ethernet1/0                unassigned      YES NVRAM  administratively down down
Ethernet1/1                unassigned      YES NVRAM  administratively down down
Ethernet1/2                unassigned      YES NVRAM  administratively down down
Ethernet1/3                unassigned      YES NVRAM  administratively down down
Serial2/0                  unassigned      YES NVRAM  administratively down down
Serial2/1                  unassigned      YES NVRAM  administratively down down
Serial2/2                  unassigned      YES NVRAM  administratively down down
Serial2/3                  unassigned      YES NVRAM  administratively down down
Serial3/0                  unassigned      YES NVRAM  administratively down down
Serial3/1                  unassigned      YES NVRAM  administratively down down
Serial3/2                  unassigned      YES NVRAM  administratively down down
Serial3/3                  unassigned      YES NVRAM  administratively down down
Loopback0                  10.12.0.1       YES manual up                    up
Loopback1                  10.12.1.1       YES manual up                    up
Loopback2                  10.12.2.1       YES manual up                    up
Loopback3                  10.12.3.1       YES manual up                    up
Elapsed time: 0:00:05.982471

Around 6 seconds per device isn’t that bad when we’re not even using threading or asyncio to speed things up. There is probably other parameters that can be tuned inside netmiko as well, i’ve seen delay-values that defaults to 1 second for example but we’ll save that for a later post.

Lämna ett svar

E-postadressen publiceras inte. Obligatoriska fält är märkta *