Home Netmiko
Post
Cancel

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:

1
2
3
4
5
6
7
8
9
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.

1
2
3
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)]

1
2
3
4
5
6
7
8
9
    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 = "nn".join(device_result)  
    connection.disconnect()  
    device_result_string += "nElapsed time: " + str(datetime.now() - start_time)  
    yield device_result_string  

def main():

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

if name == “main”:
main()

Results:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
$ 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.

This post is licensed under CC BY 4.0 by the author.

GNS3, IOU-images & VM Workstation

Netmiko part 2