Search code examples
c++macoswifi

How do I join a wireless network programmatically under OS X?


C or C++ preferred. This is not an Objective-C project.

I already know the SSID. There may be WPA2 security on the network. I need to join from a specific interface (e.g. eth2) in case the machine has more than one wireless interface.

I realize that there are various commandline tools out there (e.g. the ones provided in answers to this question), but I'd like to join the network without exec'ing external dependencies that may or may not be on the machine. Thanks.


Solution

  • In C, you can do the following:

    execlp("networksetup", "networksetup", "-setairportnetwork", "$INTERFACE", "$SSID", "$PASSWORD", NULL);