njahnke
njahnke

Reputation: 1387

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.

Upvotes: 2

Views: 1714

Answers (3)

Richard J. Ross III
Richard J. Ross III

Reputation: 55573

In C, you can do the following:

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

Upvotes: 1

Dr.Kameleon
Dr.Kameleon

Reputation: 22820

From the Terminal, try this :

networksetup -setairportnetwork $INTERFACE $SSID $PASSWORD

Or, if you want to integrate it in a Cocoa/Objective-C app :

check my code here for runCmd ( (Mac) Creating Xcode app that executes shell scripts ) on how to run an external command with NSTask and NSPipe.

In that case, you should call it like :

[self runCmd:@"/usr/sbin/networksetup" 
    withArgs:[[NSArray alloc] initWithObjects:@"-setairportnetwork",
                                              @"INTERFACE",
                                              @"SSID",
                                              @"PASS",
                                              nil]];

but I'd like to join the network without exec'ing external dependencies that may or may not be on the machine.

It will be there... :-)

Upvotes: 1

user23743
user23743

Reputation:

The networksetup tool will be on the machine, as it's part of the standard operating system. There's no need to reinvent the wheel.

Upvotes: 2

Related Questions