How would I go about creating a wireless adhoc network with a specified SSID and password on OS X? I tried looking at the networksetup
man page but didn't come up with anything to accomplish this. Is there another command I should be using?

- 1,092
- 2
- 19
- 35
-
Just an FYI if your Wi-Fi is using WPA or WPA2, computer-to-computer networks are not compatible with WPA or WPA2 protected networks. – user3439894 Jul 10 '15 at 14:48
3 Answers
In OSX 10.13, I had to modify @dan-ramos 's code to:
import Foundation
import CoreWLAN
let networkName = "foo"
let password = "bar"
if let iface = CWWiFiClient.shared().interface() {
do {
try iface.startIBSSMode(
withSSID: networkName.data(using: String.Encoding.utf8)!,
security: CWIBSSModeSecurity.WEP104,
channel: 11,
password: password as String
)
print("Success")
} catch let error as NSError {
print("Error", error)
exit(1)
}
} else {
print("Invalid interface")
exit(1)
}

- 1,070
- 13
- 19

- 10,354
- 2
- 38
- 48
-
1I packaged this up for others to use: https://github.com/hildjj/adhocnet – Joe Hildebrand Sep 14 '16 at 18:38
I didn't find any real way to do it other than to write a Swift script:
import Foundation
import CoreWLAN
var networkName = "foo";
var password = "bar";
var error: NSError?
let iface = CWWiFiClient.sharedWiFiClient().interface()
let success = iface.startIBSSModeWithSSID(
networkName.dataUsingEncoding(NSUTF8StringEncoding),
security: CWIBSSModeSecurity.WEP104,
channel: 11,
password: password as String,
error: &error
)
if !success {
println(error?.localizedDescription)
} else {
NSRunLoop.currentRunLoop().run()
}

- 1,092
- 2
- 19
- 35
If you are not set on using the command line, you can follow the instructions here which sets up the ad-hoc network just by using system preferences > sharing > Internet Sharing. Inside of this tab you can set up your ad-hoc network and specify an SSID, etc. Granted, this is a very rudimentary way to set it up, but it is quite user friendly, but it does not give you the way to set it up using terminal. With the networksetup
command in the terminal, have you used networksetup -printcommands
or networksetup -help
? This gives additional information since there is not a man page.
I am not familiar with, but additionally have found references to using the startHostAPModeWithSSID
command here or using the /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -i
command per this server fault entry
-
Unfortunately this does need to be done progamatically. I have looked at networksetup commands and can't quite find anything that will work. After doing hours of research the only option I can come up with is doing this using the `CoreWLAN` framework and writing an Objective-C/Swift script. Even after attempting to do this I'm getting errors that almost lead me to believe there is a bug in the framework... I'll keep banging my head against the wall in the mean time – Dan Ramos Jul 28 '15 at 23:42
-
Have you tried using Automator to record a workflow and replay it and/or save it as an Apple Script? I am working on setting up an ad-hoc network with two OS X systems now and it seems possible I will update my answer with results shortly. – bern Jul 29 '15 at 03:27
-
i'm not going to be able to test the ad-hoc until morning, but i did find this on [server fault](http://serverfault.com/questions/49732/setup-ad-hoc-wifi-from-terminal-in-os-x) which seems to be a related question, it does not fully answer your question, but it uses the airport command in conjunction with networksetup – bern Jul 29 '15 at 04:17