Skip to content

Instantly share code, notes, and snippets.

@chilcote
Created April 6, 2014 19:14
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save chilcote/10010307 to your computer and use it in GitHub Desktop.
Save chilcote/10010307 to your computer and use it in GitHub Desktop.
Fun with CoreWLAN and python
#!/usr/bin/env python
'''
Playing around with CoreWLAN to return information about the wi-fi connection
Documentation:
https://developer.apple.com/library/mac/documentation/CoreWLAN/Reference/CWInterface_reference/translated_content/CWInterface.html
'''
import objc
objc.loadBundle('CoreWLAN',
bundle_path='/System/Library/Frameworks/CoreWLAN.framework',
module_globals=globals())
class WiFi(object):
''' WiFI object'''
def __init__(self):
self.wifi = CWInterface.interfaceNames()
for iname in self.wifi:
self.interface = CWInterface.interfaceWithName_(iname)
def get_wifistatus(self):
if self.interface.powerOn() == 1:
return "Yes"
return "No"
def get_ssid(self):
return self.interface.ssid()
def get_interface(self):
return self.interface.interfaceName()
def get_hardwareaddress(self):
return self.interface.hardwareAddress()
def get_aggregatenoise(self):
return self.interface.aggregateNoise()
def get_aggregaterssi(self):
return self.interface.aggregateRSSI()
def get_bssid(self):
return self.interface.bssid()
def get_channel(self):
return self.interface.channel()
def get_transmitrate(self):
return self.interface.transmitRate()
def get_mcsindex(self):
return self.interface.mcsIndex()
def main():
wifi = WiFi()
print 'Interface: %s' % wifi.get_interface()
print 'Hardware Address: %s' % wifi.get_hardwareaddress()
print 'Active: %s' % wifi.get_wifistatus()
print 'SSID: %s' % wifi.get_ssid()
print 'Aggregate Noise: %s' % wifi.get_aggregatenoise()
print 'BSSID: %s' % wifi.get_bssid()
print 'RSSI: %s' % wifi.get_aggregaterssi()
print 'Channel: %s' % wifi.get_channel()
print 'Transmit Rate: %s' % wifi.get_transmitrate()
print 'MCS Index: %s' % wifi.get_mcsindex()
if __name__ == "__main__":
main()
@tejaskumark
Copy link

tejaskumark commented May 29, 2022

@chilcote I have been playing with above code and it helped me start working on WLAN Core API in python. I need your help on same, my goal is to create SSID profile and push it to Mac OS. So that whenever SSID is up, device will connect to that SSID.

  • If ssid is broadcasting, then I am able to connect to it with the help of this API
  • Now I want to create profile and push it to Mac OS, I have been able to create SSID Profile like below, but this SSID is 802.1X so I need to configure username and password as well, but some how API mentioned here I can not import into python. Possible I am overlooking something.
  • Can you please help me here, what will be the best way to configure SSID profile ? Any other possible solution is also welcome.
    Thanks.
profile.setSsidData_(ssiddata)
profile.setSecurity_(9)
pf_list = [profile]
mc = CoreWLAN.CWMutableConfiguration.alloc().init()
mc.setNetworkProfiles_(NSOrderedSet.orderedSetWithArray_(pf_list))
mc.setRequireAdministratorForAssociation_(False)


interface = CoreWLAN.CWInterface.interfaceWithName_("en0")
result, error = interface.commitConfiguration_authorization_error_(
    mc, None, None)```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment