def config_cli(): if len(sys.argv)==3: if sys.argv[2] == 'list': keys = global_config.keys() for x in keys: print x, '=', global_config[x] elif global_config.has_key(sys.argv[2]): print sys.argv[2], '=', global_config[sys.argv[2]] else: print 'Invalid key :', sys.argv[2] elif len(sys.argv) == 4: if global_config.has_key(sys.argv[2]): global default_config sections = default_config.keys() for section in sections: length = len(default_config[section]) for idx in range(length): key, val = default_config[section][idx] if key == sys.argv[2]: val = sys.argv[3] else: val = global_config[key] default_config[section][idx] = (key, val) print 'Changing \''+sys.argv[2]+'\' to \''+sys.argv[3] + '\'' gen_default_cfg() else: print 'Invalid key :', sys.argv[2] else: common_methods.exit_error('[ERROR] config: incorrect usage', 1)
def write_hostapd_conf(global_config): config_output = global_config['HOSTAPD']['OUTPUT_CONFIG'] print 'Writing', config_output, '...' try: with open(config_output, 'w') as f: for key, val in global_config['HOSTAPD'].items(): if key not in config.special_options: f.write( key + '=' + val + '\n' ) except: exit_error('[ERROR] Failed to open ' + config_output + ' in write mode')
def write_hostapd_conf(global_config): config_output = global_config['HOSTAPD']['OUTPUT_CONFIG'] print('Writing', config_output, '...') try: with open(config_output, 'w') as f: for key, val in global_config['HOSTAPD'].items(): if key not in config.special_options: f.write(key + '=' + val + '\n') except: exit_error('[ERROR] Failed to open ' + config_output + ' in write mode')
def write_dhcpd_conf(): print 'Writing', config.file_dhcpd, '...' global_config = config_gen.get_config() content = config.dhcpd_template[:] for key in config.dhcpd_defaults.keys(): key2 = '$' + key + '$' content = content.replace(key2, global_config[key]) try: with open(config.file_dhcpd, 'w') as f: f.write( content ) except: exit_error('[ERROR] Failed to open ' + config.file_dhcpd)
def write_hostapd_conf(): """ Writes the config data to', config.file_hostapd """ print 'Writing', config.file_hostapd, '...' global_config = config_gen.get_config() try: with open(config.file_hostapd, 'w') as f: for attr in config.hostapd_default: f.write( attr + '=' + global_config[attr] + '\n' ) except: exit_error('[ERROR] Failed to open' + config.file_hostapd)
def stop_hostapd(): conf = config_gen.get_config() env_tups = [(section + "_" + key, val) for section in conf.keys() for key, val in conf[section].items()] env_dict = dict(os.environ.items() + env_tups) print "Stopping..." for section in config.script_order[::-1]: if conf[section].has_key("EXIT_SCRIPT"): make_dirs(conf[section]) print "Executing %s for [%s]..." % (conf[section]["EXIT_SCRIPT"], section), ret = subprocess.call(conf[section]["EXIT_SCRIPT"], env=env_dict) if ret == 0: print "Done!" else: print "Failed!" exit_error("[ERROR] Failed to exit [%s], check log file %s" % (section, conf[section]["LOGFILE"]))
def stop_hostapd(): conf = config_gen.get_config() env_tups = [(section+'_'+key, val) for section in conf.keys() for key, val in conf[section].items()] env_dict = dict(os.environ.items() + env_tups) print 'Stopping...' for section in config.script_order[::-1]: if conf[section].has_key('EXIT_SCRIPT'): make_dirs(conf[section]) print 'Executing %s for [%s]...' % (conf[section]['EXIT_SCRIPT'], section), ret = subprocess.call(conf[section]['EXIT_SCRIPT'], env=env_dict) if ret == 0: print 'Done!' else: print 'Failed!' exit_error('[ERROR] Failed to exit [%s], check log file %s' % (section, conf[section]['LOGFILE']))
def write_cfg(content, section): configparser = ConfigParser.ConfigParser() try: with open(config.file_cfg) as f: configparser.read(f.name) except IOError as e: print bcolors.WARNING ,'[WARNING]', config.file_cfg, 'does not exist, new will be created', bcolors.ENDC try: configparser.add_section(section) except ConfigParser.DuplicateSectionError: pass for line in content: configparser.set(section, line[0],line[1]) print 'Writing Section', section, 'to',config.file_cfg,'...' try: with open(config.file_cfg,'wb') as f: configparser.write(f) except: common_methods.exit_error('[ERROR] Writing Failed!') print bcolors.OKGREEN, 'Done!', bcolors.ENDC
def write_cfg(content, section): cfgParser = configparser.ConfigParser() cfgParser.optionxform = str try: with open(config.file_cfg) as f: cfgParser.read(f.name) except IOError as e: print( bcolors.WARNING ,'[WARNING]', config.file_cfg, 'does not exist, new will be created', bcolors.ENDC) try: cfgParser.add_section(section) except cfgParser.DuplicateSectionError: pass for key, val in content.items(): cfgParser.set(section, key, val) print( 'Writing Section', section, 'to',config.file_cfg,'...' ) try: with open(config.file_cfg,'wb') as f: cfgParser.write(f) except: common_methods.exit_error('[ERROR] Writing Failed!') print( bcolors.OKGREEN, 'Done!', bcolors.ENDC)
def generate_confs(): """ For each section generate config files if TEMPLATE_CONFIG is present into OUTPUT_CONFIG Exception for HOSTAPD as it may have many variables which is not intended to be specified through TEMPLATE_CONFIG """ global_config = config_gen.get_config() for section in global_config.keys(): if 'TEMPLATE_CONFIG' in global_config[section]: if not 'OUTPUT_CONFIG' in global_config[section]: exit_error("[ERROR] 'OUTPUT_CONFIG' not specified for '" + section + "'") template_file = global_config[section]['TEMPLATE_CONFIG'] template_str = '' try: with open(template_file) as f: template_str = f.read() except: exit_error("[ERROR] Template File for '" + section + "', " + template_file + " does not exist") for key, val in global_config[section].items(): template_str = template_str.replace('$' + key + '$', val) try: with open(global_config[section]['OUTPUT_CONFIG'], 'wb') as f: print('Writing', f.name, '...') f.write(template_str) except: exit_error("[ERROR] Failed to open output_config '" + global_config[section]['OUTPUT_CONFIG'] + "' in write mode") elif section == 'HOSTAPD': write_hostapd_conf(global_config)
def generate_confs(): """ For each section generate config files if TEMPLATE_CONFIG is present into OUTPUT_CONFIG Exception for HOSTAPD as it may have many variables which is not intended to be specified through TEMPLATE_CONFIG """ global_config = config_gen.get_config() for section in global_config.keys(): if global_config[section].has_key('TEMPLATE_CONFIG'): if not global_config[section].has_key('OUTPUT_CONFIG'): exit_error("[ERROR] 'OUTPUT_CONFIG' not specified for '" + section + "'") template_file = global_config[section]['TEMPLATE_CONFIG'] template_str = '' try: with open(template_file) as f: template_str = f.read() except: exit_error("[ERROR] Template File for '" + section + "', " + template_file + " does not exist") for key, val in global_config[section].items(): template_str = template_str.replace('$' + key + '$', val) try: with open(global_config[section]['OUTPUT_CONFIG'], 'wb') as f: print 'Writing', f.name, '...' f.write(template_str) except: exit_error("[ERROR] Failed to open output_config '" + global_config[section]['OUTPUT_CONFIG'] + "' in write mode") elif section == 'HOSTAPD': write_hostapd_conf(global_config)
def start_hostapd(): generate_confs() conf = config_gen.get_config() env_tups = [(section + '_' + key, val) for section in conf.keys() for key, val in conf[section].items()] env_dict = dict(os.environ.items() + env_tups) print('Starting...') for section in config.script_order: if conf[section].has_key('SCRIPT'): make_dirs(conf[section]) print( 'Executing %s for [%s]...' % (conf[section]['SCRIPT'], section), ) ret = subprocess.call(conf[section]['SCRIPT'], env=env_dict) if ret == 0: print('Done!') else: print('Failed!') exit_error( '[ERROR] Failed to initiate [%s], check log file %s' % (section, conf[section]['LOGFILE'])) sleep(1)
def config_cli(): """ Handles Config via CLI commands """ # ./hostapd.py config if len(sys.argv) == 2: for section in global_config.keys(): print '[%s]' % section for key, val in global_config[section].items(): print '%s = %s' %(key,val) print # ./hostapd.py config <section> elif len(sys.argv) == 3: section = sys.argv[2] if global_config.has_key(section): print '[%s]' % section for key, val in global_config[section].items(): print '%s = %s' % (key, val) else: exit_error('[%s] Does not exist' % section) # ./hostapd.py config <section> <key> elif len(sys.argv) == 4: section = sys.argv[2] key = sys.argv[3] if global_config.has_key(section): if global_config[section].has_key(key): print global_config[section][key] else: exit_error('No key %s in [%s]' % (key, section)) else: exit_error('[%s] Does not exist' % section) # ./hostapd.py config <section> <key> <val> elif len(sys.argv) == 5: section = sys.argv[2] key = sys.argv[3] val = sys.argv[4] import copy conf = copy.deepcopy(global_config) conf[section][key] = val gen_cfg(conf)
def config_cli(): """ Handles Config via CLI commands """ # ./hostapd.py config if len(sys.argv) == 2: for section in global_config.keys(): print( '[%s]' % section) for key, val in global_config[section].items(): print( '%s = %s' %(key,val)) # ./hostapd.py config <section> elif len(sys.argv) == 3: section = sys.argv[2] if global_config.has_key(section): print( '[%s]' % section) for key, val in global_config[section].items(): print( '%s = %s' % (key, val)) else: exit_error('[%s] Does not exist' % section) # ./hostapd.py config <section> <key> elif len(sys.argv) == 4: section = sys.argv[2] key = sys.argv[3] if global_config.has_key(section): if global_config[section].has_key(key): print( global_config[section][key]) else: exit_error('No key %s in [%s]' % (key, section)) else: exit_error('[%s] Does not exist' % section) # ./hostapd.py config <section> <key> <val> elif len(sys.argv) == 5: section = sys.argv[2] key = sys.argv[3] val = sys.argv[4] import copy conf = copy.deepcopy(global_config) conf[section][key] = val gen_cfg(conf)