-
Notifications
You must be signed in to change notification settings - Fork 1
/
portchan.py
executable file
·97 lines (77 loc) · 2.79 KB
/
portchan.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
90
91
92
93
94
95
96
#!/usr/bin/env python3
import os
import argparse
import json
import jinja2
# These are added to all port channels
DEFAULT_VLANS = [1]
if not os.path.isdir('output'):
os.mkdir('output')
parser = argparse.ArgumentParser('portchan.py')
parser.add_argument('-p', '--prefix', required=True)
parser.add_argument('-f', '--first', type=int, default=5)
parser.add_argument('-m', '--mode', default='desirable')
parser.add_argument('-d', '--distsw', default=[], action='append')
args = parser.parse_args()
prefix = args.prefix
first = args.first
mode = args.mode
distswlist = args.distsw
class Switch(object):
def __init__(self, switch, vlans, ponum, mode, slaves):
self.switch = switch
self.vlans = vlans
self.ponum = ponum
self.mode = mode
self.slaves = slaves
def gen_port(prefix, distport):
module, portnum = distport.split('/')
if not prefix[-1].isdigit():
prefix += module
port = '{}/{}'.format(prefix, portnum)
return port
with open('config.json', 'r') as f:
config = json.load(f)
for distsw in config['mapping'].values():
distswname = distsw['name']
if distswlist and not distswname in distswlist:
continue
ponum = first
switchdict = {}
for distport, accessw in distsw['maps'].items():
if not accessw in switchdict.keys():
switchdict[accessw] = [gen_port(prefix, distport)]
else:
switchdict[accessw].append(gen_port(prefix, distport))
switches = []
for switch, distports in switchdict.items():
swconf = config['switches'][switch]
if '_include' in swconf.keys():
swgrp = config['switchgroups'][swconf['_include']]
for option in swgrp:
if not option in swconf.keys():
swconf[option] = swgrp[option]
vlanlist = set()
for vlan in DEFAULT_VLANS:
vlanlist.add(vlan)
vlanlist.add(swconf['mgmt_vlan'])
vlanlist.add(swconf['visitor_access_vlan'])
vlanlist = list(vlanlist)
vlanlist.sort()
vlans = ','.join(str(vlan) for vlan in vlanlist)
switches.append(Switch(switch, vlans, ponum, mode, distports))
ponum+=1
with open('portchan.j2') as f:
tmpl = jinja2.Template(f.read())
rendered = tmpl.render(switches=switches)
outfile = 'output/portchan-{}.txt'.format(distswname)
with open(outfile, 'w') as f:
f.write(rendered)
print('Configuration written into file {}'.format(outfile))
with open('cabling.j2') as f:
tmpl = jinja2.Template(f.read())
rendered = tmpl.render(name=distswname, switches=switchdict)
outfile = 'output/cabling-{}.txt'.format(distswname)
with open(outfile, 'w') as f:
f.write(rendered)
print('Cabling info written into file {}'.format(outfile))