Example #1
0
def _manipulate_wifi(name, func, just_ssids=False, just_profiles=False):
    '''
    The core of this code comes from this gist by Pudquick. Thank you!
    https://gist.github.com/pudquick/fcbdd3924ee230592ab4
    '''
    CoreWLAN = _load_objc_framework('CoreWLAN')
    interfaces = _get_available_wifi_interfaces(CoreWLAN)

    if not interfaces:
        return (False, 'No WiFi devices found to manage.')

    for interface in interfaces.keys():
        # Grab a mutable copy of this interface's configuration
        configuration_copy = CoreWLAN.CWMutableConfiguration.alloc(
        ).initWithConfiguration_(interfaces[interface].configuration())
        # Find all the preferred/remembered network profiles
        pro_ssid = _get_profiles_and_ssids(configuration_copy)
        profiles = pro_ssid[0]
        SSIDs = pro_ssid[1]

        # return just the profiles.
        if just_profiles:
            return profiles

        # check to see if we want just the list of SSID's
        if just_ssids:
            return SSIDs

        # see if we actually have any SSID's.
        if not SSIDs:
            # we can bounce out since there isn't anything to do with any empty
            # list of SSIDs
            return True

        # takes the function to run here as a parameter, so we can remove, sort,
        # top or bottom.
        profiles = func(name, profiles=profiles)
        # Now we have to update the mutable configuration
        # First convert it back to a NSOrderedSet
        log.trace(
            'wifi._manipulate_wifi: Attempting to set changed WiFi profiles.')
        profile_set = NSOrderedSet.orderedSetWithArray_(profiles)
        # Then set/overwrite the configuration copy's networkProfiles
        configuration_copy.setNetworkProfiles_(profile_set)
        # Then update the network interface configuration
        result = interfaces[
            interface].commitConfiguration_authorization_error_(
                configuration_copy, None, None)
    try:
        if result[0] == 1:
            return True
    except Exception as e:
        ret = 'wifi._manipulate_wifi'
        return (False, 'Caught Exception: {} in parsing return from {}'.format(
            e, ret))
    return (False, result[1])
Example #2
0
def _get_profiles_and_ssids(configuration):
    # Find all the preferred/remembered network profiles
    try:
        profiles = list(NSOrderedSet.array(configuration.networkProfiles()))
    except TypeError:
        # if we got here we probably don't have any SSIDs in the wifi list
        # we'll return a tuple of empty lists. Theres probably a better way to
        # handle this.
        log.trace('module.mac_wifi - Could not find an SSIDs.')
        return ([], [])
    # Grab all the SSIDs, in order
    SSIDs = [x.ssid() for x in profiles]
    return (profiles, SSIDs)
Example #3
0
CoreWLAN = load_objc_framework('CoreWLAN')

# Load all available wifi interfaces
interfaces = dict()
for i in CoreWLAN.CWInterface.interfaceNames():
    interfaces[i] = CoreWLAN.CWInterface.interfaceWithName_(i)

# Repeat the configuration with every wifi interface
for i in interfaces.keys():
    # Grab a mutable copy of this interface's configuration
    configuration_copy = CoreWLAN.CWMutableConfiguration.alloc().initWithConfiguration_(interfaces[i].configuration())
    # Find all the preferred/remembered network profiles
    profiles = list(configuration_copy.networkProfiles())
    # Grab all the SSIDs, in order
    SSIDs = [x.ssid() for x in profiles]
    # Check to see if our preferred SSID is in the list
    if (preferred_SSID in SSIDs):
        # Apparently it is, so let's adjust the order
        # Profiles with matching SSIDs will move to the front, the rest will remain at the end
        # Order is preserved, example where 'ssid3' is preferred:
        #    Original: [ssid1, ssid2, ssid3, ssid4]
        #   New order: [ssid3, ssid1, ssid2, ssid4]
        profiles.sort(key=lambda x: x.ssid() == preferred_SSID, reverse=True)
        # Now we have to update the mutable configuration
        # First convert it back to a NSOrderedSet
        profile_set = NSOrderedSet.orderedSetWithArray_(profiles)
        # Then set/overwrite the configuration copy's networkProfiles
        configuration_copy.setNetworkProfiles_(profile_set)
        # Then update the network interface configuration
        result = interfaces[i].commitConfiguration_authorization_error_(configuration_copy, None, None)