Skip to content

Instantly share code, notes, and snippets.

@orubel
Created July 11, 2021 18:36
Show Gist options
  • Save orubel/ea308cc361913b0dd5561c53f5749018 to your computer and use it in GitHub Desktop.
Save orubel/ea308cc361913b0dd5561c53f5749018 to your computer and use it in GitHub Desktop.
Raspberry Pi WIFI detect script (GROOVY)
#!/usr/bin/env groovy
@Grab(group='commons-cli', module='commons-cli', version='1.4')
import groovy.json.JsonBuilder
class Main {
static main(args) {
CommandLineInterface cli = CommandLineInterface.INSTANCE
cli.parse(args)
}
}
enum CommandLineInterface {
INSTANCE
CliBuilder cliBuilder
CommandLineInterface() {
cliBuilder = new CliBuilder(
usage: 'wifi_setup [<options>]',
header: 'Options:',
footer: 'Place brief example usage here'
)
// set the amount of columns the usage message will be wide
cliBuilder.width = 80 // default is 74
cliBuilder.with {
h(longOpt: 'help','Print this help text and exit.')
l(longOpt: 'list','Change Network Config for Clustered IOT Devices')
}
}
/* OPTIONS
-h,--help
-l,--list
*/
void parse(args) {
OptionAccessor options = cliBuilder.parse(args)
if (!options) {
System.err << 'Error while parsing command-line options.\n'
System.exit 1
}else{
if (options.h) {
cliBuilder.usage()
System.exit 0
}
// list wifi; parse data in clean re-usable format
if (options.l) {
ArrayList wifiList = scanWifiList()
if(wifiList[0]){
println("ERR: "+wifiList[0])
}else{
List lines = wifiList[1].split("\\r?\\n")
lines = lines*.trim()
LinkedHashMap wifiSettings = []
String lastField = ''
LinkedHashMap cell = []
Integer qualityCutoff = 55
Integer inc = 1
String currentCell = inc.toString()
lines.each(){
switch(it){
case ~/Cell [0-9]{2}.*/:
def matcher = it =~ /Cell [0-9]{2} - Address: (.*)/
if(matcher.matches()){
if(!cell.isEmpty()){
if(cell.quality.toInteger()>=qualityCutoff){
wifiSettings.put(currentCell,cell)
inc++
}
cell = []
}
currentCell = inc.toString()
cell.put('address',matcher[0][1])
break
}
case ~/Quality=([0-9]{2})\/([0-9]{2}) .*/:
def matcher = it =~ /Quality=([0-9]{2})\/([0-9]{2}) .*/
cell.put('quality',matcher[0][1])
break
default:
def matcher = it =~ /(ESSID|IE):(.*)/
if(matcher.matches()){
def key = matcher[0][1]
def val = matcher[0][2]
switch(key){
case 'ESSID':
lastField = key
cell.put('essid',val)
break
case 'IE':
lastField = key
cell.put('type','WPA')
break
}
}else{
def matcher2 = it =~ /(Group Cipher|Pairwise Ciphers \(2\)|Authentication Suites \(1\)) : (.*)/
if(matcher2.matches()){
def key = matcher2[0][1]
def val = matcher2[0][2]
switch(key){
case 'Group Cipher':
cell.put('group',val)
break
case 'Pairwise Ciphers (2)':
cell.put('pairwise',val)
break
case 'Authentication Suites (1)':
cell.put('auth',val)
break
}
}
}
break
}
}
//println(wifiSettings)
JsonBuilder jsonBuilder = new JsonBuilder(wifiSettings)
String json = jsonBuilder.toPrettyString()
// serialize and store
/*
def kryo = new Kryo()
kryo.register(Path, new PathSerializer(kryo))
def path = Paths.get('hola')
Output output = new Output(new FileOutputStream("file.bin"));
kryo.writeObject(output, path);
output.close();
*/
println(json)
}
}
}
}
ArrayList scanWifiList(){
StringBuffer sout = new StringBuffer()
StringBuffer serr = new StringBuffer()
def process = ['/bin/bash','-c','sudo iwlist wlan0 scanning | egrep \'Cell|Quality|ESSID|IEEE 802.11i/WPA2 Version 1|WPA Version 1|Group Cipher|Pairwise Ciphers|Authentication Suites\''].execute()
process.waitForProcessOutput(sout,serr)
//def value = process.exitValue()
process.waitForOrKill(1000)
return [serr.toString(),sout.toString()]
}
//parseWifiList(String wifiList){
//
//}
/*
void setupNetworking(String ssid, String password){
ssid=$1
password=$2
echo "
network={
ssid=\"$1\"
psk=\"$2\"
}" | sudo tee -a "${rootPath}/etc/wpa_supplicant/wpa_supplicant.conf" > /dev/null
}
*/
}
class PathSerializer extends FieldSerializer<Path> {
PathSerializer(Kryo kryo) {
super(kryo, Path)
}
public void write (Kryo kryo, Output output, Path path) {
def uri = path.toUri().toString()
kryo.writeObject(output, uri)
}
public Path read (Kryo kryo, Input input, Class<Path> type) {
def uri = kryo.readObject(input,String)
Paths.get(new URI(uri))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment