2

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.

Community
  • 1
  • 1
njahnke
  • 1,369
  • 2
  • 10
  • 33

3 Answers3

2

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.

1

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... :-)

Community
  • 1
  • 1
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
1

In C, you can do the following:

execlp("networksetup", "networksetup", "-setairportnetwork", "$INTERFACE", "$SSID", "$PASSWORD", NULL);
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • Just a note that I had to add a NULL to the end of the argument list to avoid receiving a `Missing sentinel in function call` warning. – njahnke Apr 26 '12 at 17:03