以编程方式创建Ad-Hoc网络OS X.

Dan*_*mos 9 macos bash networking adhoc

如何在OS X上创建具有指定SSID和密码的无线adhoc网络?我试着查看networksetup手册页,但没有想出任何东西来完成这个.我应该使用另一个命令吗?

Joe*_*and 5

在OSX 10.13中,我不得不将@ dan-ramos的代码修改为:

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)
}
Run Code Online (Sandbox Code Playgroud)


Dan*_*mos 2

除了编写 Swift 脚本之外,我没有找到任何真正的方法来做到这一点:

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()
}
Run Code Online (Sandbox Code Playgroud)