2017-07-18 13:34:07 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2017-07-22 14:14:06 +00:00
|
|
|
|
|
|
|
LOGFILE="/home/sats/satsd/log/dwc.log";
|
2017-07-18 13:34:07 +00:00
|
|
|
slog(){
|
2017-07-22 14:14:06 +00:00
|
|
|
echo -e "$1" | sudo -u sats tee -a $LOGFILE > /dev/null;
|
2017-07-18 13:34:07 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 13:47:26 +00:00
|
|
|
failexit(){
|
2017-07-25 13:57:04 +00:00
|
|
|
slog "[`date +%s`] > failed";
|
2017-07-25 13:47:26 +00:00
|
|
|
exit 127;
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedexit(){
|
2017-07-25 13:57:04 +00:00
|
|
|
slog "[`date +%s`] > connected";
|
|
|
|
slog "[`date +%s`] <<< Finished\n";
|
2017-07-25 13:47:26 +00:00
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
2017-07-22 14:14:06 +00:00
|
|
|
test ! -f $LOGFILE && sudo -u sats touch $LOGFILE;
|
|
|
|
|
2017-07-18 13:34:07 +00:00
|
|
|
HSALT="***SALT***";
|
|
|
|
HPEPPER="***PEPPER***";
|
|
|
|
|
2017-07-22 14:14:06 +00:00
|
|
|
|
2017-07-25 13:57:04 +00:00
|
|
|
slog "[`date +%s`] >>> Dynamic Wireless Credentials";
|
2017-07-18 13:34:07 +00:00
|
|
|
|
2017-07-25 13:47:26 +00:00
|
|
|
|
|
|
|
# [1] Checking connectivity
|
|
|
|
#========================================================#
|
2017-07-25 13:57:04 +00:00
|
|
|
slog "[`date +%s`] * 1. Checking connectivity"
|
2017-07-25 13:47:26 +00:00
|
|
|
[ -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";
|
2017-07-25 13:47:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# [2] Wifi scan for "SATS_*" APs
|
2017-07-18 13:34:07 +00:00
|
|
|
#========================================================#
|
|
|
|
|
|
|
|
# (1) Get interface name #
|
2017-07-25 13:57:04 +00:00
|
|
|
slog "[`date +%s`] * 2. Looking for wireless interface";
|
2017-09-29 08:03:07 +00:00
|
|
|
IFACE=`sudo ifconfig -a | grep -P "^w" | awk '{print $1}' | sed 's/:$//' | head -n 1`;
|
2017-07-20 13:46:11 +00:00
|
|
|
sudo ifconfig $IFACE up;
|
2017-07-20 14:17:35 +00:00
|
|
|
|
|
|
|
# (2) Manage no IFACE found #
|
2017-07-25 13:47:26 +00:00
|
|
|
test -z "$IFACE" && failexit;
|
2017-07-25 13:57:04 +00:00
|
|
|
slog "[`date +%s`] > done (got '$IFACE')";
|
2017-07-25 13:47:26 +00:00
|
|
|
|
2017-07-18 13:34:07 +00:00
|
|
|
|
2017-07-20 14:17:35 +00:00
|
|
|
# (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_.+'";
|
2017-07-20 13:46:11 +00:00
|
|
|
AP_HASH=`sudo iwlist $IFACE scan | grep -P "^\s*ESSID:\"SATS_.+\"\s*$" | sed 's/^[ \t]*ESSID:"SATS_//' | sed 's/"[ \t]*$//' | head -n 1`;
|
2017-07-20 14:17:35 +00:00
|
|
|
|
|
|
|
# (4) Manage no AP found #
|
2017-07-25 13:47:26 +00:00
|
|
|
test -z "$AP_HASH" && failexit;
|
2017-07-25 13:57:04 +00:00
|
|
|
slog "[`date +%s`] > done (got 'SATS_$AP_HASH')";
|
2017-07-18 13:34:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-25 13:47:26 +00:00
|
|
|
# [3] Calculate WIFI PASS from SSID hash
|
2017-07-18 13:34:07 +00:00
|
|
|
#========================================================#
|
2017-07-25 13:57:04 +00:00
|
|
|
slog "[`date +%s`] * 4. Processing WPA2 passphrase"
|
2017-07-20 13:46:11 +00:00
|
|
|
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";
|
2017-07-18 13:34:07 +00:00
|
|
|
|
|
|
|
|
2017-07-25 13:47:26 +00:00
|
|
|
# [4] Update 'wpa_supplicant' configuration
|
2017-07-18 13:34:07 +00:00
|
|
|
#========================================================#
|
2017-10-02 13:44:08 +00:00
|
|
|
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;
|
2017-07-18 13:34:07 +00:00
|
|
|
|
2017-07-20 13:46:11 +00:00
|
|
|
|
2017-07-25 13:47:26 +00:00
|
|
|
# [5] Restart connection
|
|
|
|
#========================================================#
|
|
|
|
# (1) Starting wpa_supplicant #
|
2017-07-25 13:57:04 +00:00
|
|
|
slog "[`date +%s`] * 5. Starting wpa_supplicant"
|
2017-07-25 13:47:26 +00:00
|
|
|
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" \
|
2017-07-25 13:47:26 +00:00
|
|
|
|| failexit;
|
|
|
|
|
|
|
|
|
|
|
|
# (2) Starting dhclient #
|
2017-07-25 13:57:04 +00:00
|
|
|
slog "[`date +%s`] * 6. Starting dhclient"
|
2017-07-25 13:47:26 +00:00
|
|
|
if [ ! -z "`pgrep dhclient`" ]; then
|
2017-07-25 13:57:04 +00:00
|
|
|
slog "[`date +%s`] > already running";
|
2017-07-25 13:47:26 +00:00
|
|
|
else
|
|
|
|
sudo dhclient $IFACE \
|
2017-07-25 13:57:04 +00:00
|
|
|
&& slog "[`date +%s`] > done" \
|
2017-07-25 13:47:26 +00:00
|
|
|
|| failexit;
|
|
|
|
fi;
|
|
|
|
|
|
|
|
|
2017-07-25 13:57:04 +00:00
|
|
|
slog "[`date +%s`] <<< Finished\n";
|