setup/clone/utility/sats-wpa

56 lines
1.8 KiB
Bash

#!/bin/bash
slog(){
echo "$1" | sudo -u sats tee -a /home/sats/satsd/log/update.log > /dev/null;
}
HSALT="***SALT***";
HPEPPER="***PEPPER***";
slog ">>> Dynamic Wireless Credentials";
# [1] Wifi scan for "SATS_*" APs
#========================================================#
# (1) Get interface name #
slog "(1) Looking for wireless interface";
IFACE=`sudo ifconfig -a | grep -P "^w" | awk '{print $1}' | head -n 1`;
sudo ifconfig $IFACE up;
slog " > got '$IFACE'";
# (2) Get nearest AP matching "SATS_*" and extract HASH #
slog "(2) Looking for nearest AP matching 'SATS_.+'";
AP_HASH=`sudo iwlist $IFACE scan | grep -P "^\s*ESSID:\"SATS_.+\"\s*$" | sed 's/^[ \t]*ESSID:"SATS_//' | sed 's/"[ \t]*$//' | head -n 1`;
slog " > got 'SATS_$AP_HASH'";
# (3) Manage no AP found #
test -z "$AP_HASH" && slog "no AP found" && exit;
# [2] Calculate WIFI PASS from SSID hash
#========================================================#
slog "(3) Processing WPA2 passphrase"
PASS=`echo -ne "$HPEPPER$(echo -ne "${HSALT}${AP_HASH}" | sha512sum | sed 's/[ \t]*-$//')" | sha512sum | sed 's/[ \t]*-//' | cut -b 1-63`;
slog " > done";
# [3] Update 'wpa_supplicant.conf'
#========================================================#
# (1) Update SSID #
sudo cat /etc/wpa_supplicant/wpa_supplicant.conf | sed "s/^[ \t]*ssid=.*$/\tssid=\"SATS_$AP_HASH\"/" | sudo tee /etc/wpa_supplicant/wpa_supplicant.tmp;
# (2) Update PASS #
sudo cat /etc/wpa_supplicant/wpa_supplicant.tmp | sed "s/^[ \t]*psk=.*$/\tpsk=\"$PASS\"/" | sudo tee /etc/wpa_supplicant/wpa_supplicant.conf;
# (3) Remove tmp file #
sudo rm /etc/wpa_supplicant/wpa_supplicant.tmp;
slog "(4) Trying to connect";
sudo wpa_supplicant -B -Dwext -i$IFACE -c/etc/wpa_supplicant/wpa_supplicant.conf;
sudo dhclient $IFACE;
slog "<<< Done";