Search code examples
macosbashnetwork-programmingadhoc

Programmatically create Ad-Hoc network OS X


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?


Solution

  • 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()
    }