-
Notifications
You must be signed in to change notification settings - Fork 2
/
merge-configs.py
43 lines (31 loc) · 1.14 KB
/
merge-configs.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
#!/usr/bin/python
import csv
import os
# Open CSV as dictionary
management_file = open('management.csv')
management_dict = csv.DictReader(management_file)
h_list = []
try:
for row in management_dict:
h_var = (row["hostname"])
var={row["hostname"] : row}
h_list.append(row["hostname"])
os.chdir('./files')
for x in h_list:
mgmt = open(x+".mgmt")
mgmt_contents = mgmt.read()
mgmt.close()
vlans = open(x+".vlans")
vlans_contents = vlans.read()
vlans.close()
switchports = open(x+".switchports")
switchports_contents = switchports.read()
switchports.close()
merged = open(x+".config", "w") # open in `w` mode to write
merged.write(mgmt_contents + vlans_contents + switchports_contents) # concatenate the contents
merged.close()
print("-------------------------------------------------------")
print("Configuration files merged as ./files/<hostname>.config")
print("-------------------------------------------------------")
except Exception as e:
print("An error has occurred - merging has not happened correctly")