-
Notifications
You must be signed in to change notification settings - Fork 5
/
script.py
89 lines (72 loc) · 2.24 KB
/
script.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
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
'''
Author : Ahmad Rosid Komarudin (ArRosid)
Website : https://arrosid.com/ , http://coretanbocahit.blogspot.co.id/ , https://agunacourse.com/
Email : rosid@idn.id
Fb : https://www.facebook.com/ahmadrosidkomarudin
Github : https://github.com/arrosid
Linkedin: https://www.linkedin.com/in/arrosid/
IG : https://www.instagram.com/ahmadrosidkomarudin/
'''
import yaml
from pprint import pprint
from netmiko import ConnectHandler
def read_yaml(yaml_file):
with open(yaml_file) as f:
inventory = f.read()
inventory_dict = yaml.load(inventory)
return inventory_dict
def device_connection(router_ip):
device = {
"device_type" : "cisco_ios",
"ip" : router_ip,
"username" : "cisco",
"password" : "cisco"
}
conn = ConnectHandler(**device)
return conn
def conf_ip(conn, ip_config):
interface = ip_config['interface']
ip_addr = ip_config['ip_address']
config_list = ['interface {}'.format(interface),
'ip address {}'.format(ip_addr),
'no shutdown']
print conn.send_config_set(config_list)
def conf_dhcp(conn, dhcp_config):
pool = dhcp_config['pool']
network = dhcp_config['network']
gateway = dhcp_config['gateway']
config_list = ['ip dhcp pool {}'.format(pool),
'network {}'.format(network),
'default-router {}'.format(gateway)]
print conn.send_config_set(config_list)
def conf_ospf(conn, ospf_config):
area = ospf_config['area']
network_list = ospf_config['network']
config_list = ['router ospf 1']
for network in network_list:
config_list.append('network {} area {}'.format(network, area))
print conn.send_config_set(config_list)
def main():
yaml_file = 'inventory.yaml'
inventory_dict = read_yaml(yaml_file)
pprint(inventory_dict)
for router in inventory_dict['CORE']:
router_ip = router['host']
print "-------------------------------"
print "Configuring {}".format(router_ip)
print "-------------------------------"
#connection
conn = device_connection(router_ip)
#configure ip address
ip_config = router['int_config']
for conf in ip_config:
conf_ip(conn, conf)
#configure dhcp
dhcp_config = router['dhcp_config']
for config in dhcp_config:
conf_dhcp(conn, config)
#configure ospf
ospf_config = router['ospf_config']
for config in ospf_config:
conf_ospf(conn, config)
main()