def connect_wpa(self, supplicant_conf_path, do_execute=True): ''' Connect to the network whose configuration is stored in supplicant_conf_path. This function return the list of commands that would have been executed (useful for testing) in the second position of a tuple. If do_execute is not True, it builds the list of commands without actually running them Types: * If configured in router as: WPA2-PSK[AES] is reported in scan as: IEEE 802.11i/WPA2 Version 1 * If configured in router as: WPA-PSK [TKIP] + WPA2-PSK [AES] is reported in scan as: IEEE 802.11i/WPA2 Version 1 WPA Version 1 * If configured in router as: WPA/WPA2 Enterprise, WPA [TKIP] is reported in scan as: WPA Version 1 * More options with Enterprise require radius management, this is a @TODO ''' logger = logging.getLogger(Conf.LOGGER_NAME) connect_commands = self.connect_common_pre() supplicant_command = cp_builder.build_supplicant_command(self.config, supplicant_conf_path) connect_commands.append((supplicant_command, None)) post = self.connect_common_post() connect_commands += post success = True if do_execute: return_values = cp_builder.execute(connect_commands, True) commands_output = reduce(lambda x,y:x or y, return_values) if commands_output == 1: success = False return success, connect_commands
def generate_supplicant_file(self, net_o, passw=None): """ This function reuses or try to generate new configuration file for wpa_supplicant Returns a tuple containing a boolean for success and the path for the supplicant file """ logger = logging.getLogger(Conf.LOGGER_NAME) valid_conf_created = False passphrase = '' supplicant_conf_path = '' if passw is None or not type(passw) == str: passphrase = util.ask_for_password(net_o) else: passphrase = passw if not passphrase is None: supplicant_conf_path = self.get_network_conf_file(net_o) wpapass_command = cp_builder.build_wpapass_command(self.config, net_o, passphrase) wpa_commands = [(wpapass_command, supplicant_conf_path)] wpa_return_values = cp_builder.execute(wpa_commands, True) if wpa_return_values[0] == 0: valid_conf_created = True else: print('Error creating configuration file for network {0}'.format(net_o.essid)) return valid_conf_created,supplicant_conf_path
def disconnect(self): ''' disconnect from all possible forms of connection and shut down wireless networking ''' logger = logging.getLogger(Conf.LOGGER_NAME) supplicant_command = cp_builder.build_supplicant_kill_command(self.config) ifconfig_command = cp_builder.build_ifconfig_command(self.config, Conf.IFCONFIG_ACTION_DOWN) rfkill_command = cp_builder.build_rfkill_command(self.config, Conf.RFKILL_VAL_BLOCK, Conf.RFKILL_VAL_WIFI) disconnect_commands = [(supplicant_command, None), (ifconfig_command, None), (rfkill_command, None)] disconnect_return_values = cp_builder.execute(disconnect_commands, False)
def start(self): """This function scans for wifi networks and outputs to a temporary file""" from conf import Conf import logging logger = logging.getLogger(Conf.LOGGER_NAME) import subprocess self.error = 0 import os, parser, cp_builder self.tmp_scan = cp_builder.build_tmp_scan_path(self.config) if Conf.DISABLE_SCAN: if Conf.DEBUG_MODE: logger.info('Scan disabled, we try to read networks from {}'.format(self.tmp_scan)) if not os.path.exists(self.tmp_scan): self.error = 1 logger.error('Modo debug pero el archivo de redes escaneadas {} no existe'. format(self.tmp_scan)) else: try: os.remove(self.config.conf_files[Conf.PATH_TMP_SCAN_FILE]) if Conf.DEBUG_MODE: logger.info("Previous scan deleted") except OSError: pass rfkill_command = cp_builder.build_rfkill_command(self.config, Conf.RFKILL_VAL_UNBLOCK, Conf.RFKILL_VAL_WIFI) ifconfig_command = cp_builder.build_ifconfig_command(self.config, Conf.IFCONFIG_ACTION_UP) scan_command = cp_builder.build_scan_command(self.config) scan_commands = [(rfkill_command, None), (ifconfig_command, None), (scan_command, self.tmp_scan)] scan_return_values = cp_builder.execute(scan_commands, True) if Conf.DEBUG_MODE: logger.info('Commands returned: ' + scan_return_values.__repr__())
def connect_key(self,key_conf_path, net_o, do_execute=True): ''' Connect to a network with non specified encryption. A network that reports encryption on but with no specified algorithms This function return the list of commands that would have been executed (useful for testing) in the second position of a tuple. If do_execute is not True, it builds the list of commands without actually running them ''' logger = logging.getLogger(Conf.LOGGER_NAME) success = True connect_commands = self.connect_common_pre() #get the key key = None try: with open(key_conf_path, 'rb') as f: key = pickle.load(f) if Conf.DEBUG_MODE: logger.info("Found key for network") except IOError: key = None if not key is None: iwconfig_command = cp_builder.build_iwconfig_command(self.config, net_o, key) connect_commands.append((iwconfig_command, None)) post = self.connect_common_post() connect_commands += post if do_execute: return_values = cp_builder.execute(connect_commands, True) commands_output = reduce(lambda x,y:x or y, return_values) if commands_output == 1: success = False else: success = False logger.error("Error trying to read wireless key from {}".format(key_conf_path)) return success, connect_commands
def connect_open(self, open_conf_path, net_o, do_execute=True): ''' Connects to ann open network (encryption off) whose configuration is stored in open_conf_path. This function return the list of commands that would have been executed (useful for testing) in the second position of a tuple. If do_execute is not True, it builds the list of commands without actually running them ''' logger = logging.getLogger(Conf.LOGGER_NAME) success = True connect_commands = self.connect_common_pre() iwconfig_command = cp_builder.build_iwconfig_command(self.config, net_o, None) connect_commands.append((iwconfig_command, None)) post = self.connect_common_post() connect_commands += post if do_execute: return_values = cp_builder.execute(connect_commands, True) commands_output = reduce(lambda x,y:x or y, return_values) if commands_output == 1: success = False return success, connect_commands