#!/usr/bin/env python ''' CLI interface which configures and makes new nodes with the most commonly used commands. Rick van der Zwet ''' import getrange import gformat import os import readline import sys print "WARNING I AM NOT PRODUCTION CODE YET!" + __doc__ DEBUG = "-g" in sys.argv def rlprompt(prompt, prefill=''): ''' Get line with some boring default ''' if DEBUG: return prefill return raw_input("%s%s: " % (prompt, ' [' + str(prefill) + ']' if prefill else '')) or prefill def rlinput(prompt, prefill=''): ''' Get line with some editable boring default ''' if DEBUG: return prefill readline.set_startup_hook(lambda: readline.insert_text(prefill)) try: return raw_input(prompt + ": ") finally: readline.set_startup_hook() def rlinput_float(prompt, prefill=0): while True: try: prefill = rlinput(prompt, prefill) return float(prefill) except ValueError: print "Invalid Number!" pass def rlinput_choices(prompt, choices, prefill=''): if DEBUG: return prefill while True: filtered_choices = [x for x in choices if (str(prefill).lower() in str(x).lower())] if len(filtered_choices) == 1: return filtered_choices[0] elif len(filtered_choices) == 0: filtered_choices = choices prefill = rlinput(prompt + ' ' + str(filtered_choices), prefill) if prefill in prompt: return prefill rlinput_choices("Test", gformat.get_hostlist()) def newNode(): datadump = { 'nodename' : "New", 'location' : "1234 AB, Leiden", 'nodetype' : "Hybrid", 'latitude' : "52.", 'longitude' : "4.", } datadump['nodename'] = rlprompt("Nodename", datadump['nodename']) datadump['location'] = rlprompt("Location", datadump['location']) datadump['nodetype'] = rlprompt("Nodetype", datadump['nodetype']) datadump['latitude'] = rlinput_float("Latitude", datadump['latitude']) datadump['longitude'] = rlinput_float("Longitude", datadump['longitude']) datadump['autogen_item'] = datadump['nodetype'] + datadump['nodename'] print "Generating the Master IP range..." master_network = getrange.get_ranges(False, 24, 1)[0] master_ip = getrange.showaddr(master_network + 1) datadump['master_ip'] = master_ip if rlprompt("Add ath0 Accesspoint", "yes") == "yes": ifname = rlinput("Interface name", "ath0") datadump['iface_' + ifname] = { 'ip' : rlinput(" - IP", master_ip + "/26"), 'dhcp' : rlinput(" - DHCP range", "10-60"), 'ssid' : rlinput(" - SSID", "ap-WirelessLeiden-" + datadump['nodename']), 'mode' : 'ap', 'type' : '11b', 'comment' : rlinput(" - Comment", "Omni voor de Buurt"), 'sdesc': rlinput(" - Short Description", "2buurt"), 'channel' : rlprompt(" - Channel", "10"), 'status' : 'up', } if_nr = 0 while rlprompt("Add Interlink", "yes") == "yes": ifname = rlinput("Interface name", "vr%i" % if_nr) if_nr = if_nr + 1 remote = rlinput("Remote Node", "Unused") netmask = 28 print "Generating the Interlink IP range..." link_network = getrange.get_ranges(True, netmask, 1)[0] datadump['iface_%s' % ifname] = { 'comment': rlinput(" - Comment", "Link naar " + remote), 'sdesc': rlinput(" - Short Description", "2" + remote), 'ssid': rlinput(" - SSID", 'il-X.wleiden.net'), 'dhcp': 'False', 'mode': "ap-wds", 'ip': rlinput(" - IP", getrange.showaddr(link_network + 1) + "/" + str(netmask)), 'type': "eth", 'ns_ip': rlinput(" - NS IP", getrange.showaddr(link_network + 2) + "/" + str(netmask)), 'ns_mac': rlinput(" - NS MAC", "00:27:22:"), 'bridge_type': rlinput(' - Bridge Type', 'NanoStation5M'), 'status': 'up', } break print datadump #os.mkdir(datadump['autogen_item']) #gformat.store_yaml(datadump) if __name__ == '__main__': def printMenu(): print '''Node File Editor by Rick van der Zwet 1) New Node 9) Quit ''' printMenu() choice = 1 while choice != 9: choice = int(rlprompt("Choice", choice)) if choice == 9: break elif choice == 1: newNode() printMenu() sys.exit(1) else: print "Invalid choice!"