1 | #!/usr/bin/env python
|
---|
2 | '''
|
---|
3 | CLI interface which configures and makes new nodes with the most commonly
|
---|
4 | used commands.
|
---|
5 |
|
---|
6 | Rick van der Zwet <info@rickvanderzwet.nl>
|
---|
7 | '''
|
---|
8 |
|
---|
9 | import getrange
|
---|
10 | import gformat
|
---|
11 | import os
|
---|
12 | import readline
|
---|
13 | import sys
|
---|
14 |
|
---|
15 |
|
---|
16 | print "WARNING I AM NOT PRODUCTION CODE YET!" + __doc__
|
---|
17 |
|
---|
18 | DEBUG = "-g" in sys.argv
|
---|
19 |
|
---|
20 | def rlprompt(prompt, prefill=''):
|
---|
21 | ''' Get line with some boring default '''
|
---|
22 | prompt = "%s [%s]: " % (prompt, prefill)
|
---|
23 | if DEBUG:
|
---|
24 | print prompt
|
---|
25 | return prefill
|
---|
26 | return raw_input(prompt) or prefill
|
---|
27 |
|
---|
28 | def rlinput(prompt, prefill=''):
|
---|
29 | ''' Get line with some editable boring default '''
|
---|
30 | if DEBUG:
|
---|
31 | print "%s [%s]:" % (prompt, prefill)
|
---|
32 | return prefill
|
---|
33 | readline.set_startup_hook(lambda: readline.insert_text(prefill))
|
---|
34 | try:
|
---|
35 | return raw_input(prompt + ": ")
|
---|
36 | finally:
|
---|
37 | readline.set_startup_hook()
|
---|
38 |
|
---|
39 | def rlinput_float(prompt, prefill=0):
|
---|
40 | while True:
|
---|
41 | try:
|
---|
42 | prefill = rlinput(prompt, prefill)
|
---|
43 | return float(prefill)
|
---|
44 | except ValueError:
|
---|
45 | print "Invalid Number!"
|
---|
46 | pass
|
---|
47 |
|
---|
48 | def rlinput_choices(prompt, choices, prefill=''):
|
---|
49 | if DEBUG:
|
---|
50 | print '%s [%s]:' % (prompt, prefill)
|
---|
51 | return prefill
|
---|
52 | while True:
|
---|
53 | filtered_choices = sorted(list(set([x for x in choices if (str(prefill).lower() in str(x).lower())])))
|
---|
54 | if len(filtered_choices) == 1:
|
---|
55 | return filtered_choices[0]
|
---|
56 | elif len(filtered_choices) == 0:
|
---|
57 | filtered_choices = choices
|
---|
58 | prefill = rlinput(prompt + ' ' + str(filtered_choices), prefill)
|
---|
59 | if prefill in prompt:
|
---|
60 | return prefill
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 |
|
---|
65 |
|
---|
66 |
|
---|
67 | def newNode():
|
---|
68 | datadump = {
|
---|
69 | 'nodename' : "New",
|
---|
70 | 'location' : "1234 AB, Leiden",
|
---|
71 | 'nodetype' : "Hybrid",
|
---|
72 | 'latitude' : "52.",
|
---|
73 | 'longitude' : "4.",
|
---|
74 | }
|
---|
75 | datadump['nodename'] = rlprompt("Nodename", datadump['nodename'])
|
---|
76 | datadump['location'] = rlprompt("Location", datadump['location'])
|
---|
77 | datadump['nodetype'] = rlprompt("Nodetype", datadump['nodetype'])
|
---|
78 | datadump['latitude'] = rlinput_float("Latitude", datadump['latitude'])
|
---|
79 | datadump['longitude'] = rlinput_float("Longitude", datadump['longitude'])
|
---|
80 | datadump['autogen_item'] = datadump['nodetype'] + datadump['nodename']
|
---|
81 |
|
---|
82 | print "Generating the Master IP range..."
|
---|
83 | master_network = getrange.get_ranges(False, 24, 1)[0]
|
---|
84 | master_ip = getrange.showaddr(master_network + 1)
|
---|
85 | datadump['master_ip'] = master_ip
|
---|
86 |
|
---|
87 | def new_access_point():
|
---|
88 | ifname = rlinput("Interface name", "ath0")
|
---|
89 | datadump['iface_' + ifname] = {
|
---|
90 | 'ip' : rlinput(" - IP", master_ip + "/26"),
|
---|
91 | 'dhcp' : rlinput(" - DHCP range", "10-60"),
|
---|
92 | 'ssid' : rlinput(" - SSID", "ap-WirelessLeiden-" + datadump['nodename']),
|
---|
93 | 'mode' : 'ap',
|
---|
94 | 'type' : '11b',
|
---|
95 | 'comment' : rlinput(" - Comment", "Omni voor de Buurt"),
|
---|
96 | 'sdesc': rlinput(" - Short Description", "2buurt"),
|
---|
97 | 'channel' : rlprompt(" - Channel", "10"),
|
---|
98 | 'status' : 'up',
|
---|
99 | }
|
---|
100 |
|
---|
101 | newNode.if_nr = 0
|
---|
102 | def new_interlink(link_type='wireless', link_mode='station'):
|
---|
103 | ifname = rlinput("Interface name", "vr%i" % newNode.if_nr)
|
---|
104 | newNode.if_nr = newNode.if_nr + 1
|
---|
105 | netmask = 28
|
---|
106 | if link_mode == 'station':
|
---|
107 | remote = rlinput_choices("Create interlink with (remote host)", gformat.get_hostlist(),'Hybrid')
|
---|
108 | remote_datadump = gformat.get_yaml(remote)
|
---|
109 | print "Available remote interfaces:"
|
---|
110 | if_choices = []
|
---|
111 | for ifkey in remote_datadump['autogen_iface_keys']:
|
---|
112 | ifdump = remote_datadump[ifkey]
|
---|
113 | # Could only do this if all meta information is correct
|
---|
114 | #if link_type == 'wireless' and link_mode == 'station':
|
---|
115 | # basedump = remote_datadump['iface_' + ifdump['autogen_ifbase']]
|
---|
116 | # if not basedump.has_key('mode') or basedump['mode'] != 'ap-wds':
|
---|
117 | # continue
|
---|
118 | if_choices.append(ifdump['autogen_ifbase'])
|
---|
119 | print " - %4s" % ifdump['autogen_ifbase'], '- %20s' % ifdump['ip'], '- %30s' % ifdump['comment'],
|
---|
120 | if ifdump.has_key('ssid'):
|
---|
121 | print '-', ifdump['ssid']
|
---|
122 | else:
|
---|
123 | print ''
|
---|
124 | iface = rlinput_choices("Remote interface", if_choices)
|
---|
125 | ifdump = remote_datadump['iface_%s' % iface]
|
---|
126 |
|
---|
127 | #make_alias = rlprompt("Create Alias", "no") == "yes"
|
---|
128 | link_network = gformat.network(ifdump['ip'])
|
---|
129 | elif link_mode == 'master':
|
---|
130 | remote = rlinput("Interlink is planned for (remote host)", 'Unused%s' % newNode.if_nr)
|
---|
131 | print "Generating the Interlink IP range..."
|
---|
132 | link_network = getrange.get_ranges(True, netmask, 1)[0]
|
---|
133 |
|
---|
134 | print "Using Network %s/%s" % (gformat.showaddr(link_network), netmask)
|
---|
135 |
|
---|
136 | # XXX: Create remote linking
|
---|
137 | # XXX: This does not handle IP pools
|
---|
138 |
|
---|
139 | # Common part
|
---|
140 | datadump['iface_%s' % ifname] = {
|
---|
141 | 'comment': rlinput(" - Comment", "Link naar " + remote),
|
---|
142 | 'sdesc': rlinput(" - Short Description", "2" + remote),
|
---|
143 | 'type': "eth",
|
---|
144 | 'status': 'up',
|
---|
145 | 'dhcp': 'False',
|
---|
146 | }
|
---|
147 | if link_type == 'wireless':
|
---|
148 | compass = rlprompt("Compass direction", 'n')
|
---|
149 | datadump['iface_%s' % ifname].update({
|
---|
150 | 'ns_mac': rlinput(" - NS MAC", "00:27:22:"),
|
---|
151 | 'bridge_type': rlinput(' - Bridge Type', 'NanoStation5M'),
|
---|
152 | 'compass': compass,
|
---|
153 | })
|
---|
154 | if link_mode == 'master':
|
---|
155 | datadump['iface_%s' % ifname].update({
|
---|
156 | 'ssid': rlinput(" - SSID", 'il-%s.%s.wleiden.net' % (compass, datadump['nodename'])),
|
---|
157 | 'ip': rlinput(" - IP", getrange.showaddr(link_network + 1) + "/" + str(netmask)),
|
---|
158 | 'ns_ip': rlinput(" - NS IP", getrange.showaddr(link_network + 2) + "/" + str(netmask)),
|
---|
159 | 'mode': "ap-wds",
|
---|
160 | })
|
---|
161 | elif link_mode == 'station':
|
---|
162 | datadump['iface_%s' % ifname].update({
|
---|
163 | 'ssid': rlinput(" - SSID", ifdump['ssid']),
|
---|
164 | 'ip': rlinput(" - IP", getrange.showaddr(link_network + 3) + "/" + str(netmask)),
|
---|
165 | 'ns_ip': rlinput(" - NS IP", getrange.showaddr(link_network + 4) + "/" + str(netmask)),
|
---|
166 | 'mode': "station-wds",
|
---|
167 | })
|
---|
168 | elif link_type == 'wired':
|
---|
169 | if link_mode == 'master':
|
---|
170 | datadump['iface_%s' % ifname].update({
|
---|
171 | 'ip': rlinput(" - IP", getrange.showaddr(link_network + 1) + "/" + str(netmask)),
|
---|
172 | })
|
---|
173 | elif link_mode == 'station':
|
---|
174 | datadump['iface_%s' % ifname].update({
|
---|
175 | 'ip': rlinput(" - IP", getrange.showaddr(link_network + 2) + "/" + str(netmask)),
|
---|
176 | })
|
---|
177 |
|
---|
178 | def printMenu():
|
---|
179 | print '''\
|
---|
180 | ==========================================
|
---|
181 | Create Interfaces
|
---|
182 |
|
---|
183 | 1) New Accesspoint (ath0)
|
---|
184 | 2) New Wired Interlink (station)
|
---|
185 | 3) New Wired Interlink (master)
|
---|
186 | 4) New Wireless Interlink (station)
|
---|
187 | 5) New Wireless Interlink (master)
|
---|
188 | 9) Finish and Save
|
---|
189 | ==========================================
|
---|
190 | '''
|
---|
191 |
|
---|
192 | choice = 5
|
---|
193 | while choice != 9:
|
---|
194 | printMenu()
|
---|
195 | choice = int(rlprompt("Choice", choice))
|
---|
196 | if choice == 1:
|
---|
197 | new_access_point()
|
---|
198 | elif choice == 2:
|
---|
199 | new_interlink('wired', 'station')
|
---|
200 | elif choice == 3:
|
---|
201 | new_interlink('wired', 'master')
|
---|
202 | elif choice == 4:
|
---|
203 | new_interlink('wireless', 'station')
|
---|
204 | elif choice == 5:
|
---|
205 | new_interlink('wireless', 'master')
|
---|
206 | else:
|
---|
207 | print "Invalid choice!"
|
---|
208 |
|
---|
209 | # Output final result
|
---|
210 | print datadump
|
---|
211 |
|
---|
212 | #os.mkdir(datadump['autogen_item'])
|
---|
213 | #gformat.store_yaml(datadump)
|
---|
214 |
|
---|
215 |
|
---|
216 |
|
---|
217 | if __name__ == '__main__':
|
---|
218 | def printMenu():
|
---|
219 | print '''\
|
---|
220 | ==========================================
|
---|
221 | Node File Editor by Rick van der Zwet
|
---|
222 |
|
---|
223 | 1) New Node
|
---|
224 | 9) Quit
|
---|
225 | ==========================================
|
---|
226 | '''
|
---|
227 |
|
---|
228 | printMenu()
|
---|
229 | choice = 1
|
---|
230 | while choice != 9:
|
---|
231 | choice = int(rlprompt("Choice", choice))
|
---|
232 | if choice == 9:
|
---|
233 | break
|
---|
234 | elif choice == 1:
|
---|
235 | newNode()
|
---|
236 | printMenu()
|
---|
237 | sys.exit(1)
|
---|
238 | else:
|
---|
239 | print "Invalid choice!"
|
---|