Home Netmiko part 2
Post
Cancel

Netmiko part 2

A follow-up on the latest post about Netmiko where we logged in to our devices and did some show commands, let’s continue and do a very common thing in production networks with many devices - pushing out new config from a template.

I’ll be using our latest login_simple.py from last post as a base and just modify it slightly. First we create our config-file that we want in all our devices., let’s say we want to rollout a new route-map in our network.

config_template.txt

1
2
3
4
5
6
7
8
ip prefix-list DEFAULT-ROUTE seq 5 permit 0.0.0.0/0
!
route-map DEFAULT-INJECT permit 10
 match ip address prefix-list DEFAULT-ROUTE
 set extcommunity rt 300:301
!
route-map DEFAULT-INJECT permit 20
 set extcommunity rt 300:300

In netmiko we can then use “send_config_from_file” when we’ve logged in to our device(s), super simple! Notice that we don’t have to manage the context in our router (enable/conf t etc), netmiko does it for us.

conf_from_file.py

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
from netmiko import ConnectHandler
from datetime import datetime
from getpass import getpass

DEVICES = ["192.168.15.10", "192.168.15.11", "192.168.15.12"]
USERNAME = "joco02"
PASSWORD = getpass()
FILE = 'config_template.txt'

def config_commands(devices, file):
    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)]

        command_result = connection.send_config_from_file(FILE)
        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():

    results = config_commands(DEVICES, FILE)
    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
$ python conf_from_file.py
Password:
==================== R1# ====================

config term
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#ip prefix-list DEFAULT-ROUTE seq 5 permit 0.0.0.0/0
R1(config)#!
R1(config)#route-map DEFAULT-INJECT permit 10
R1(config-route-map)# match ip address prefix-list DEFAULT-ROUTE
R1(config-route-map)# set extcommunity rt 300:301
R1(config-route-map)#!
R1(config-route-map)#route-map DEFAULT-INJECT permit 20
R1(config-route-map)# set extcommunity rt 300:300
R1(config-route-map)#end
R1#
Elapsed time: 0:00:08.875642
==================== R2# ====================

config term
Enter configuration commands, one per line.  End with CNTL/Z.
R2(config)#ip prefix-list DEFAULT-ROUTE seq 5 permit 0.0.0.0/0
R2(config)#!
R2(config)#route-map DEFAULT-INJECT permit 10
R2(config-route-map)# match ip address prefix-list DEFAULT-ROUTE
R2(config-route-map)# set extcommunity rt 300:301
R2(config-route-map)#!
R2(config-route-map)#route-map DEFAULT-INJECT permit 20
R2(config-route-map)# set extcommunity rt 300:300
R2(config-route-map)#end
R2#
Elapsed time: 0:00:08.799664
==================== R3# ====================

config term
Enter configuration commands, one per line.  End with CNTL/Z.
R3(config)#ip prefix-list DEFAULT-ROUTE seq 5 permit 0.0.0.0/0
R3(config)#!
R3(config)#route-map DEFAULT-INJECT permit 10
R3(config-route-map)# match ip address prefix-list DEFAULT-ROUTE
R3(config-route-map)# set extcommunity rt 300:301
R3(config-route-map)#!
R3(config-route-map)#route-map DEFAULT-INJECT permit 20
R3(config-route-map)# set extcommunity rt 300:300
R3(config-route-map)#end
R3#
Elapsed time: 0:00:09.067742

We can now easily just change the config in our template and run the script again when it’s time for another rollout.

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

Netmiko

Netmiko part 3 - YAML