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 | if DEBUG:
|
---|
23 | return prefill
|
---|
24 | return raw_input("%s%s: " % (prompt, ' [' + str(prefill) + ']' if prefill else '')) or prefill
|
---|
25 |
|
---|
26 | def rlinput(prompt, prefill=''):
|
---|
27 | ''' Get line with some editable boring default '''
|
---|
28 | if DEBUG:
|
---|
29 | return prefill
|
---|
30 | readline.set_startup_hook(lambda: readline.insert_text(prefill))
|
---|
31 | try:
|
---|
32 | return raw_input(prompt + ": ")
|
---|
33 | finally:
|
---|
34 | readline.set_startup_hook()
|
---|
35 |
|
---|
36 | def rlinput_float(prompt, prefill=0):
|
---|
37 | while True:
|
---|
38 | try:
|
---|
39 | prefill = rlinput(prompt, prefill)
|
---|
40 | return float(prefill)
|
---|
41 | except ValueError:
|
---|
42 | print "Invalid Number!"
|
---|
43 | pass
|
---|
44 |
|
---|
45 | def rlinput_choices(prompt, choices, prefill=''):
|
---|
46 | if DEBUG:
|
---|
47 | return prefill
|
---|
48 | while True:
|
---|
49 | filtered_choices = [x for x in choices if (str(prefill).lower() in str(x).lower())]
|
---|
50 | if len(filtered_choices) == 1:
|
---|
51 | return filtered_choices[0]
|
---|
52 | elif len(filtered_choices) == 0:
|
---|
53 | filtered_choices = choices
|
---|
54 | prefill = rlinput(prompt + ' ' + str(filtered_choices), prefill)
|
---|
55 | if prefill in prompt:
|
---|
56 | return prefill
|
---|
57 |
|
---|
58 |
|
---|
59 |
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 | def newNode():
|
---|
64 | datadump = {
|
---|
65 | 'nodename' : "New",
|
---|
66 | 'location' : "1234 AB, Leiden",
|
---|
67 | 'nodetype' : "Hybrid",
|
---|
68 | 'latitude' : "52.",
|
---|
69 | 'longitude' : "4.",
|
---|
70 | }
|
---|
71 | datadump['nodename'] = rlprompt("Nodename", datadump['nodename'])
|
---|
72 | datadump['location'] = rlprompt("Location", datadump['location'])
|
---|
73 | datadump['nodetype'] = rlprompt("Nodetype", datadump['nodetype'])
|
---|
74 | datadump['latitude'] = rlinput_float("Latitude", datadump['latitude'])
|
---|
75 | datadump['longitude'] = rlinput_float("Longitude", datadump['longitude'])
|
---|
76 | datadump['autogen_item'] = datadump['nodetype'] + datadump['nodename']
|
---|
77 |
|
---|
78 | print "Generating the Master IP range..."
|
---|
79 | master_network = getrange.get_ranges(False, 24, 1)[0]
|
---|
80 | master_ip = getrange.showaddr(master_network + 1)
|
---|
81 | datadump['master_ip'] = master_ip
|
---|
82 |
|
---|
83 | if rlprompt("Add ath0 Accesspoint", "yes") == "yes":
|
---|
84 | ifname = rlinput("Interface name", "ath0")
|
---|
85 | datadump['iface_' + ifname] = {
|
---|
86 | 'ip' : rlinput(" - IP", master_ip + "/26"),
|
---|
87 | 'dhcp' : rlinput(" - DHCP range", "10-60"),
|
---|
88 | 'ssid' : rlinput(" - SSID", "ap-WirelessLeiden-" + datadump['nodename']),
|
---|
89 | 'mode' : 'ap',
|
---|
90 | 'type' : '11b',
|
---|
91 | 'comment' : rlinput(" - Comment", "Omni voor de Buurt"),
|
---|
92 | 'sdesc': rlinput(" - Short Description", "2buurt"),
|
---|
93 | 'channel' : rlprompt(" - Channel", "10"),
|
---|
94 | 'status' : 'up',
|
---|
95 | }
|
---|
96 |
|
---|
97 | if_nr = 0
|
---|
98 | while rlprompt("Add Interlink", "yes") == "yes":
|
---|
99 | ifname = rlinput("Interface name", "vr%i" % if_nr)
|
---|
100 | if_nr = if_nr + 1
|
---|
101 | remote = rlinput_choices("Create interlink", gformat.get_hostlist(),'HybridCeTIM2')
|
---|
102 | remote_datadump = gformat.get_yaml(remote)
|
---|
103 | print "Available remote interfaces:"
|
---|
104 | for ifkey in remote_datadump['autogen_iface_keys']:
|
---|
105 | ifdump = remote_datadump[ifkey]
|
---|
106 | print " - %4s" % ifdump['autogen_ifbase'], '- %30s' % ifdump['comment'],
|
---|
107 | if ifdump.has_key('ssid'):
|
---|
108 | print '-', ifdump['ssid']
|
---|
109 | else:
|
---|
110 | print ''
|
---|
111 | iface = rlinput_choices("Interface Used", remote_datadump['autogen_iface_keys'], "vr1")
|
---|
112 | netmask = 28
|
---|
113 | print "Generating the Interlink IP range..."
|
---|
114 | link_network = getrange.get_ranges(True, netmask, 1)[0]
|
---|
115 |
|
---|
116 | datadump['iface_%s' % ifname] = {
|
---|
117 | 'comment': rlinput(" - Comment", "Link naar " + remote),
|
---|
118 | 'sdesc': rlinput(" - Short Description", "2" + remote),
|
---|
119 | 'ssid': rlinput(" - SSID", 'il-X.wleiden.net'),
|
---|
120 | 'dhcp': 'False',
|
---|
121 | 'mode': "ap-wds",
|
---|
122 | 'ip': rlinput(" - IP", getrange.showaddr(link_network + 1) + "/" + str(netmask)),
|
---|
123 | 'type': "eth",
|
---|
124 | 'ns_ip': rlinput(" - NS IP", getrange.showaddr(link_network + 2) + "/" + str(netmask)),
|
---|
125 | 'ns_mac': rlinput(" - NS MAC", "00:27:22:"),
|
---|
126 | 'bridge_type': rlinput(' - Bridge Type', 'NanoStation5M'),
|
---|
127 | 'status': 'up',
|
---|
128 | }
|
---|
129 | break
|
---|
130 |
|
---|
131 | print datadump
|
---|
132 | #os.mkdir(datadump['autogen_item'])
|
---|
133 | #gformat.store_yaml(datadump)
|
---|
134 |
|
---|
135 |
|
---|
136 |
|
---|
137 | if __name__ == '__main__':
|
---|
138 | def printMenu():
|
---|
139 | print '''Node File Editor by Rick van der Zwet
|
---|
140 |
|
---|
141 | 1) New Node
|
---|
142 | 9) Quit
|
---|
143 | '''
|
---|
144 |
|
---|
145 | printMenu()
|
---|
146 | choice = 1
|
---|
147 | while choice != 9:
|
---|
148 | choice = int(rlprompt("Choice", choice))
|
---|
149 | if choice == 9:
|
---|
150 | break
|
---|
151 | elif choice == 1:
|
---|
152 | newNode()
|
---|
153 | printMenu()
|
---|
154 | sys.exit(1)
|
---|
155 | else:
|
---|
156 | print "Invalid choice!"
|
---|