def detect_driver(): # MacOS response = cmd('which networksetup') if len(response.stdout) > 0 and 'not found' not in response.stdout: return 'networksetup' # Linux response = cmd('which nmcli') if len(response.stdout) > 0 and 'not found' not in response.stdout: return 'nmcli' raise Exception('Unable to find compatible wireless driver.')
def scan(self, ssid=None): response = cmd("nmcli -t -f active,ssid dev wifi list") ssids = [] for line in response.stdout.splitlines(): line_status, line_ssid = line.split(":") if ssid == None or ssid == line_ssid: ssids.append(line_ssid) return ssids
def get_ssid(self): response = cmd("nmcli -t -f active,ssid dev wifi") ssid = "" for line in response.stdout.splitlines(): line_status, line_ssid = line.split(":") if line_status == "yes": ssid = line_ssid return ssid
def get_channel(self): response = cmd("nmcli -t -f active,chan dev wifi") chan = "" for line in response.stdout.splitlines(): line_status, line_chan = line.split(":") if line_status == "yes": chan = line_chan return chan
def get_interface(self): response = cmd("nmcli -t -f device,type dev") interface = "" for line in response.stdout.splitlines(): line_interface, line_type = line.split(":") if line_type == "wifi": interface = line_interface return interface
def install_8021x_creds(self, ssid, address, signature, timestamp): mobileconfig_name = self.get_mobileconfig_name(ssid, address) response = cmd( 'nmcli con add type wifi ifname %s con-name %s ssid %s ipv4.method auto 802-1x.eap ttls 802-1x.phase2-auth pap 802-1x.identity %s 802-1x.password %s-%s 802-11-wireless-security.key-mgmt wpa-eap' % (self.interface(), mobileconfig_name, ssid, address, timestamp, signature)) if not response.returncode == 0: log("An error occured: %s" % response.stdout, "red") return False return True
def get_hardwareaddress(self): interface = self.get_interface() if len(interface) == 0: return None response = cmd("nmcli -t -f general.hwaddr dev show " + interface) hwaddr = "" for line in response.stdout.splitlines(): line_field, line_hwaddr = line.split(":") if line_field == "GENERAL.HWADDR": hwaddr = line_hwaddr return hwaddr
def has_8021x_creds(self, ssid, address, signature): mobileconfig_name = self.get_mobileconfig_name(ssid, address) command = '''profiles -Lv | grep "name: %s" -4 | awk -F": " "/attribute: profileIdentifier/{print $NF}" ''' % \ mobileconfig_name response = cmd(command) if not response.stdout: return False else: return True
def has_8021x_creds(self, ssid, address, signature): mobileconfig_name = self.get_mobileconfig_name(ssid, address) interface = self.interface() if len(interface) == 0: return None response = cmd("nmcli -t -f 802-1x.eap conn show " + ssid) has_config = False for line in response.stdout.splitlines(): line_field, line_value = line.split(":") if line_field == "802-1x.eap" and line_value == "ttls": has_config = True break return has_config
def install_8021x_creds(self, ssid, address, signature, timestamp): template_env = Environment(loader=FileSystemLoader(RESOURCES_PATH)) mobileconfig_name = self.get_mobileconfig_name(ssid, address) print("python radiusauth.py -s /tmp/magicsock %s %s-%s" % (address, timestamp, signature)) rendered_mobileconfig = template_env.get_template( 'magic.mobileconfig.template').render(address=address, signature=signature, ssid=ssid, name=mobileconfig_name, timestamp=timestamp) log( "As a first time user, Magic will install a network profile to make connecting to the network easier.", "blue") try: passwd_prompt = get_prompt([{ 'type': 'password', 'message': 'Enter your computer password', 'name': 'password' }]) passwd = passwd_prompt.get('password') + "\n" except Exception as error: raise Exception("An error occured: %s" % error) mobileconfig_filename = "%s.mobileconfig" % mobileconfig_name mobileconfig_file = open( "%s/%s" % (RESOURCES_PATH, mobileconfig_filename), "w") mobileconfig_file.write(rendered_mobileconfig) mobileconfig_file.close() response = cmd( "profiles install -path=%s/%s -user=%s" % (RESOURCES_PATH, mobileconfig_filename, getpass.getuser()), True, passwd.encode()) # Remove generated mobileconfig remove("%s/%s" % (RESOURCES_PATH, mobileconfig_filename)) if not response.returncode == 0: log("An error occured: %s" % response.stdout, "red") return False
def get_wifistatus(self): response = cmd("nmcli radio wifi") if "enabled" in response.stdout: return "Yes" return "No"
def associate(self, ssid): response = cmd("nmcli con up %s" % ssid) if not response.returncode == 0: log("An error occured: %s" % response.stdout, "red") return False return True
def install_profile(passwd): response = cmd("profiles install -path=magic/resources/magic.mobileconfig -user=%s" % getpass.getuser(), True, passwd.encode()) if not response.returncode == 0: log("An error occured: %s" % response.stdout, "red")
def get_profile(self): response = cmd( "profiles -Lv | grep \"name: %s\" -4 | awk -F\": \" '/attribute: profileIdentifier/{print $NF}'" % self.profile_name) return response.stdout