I am working on developing a client to complete 8021.x wireless authentication by python.
According to the CoreWLAN Documentation scanForNetworks(withName:), I'm going to use scanForNetworksWithName_error_ and associateToEnterpriseNetwork_identity_username_password_error_ provided in CoreWLAN. And I wrote a script to have a try.
import os
import pwd
from CoreWLAN import CWWiFiClient
from Foundation import NSString
def get_real_user():
sudo_user = os.environ.get('SUDO_USER')
if sudo_user:
return sudo_user
return os.environ.get('USER', 'root')
def run_as_user(username):
if os.geteuid() == 0:
uid = pwd.getpwnam(username).pw_uid
gid = pwd.getpwnam(username).pw_gid
os.setuid(uid)
def connect_to_enterprise_network(ssid, username, password):
try:
real_user = get_real_user()
if os.geteuid() == 0:
run_as_user(real_user)
client = CWWiFiClient.sharedWiFiClient()
interface = client.interface()
if not interface:
print("no interface")
return False
print("scaning...")
error = None
scan_result, error = interface.scanForNetworksWithName_error_(ssid, None)
if error:
print(f"scan fialed: {error.localizedDescription()}")
return False
target_network = None
for network in scan_result.allObjects():
if network.ssid() == ssid:
target_network = network
break
if not target_network:
print("no target network")
return False
success, error = interface.associateToEnterpriseNetwork_identity_username_password_error_(
target_network,
None,
NSString.stringWithString_(username),
NSString.stringWithString_(password),
None
)
if not success:
print(f"connect failed: {error.localizedDescription() if error else 'unknown error'}")
return False
print("connect successfully")
return True
except Exception as e:
print(f"exception: {str(e)}")
return False
if __name__ == "__main__":
ssid = "ssid"
username = "username"
password = "password"
success = connect_to_enterprise_network(ssid, username, password)
However, I can only execute this script normally under non-root permissions. When I switch to root and execute it, the variable "scan_result.allObjects()" will be an object without any ssid and bssid. Finally the function prints "no target network" and returned.
<CWNetwork: 0x107104080> [ssid=(null), bssid=(null), security=WPA2 Enterprise, rssi=-52, channel=<CWChannel: 0x11e8a1fd0> [channelNumber=44(5GHz), channelWidth={20MHz}], ibss=0]
Compared with the value without sudo:
[<CWNetwork: 0x144650580> [ssid=ssid, bssid=<redacted>, security=WPA2 Enterprise, rssi=-55, channel=<CWChannel: 0x1247040d0> [channelNumber=149(5GHz), channelWidth={20MHz}], ibss=0]]
My python code will be included in an app that must be executed as a root user, so this issue can't be ignored and waiting for your help. THANKS!
Post
Replies
Boosts
Views
Activity
It was possible to excute:
sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -s
on previous mac os version to get all SSID list.
But now on mac os 14.4 it returns like:
WARNING: The airport command line tool is deprecated and will be removed in a future release. For diagnosing Wi-Fi related issues, use the Wireless Diagnostics app or wdutil command line tool.
Is there any other way to list all SSIDs? It's important to me since my code depends on this feature.
P.S. Seems other airport command like "airport -I" didn't work too. Is there any other tool could replace airport?