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 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 start_hostapd(): """ Configs the IN interface, starts dhcpd, configs iptables, Starts Hostapd """ generate_confs() global_config = config_gen.get_config() IN = global_config['in'] OUT = global_config['out'] IP = global_config['ip_wlan'] NETMASK = global_config['netmask'] try: with open(config.file_hostapd) as f: pass except IOError as e: exit_error('[ERROR] ' + config.file_hostapd + ' doesn\'t exist') # Configure network interface print 'configuring',IN,'...' subprocess.call(['ifconfig', IN, 'up', IP, 'netmask', NETMASK]) sleep(1) dhcp_log = open('./dhcp.log', 'w') # Start dhcpd print 'Starting dhcpd...' dhcp_proc = subprocess.Popen(['dhcpd',IN, '-cf', config.file_dhcpd],stdout = dhcp_log, stderr = dhcp_log) sleep(1) dhcp_log.close(); # Configure iptables print 'Configuring iptables...' subprocess.call(['iptables','--flush']) subprocess.call(['iptables','--table','nat','--flush']) subprocess.call(['iptables','--delete-chain']) subprocess.call(['iptables','--table','nat','--delete-chain']) subprocess.call(['iptables','--table','nat','--append','POSTROUTING','--out-interface',OUT,'-j','MASQUERADE']) subprocess.call(['iptables','--append','FORWARD','--in-interface',IN,'-j','ACCEPT']) subprocess.call(['sysctl','-w','net.ipv4.ip_forward=1']) # Start hostapd print 'Starting Hostapd...' hostapd_proc = subprocess.Popen(['hostapd -t -d '+config.file_hostapd+' >./hostapd.log'],shell=True) print 'Done... (Hopefully!)' print
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)