-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator.py
121 lines (104 loc) · 3.31 KB
/
generator.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
# Author: Micah Martin
import json
#import yaml
import sys
try:
from termcolor import colored as c
except:
def c(text, *ars, **kwargs):
return text
COLOR = 'blue'
def prompt(text="", color=COLOR):
'''
Generate a prompt with the given color and return the output
'''
return input(c(text, color, attrs=('bold',)))
def getTeamRange():
'''
Generate a list of the team numbers based on starting and ending values
'''
while True:
try:
# Get the input
start = int(prompt("Starting team number: ", COLOR))
end = int(prompt("Ending team number: ", COLOR))
# Generate an array with the team numbers
teams = [i for i in range(start, end+1)]
# Validate that the input is correct
print(teams)
isGood = prompt("Correct? [Y/n]", "green")
if isGood in ("", "y", "yes"):
# If its right, return the array
return teams
# Loop if not correct
except Exception as E:
print("Invalid range")
# Loop if there is any errors
def addHost(network):
'''
Get the input information for a host on the network
'''
while True:
ip = input(c('IP: ', COLOR, attrs=('bold',))+network+".")
hostname = prompt("Hostname: ")
os = prompt("OS: ")
isGood = prompt("Correct? [Y/n]", "green")
if isGood in ("", "y", "yes"):
# If its right, return the host info
return {'ip': ip, 'name': hostname, 'os': os}
def addNetwork():
'''
Add a network to the config. add hosts to each network
'''
data = {}
# Get the network ip. Keep trying until it is right
while True:
try:
ip = prompt(
'Network IP (e.g. "10.2.x.0"): ')
ip = ".".join(ip.split(".")[:3]).lower()
data['ip'] = ip
# Make sure there is an 'x' in the ip
ip.index("x")
break
except Exception as E:
print("Invalid Network Name")
# get the network name
data['name'] = prompt('Network name (e.g. "cloud"): ')
# Add hosts to the network
data['hosts'] = []
while True:
# Keep adding hosts until we are done
newHost = prompt("Add a host to this network? [Y/n]", "green")
if newHost not in ("", "y", "yes"):
break
# Create a new host and add it to the dataset
data['hosts'] += [addHost(ip)]
return data
def addNetworks():
data = []
while True:
# Keep adding networks until we are done
newNetwork = prompt("Add network? [Y/n]", "green")
if newNetwork not in ("", "y", "yes"):
break
# Create a new network and add it to the dataset
data += [addNetwork()]
return data
def main():
'''
Call all the generate functions and build a json config
'''
config = {}
# Get the teams
config['teams'] = getTeamRange()
# Get the networks
config['networks'] = addNetworks()
print(json.dumps(config, indent=4))
with open("newtopology.json", "w") as fil:
fil.write(json.dumps(config, indent=2))
fil.write("\n")
print("topology saved to newtopology.json", file=sys.stderr)
if __name__ == '__main__':
main()