setup/clone/utility/wlan0.dwc

96 lines
2.6 KiB
Plaintext
Raw Normal View History

#!/bin/bash
LOGFILE="/home/sats/satsd/log/dwc.log";
slog(){
echo -e "$1" | sudo -u sats tee -a $LOGFILE > /dev/null;
}
failexit(){
2017-07-25 13:57:04 +00:00
slog "[`date +%s`] > failed";
exit 127;
}
connectedexit(){
2017-07-25 13:57:04 +00:00
slog "[`date +%s`] > connected";
slog "[`date +%s`] <<< Finished\n";
exit 0;
}
test ! -f $LOGFILE && sudo -u sats touch $LOGFILE;
HSALT="***SALT***";
HPEPPER="***PEPPER***";
2017-07-25 13:57:04 +00:00
slog "[`date +%s`] >>> Dynamic Wireless Credentials";
# [1] Checking connectivity
#========================================================#
2017-07-25 13:57:04 +00:00
slog "[`date +%s`] * 1. Checking connectivity"
[ -z "`ping xdrm.io -q -c 1 -i 1 2>&1 > /dev/null`" ] \
&& connectedexit \
2017-07-25 13:57:04 +00:00
|| slog "[`date +%s`] > offline";
# [2] Wifi scan for "SATS_*" APs
#========================================================#
# (1) Get interface name #
2017-07-25 13:57:04 +00:00
slog "[`date +%s`] * 2. Looking for wireless interface";
IFACE=`sudo ifconfig -a | grep -P "^w" | awk '{print $1}' | sed 's/:$//' | head -n 1`;
sudo ifconfig $IFACE up;
# (2) Manage no IFACE found #
test -z "$IFACE" && failexit;
2017-07-25 13:57:04 +00:00
slog "[`date +%s`] > done (got '$IFACE')";
# (3) Get nearest AP matching "SATS_*" and extract HASH #
2017-07-25 13:57:04 +00:00
slog "[`date +%s`] * 3. 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`;
# (4) Manage no AP found #
test -z "$AP_HASH" && failexit;
2017-07-25 13:57:04 +00:00
slog "[`date +%s`] > done (got 'SATS_$AP_HASH')";
# [3] Calculate WIFI PASS from SSID hash
#========================================================#
2017-07-25 13:57:04 +00:00
slog "[`date +%s`] * 4. Processing WPA2 passphrase"
PASS=`echo -ne "$HPEPPER$(echo -ne "${HSALT}${AP_HASH}" | sha512sum | sed 's/[ \t]*-$//')" | sha512sum | sed 's/[ \t]*-//' | cut -b 1-63`;
2017-07-25 13:57:04 +00:00
slog "[`date +%s`] > done";
# [4] Update 'wpa_supplicant' configuration
#========================================================#
echo -e "ctrl_interface=/var/run/wpa_supplicant\n\nnetwork={\n\tssid=\"SATS_$AP_HASH\"\n\tpsk=\"$PASS\"\n}" | sudo tee /etc/wpa_supplicant/$IFACE.conf;
# [5] Restart connection
#========================================================#
# (1) Starting wpa_supplicant #
2017-07-25 13:57:04 +00:00
slog "[`date +%s`] * 5. Starting wpa_supplicant"
sudo pkill wpa_supplicant;
sudo wpa_supplicant -B -Dwext -i$IFACE -c/etc/wpa_supplicant/$IFACE.conf \
2017-07-25 13:57:04 +00:00
&& slog "[`date +%s`] > done" \
|| failexit;
# (2) Starting dhclient #
2017-07-25 13:57:04 +00:00
slog "[`date +%s`] * 6. Starting dhclient"
if [ ! -z "`pgrep dhclient`" ]; then
2017-07-25 13:57:04 +00:00
slog "[`date +%s`] > already running";
else
sudo dhclient $IFACE \
2017-07-25 13:57:04 +00:00
&& slog "[`date +%s`] > done" \
|| failexit;
fi;
2017-07-25 13:57:04 +00:00
slog "[`date +%s`] <<< Finished\n";