├── LICENSE ├── README.md ├── Topology.png ├── inventory.yml └── script.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Ahmad Rosid Komarudin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![published](https://static.production.devnetcloud.com/codeexchange/assets/images/devnet-published.svg)](https://developer.cisco.com/codeexchange/github/repo/arrosid/netconf-static-route) 2 | # Netmiko for OSPF & DHCP Automation 3 | Configure IP Address, DHCP Server and OSPF automatically using Netmiko 4 | 5 | This repository contains two scripts to configure IP Address, DHCP Server, and OSPF automatically using Netmiko. The first script is inventory.yml. It contains data about the router. The second script is a Python script that performs the automation. 6 | 7 | You can automate OSPF & DHCP Configuration with differents topologies using this script. You just need to edit the inventory.yaml to align with the topology that you have. 8 | 9 |

Requires

10 | 13 |

Supports

14 | 17 | 18 |

How to use

19 | 25 | -------------------------------------------------------------------------------- /Topology.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ArRosid/netmiko-ospf-dhcp/b72768943d8754ff3b5c72eea91fe0017eb794c6/Topology.png -------------------------------------------------------------------------------- /inventory.yml: -------------------------------------------------------------------------------- 1 | CORE: 2 | - host: 192.168.122.11 3 | int_config: 4 | - interface: GigabitEthernet3 5 | ip_address: 192.168.10.1 255.255.255.0 6 | - interface: GigabitEthernet4 7 | ip_address: 192.168.11.1 255.255.255.0 8 | - interface: GigabitEthernet2 9 | ip_address: 10.10.12.1 255.255.255.0 10 | dhcp_config: 11 | - pool: network10 12 | network: 192.168.10.0 255.255.255.0 13 | gateway: 192.168.10.1 14 | - pool: network11 15 | network: 192.168.11.0 255.255.255.0 16 | gateway: 192.168.11.1 17 | ospf_config: 18 | - area: 0 19 | network: 20 | - 10.10.12.1 0.0.0.255 21 | - 192.168.10.0 0.0.0.255 22 | - 192.168.11.0 0.0.0.255 23 | 24 | - host: 192.168.122.12 25 | int_config: 26 | - interface: GigabitEthernet3 27 | ip_address: 10.10.23.2 255.255.255.0 28 | - interface: GigabitEthernet4 29 | ip_address: 192.168.20.1 255.255.255.0 30 | - interface: GigabitEthernet2 31 | ip_address: 10.10.12.2 255.255.255.0 32 | dhcp_config: 33 | - pool: network20 34 | network: 192.168.20.0 255.255.255.0 35 | gateway: 192.168.20.1 36 | ospf_config: 37 | - area: 0 38 | network: 39 | - 10.10.12.0 0.0.0.255 40 | - 10.10.23.0 0.0.0.255 41 | - 192.168.20.0 0.0.0.255 42 | 43 | - host: 192.168.122.13 44 | int_config: 45 | - interface: GigabitEthernet3 46 | ip_address: 10.10.34.3 255.255.255.0 47 | - interface: GigabitEthernet4 48 | ip_address: 192.168.30.1 255.255.255.0 49 | - interface: GigabitEthernet2 50 | ip_address: 10.10.23.3 255.255.255.0 51 | dhcp_config: 52 | - pool: network30 53 | network: 192.168.30.0 255.255.255.0 54 | gateway: 192.168.30.1 55 | ospf_config: 56 | - area: 0 57 | network: 58 | - 10.10.23.0 0.0.0.255 59 | - area: 1 60 | network: 61 | - 10.10.34.0 0.0.0.255 62 | - 192.168.30.0 0.0.0.255 63 | 64 | - host: 192.168.122.14 65 | int_config: 66 | - interface: GigabitEthernet3 67 | ip_address: 192.168.40.1 255.255.255.0 68 | - interface: GigabitEthernet4 69 | ip_address: 192.168.41.1 255.255.255.0 70 | - interface: GigabitEthernet2 71 | ip_address: 10.10.34.4 255.255.255.0 72 | dhcp_config: 73 | - pool: network40 74 | network: 192.168.40.0 255.255.255.0 75 | gateway: 192.168.40.1 76 | - pool: network41 77 | network: 192.168.41.0 255.255.255.0 78 | gateway: 192.168.41.1 79 | ospf_config: 80 | - area: 1 81 | network: 82 | - 192.168.40.0 0.0.0.255 83 | - 192.168.41.0 0.0.0.255 84 | - 10.10.34.0 0.0.0.255 85 | -------------------------------------------------------------------------------- /script.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Author : Ahmad Rosid Komarudin (ArRosid) 3 | Website : https://arrosid.com/ , http://coretanbocahit.blogspot.co.id/ , https://agunacourse.com/ 4 | Email : rosid@idn.id 5 | Fb : https://www.facebook.com/ahmadrosidkomarudin 6 | Github : https://github.com/arrosid 7 | Linkedin: https://www.linkedin.com/in/arrosid/ 8 | IG : https://www.instagram.com/ahmadrosidkomarudin/ 9 | ''' 10 | 11 | import yaml 12 | from pprint import pprint 13 | from netmiko import ConnectHandler 14 | 15 | 16 | def read_yaml(yaml_file): 17 | with open(yaml_file) as f: 18 | inventory = f.read() 19 | 20 | inventory_dict = yaml.load(inventory) 21 | return inventory_dict 22 | 23 | def device_connection(router_ip): 24 | device = { 25 | "device_type" : "cisco_ios", 26 | "ip" : router_ip, 27 | "username" : "cisco", 28 | "password" : "cisco" 29 | } 30 | 31 | conn = ConnectHandler(**device) 32 | return conn 33 | 34 | def conf_ip(conn, ip_config): 35 | interface = ip_config['interface'] 36 | ip_addr = ip_config['ip_address'] 37 | config_list = ['interface {}'.format(interface), 38 | 'ip address {}'.format(ip_addr), 39 | 'no shutdown'] 40 | print conn.send_config_set(config_list) 41 | 42 | def conf_dhcp(conn, dhcp_config): 43 | pool = dhcp_config['pool'] 44 | network = dhcp_config['network'] 45 | gateway = dhcp_config['gateway'] 46 | config_list = ['ip dhcp pool {}'.format(pool), 47 | 'network {}'.format(network), 48 | 'default-router {}'.format(gateway)] 49 | print conn.send_config_set(config_list) 50 | 51 | def conf_ospf(conn, ospf_config): 52 | area = ospf_config['area'] 53 | network_list = ospf_config['network'] 54 | config_list = ['router ospf 1'] 55 | for network in network_list: 56 | config_list.append('network {} area {}'.format(network, area)) 57 | 58 | print conn.send_config_set(config_list) 59 | 60 | def main(): 61 | yaml_file = 'inventory.yaml' 62 | inventory_dict = read_yaml(yaml_file) 63 | pprint(inventory_dict) 64 | 65 | for router in inventory_dict['CORE']: 66 | 67 | router_ip = router['host'] 68 | print "-------------------------------" 69 | print "Configuring {}".format(router_ip) 70 | print "-------------------------------" 71 | #connection 72 | conn = device_connection(router_ip) 73 | 74 | #configure ip address 75 | ip_config = router['int_config'] 76 | for conf in ip_config: 77 | conf_ip(conn, conf) 78 | 79 | #configure dhcp 80 | dhcp_config = router['dhcp_config'] 81 | for config in dhcp_config: 82 | conf_dhcp(conn, config) 83 | 84 | #configure ospf 85 | ospf_config = router['ospf_config'] 86 | for config in ospf_config: 87 | conf_ospf(conn, config) 88 | 89 | main() 90 | --------------------------------------------------------------------------------