Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 539
Dan Ramos Avatar asked Jul 10 '15 13:07

Dan Ramos


2 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)
}
like image 120
2 revs, 2 users 90% Avatar answered Sep 18 '22 04:09

2 revs, 2 users 90%


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()
}
like image 29
Dan Ramos Avatar answered Sep 17 '22 04:09

Dan Ramos