Last active
February 2, 2017 21:15
-
-
Save joesan/236fc862944c91fa35aa8d16e2294a8c to your computer and use it in GitHub Desktop.
Programatic WiFi Connectivity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object WiFiConnector extends App { | |
if (System.getProperty("os.name").contains("Mac")) | |
scanMac() | |
else | |
scanWindows() | |
def scanWindows() = { | |
val builder = new ProcessBuilder( | |
"cmd.exe", "/c", "netsh wlan show profile" | |
) | |
builder.redirectErrorStream(true) | |
val process = builder.start() | |
val read = scala.io.Source.fromInputStream(process.getInputStream).getLines().toSeq | |
read foreach println | |
} | |
def scanMac() = { | |
val sensorIp = "192.168.253.1" | |
val probe = s"$sensorIp/sys" | |
val networkPortsCommand = "networksetup -listallhardwareports" | |
val readCommand = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport scan" | |
val connectCommand = (ssid: String, pass: String, wifiDevice: String) => { | |
println(s"connecting to $ssid with pass $pass") | |
s"networksetup -setairportnetwork $wifiDevice $ssid $pass" | |
} | |
val rt = Runtime.getRuntime | |
val networkPorts = rt.exec(networkPortsCommand) | |
val wifiDevice = { | |
scala.io.Source.fromInputStream(networkPorts.getInputStream).getLines() | |
.filter(_.nonEmpty).toSeq | |
.grouped(3) | |
.toSeq | |
} | |
val deviceIdentifier = wifiDevice.flatMap { | |
case elems => { | |
if (elems.exists(_.contains("Wi-Fi"))) { | |
val device = elems.filter(_.contains("Device")) | |
if (device.headOption.nonEmpty) | |
device.head.split(" ").toSeq.last | |
else Seq.empty[String] | |
} | |
else Seq.empty[String] | |
} | |
} | |
val readProc = rt.exec(readCommand) | |
val read = scala.io.Source.fromInputStream(readProc.getInputStream).getLines().toSeq | |
read foreach println | |
val filtered = read filter (_.contains("your required ssid name")) | |
val ssid = filtered.head.split(" ").toSeq.filter(_.nonEmpty).head | |
println(s"ssid obtained is $ssid ") | |
// turn off Wifi | |
//rt.exec("networksetup -setairportpower en1 off") | |
Thread.sleep(500) | |
//rt.exec("networksetup -setairportpower en1 on") | |
Thread.sleep(500) | |
//val connectProc = rt.exec(connectCommand(ssid, "24681012", deviceIdentifier)) | |
Thread.sleep(10000000) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment