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

时间:2015-07-10 13:43:29

标签: macos bash networking adhoc

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

3 个答案:

答案 0 :(得分: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)
}

答案 1 :(得分:1)

除了编写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()
}

答案 2 :(得分:0)

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