[update] sats-dwc@.service can now check for connectivity before executing (so keep connection) or after (to check if all is ok)

This commit is contained in:
xdrm-brackets 2017-07-25 15:47:26 +02:00
parent 6fc62407e5
commit 389eee90ce
2 changed files with 54 additions and 24 deletions

View File

@ -8,20 +8,7 @@ After=sys-subsystem-net-devices-%i.device network-online.target
Type=oneshot
RemainAfterExit=yes
ExecStartPre=-/bin/echo "[0] Killing previous wpa_supplicant instances"
ExecStartPre=-/usr/bin/pkill wpa_supplicant
ExecStart=-/bin/echo "[1] Bringing %i up"
ExecStart=-/usr/sbin/ip link set dev %i up
ExecStart=-/bin/echo "[2] Running Dynamic Wireless Configuration"
ExecStart=/bin/bash /etc/wpa_supplicant/%i.dwc
ExecStartPost=-/bin/echo "[3] Running WPA Supplicant"
ExecStartPost=/sbin/wpa_supplicant -B -Dwext -i%i -c/etc/wpa_supplicant/%i.conf
ExecStartPost=-/bin/echo "[4] Running DHCP client"
ExecStartPost=/sbin/dhclient %i
ExecStartPost=-/bin/echo "[5] Checking connectivity"
ExecStartPost=/usr/bin/test -z "`ping xdrm.io -q -c 1 -i 1 2>&1 > /dev/null`"
[Install]
WantedBy=multi-user.target

View File

@ -6,6 +6,17 @@ slog(){
echo -e "$1" | sudo -u sats tee -a $LOGFILE > /dev/null;
}
failexit(){
slog " > failed";
exit 127;
}
connectedexit(){
slog " > connected";
slog "<<< Finished\n";
exit 0;
}
test ! -f $LOGFILE && sudo -u sats touch $LOGFILE;
HSALT="***SALT***";
@ -14,39 +25,71 @@ HPEPPER="***PEPPER***";
slog ">>> Dynamic Wireless Credentials";
# [1] Wifi scan for "SATS_*" APs
# [1] Checking connectivity
#========================================================#
slog " * 1. Checking connectivity"
[ -z "`ping xdrm.io -q -c 1 -i 1 2>&1 > /dev/null`" ] \
&& connectedexit \
|| slog " > offline";
# [2] Wifi scan for "SATS_*" APs
#========================================================#
# (1) Get interface name #
slog " * 1. Looking for wireless interface";
slog " * 2. Looking for wireless interface";
IFACE=`sudo ifconfig -a | grep -P "^w" | awk '{print $1}' | head -n 1`;
sudo ifconfig $IFACE up;
# (2) Manage no IFACE found #
test -z "$IFACE" && slog " > no wireless IFACE found" && exit;
slog " > got '$IFACE'";
test -z "$IFACE" && failexit;
slog " > done (got '$IFACE')";
# (3) Get nearest AP matching "SATS_*" and extract HASH #
slog " * 2. Looking for nearest AP matching 'SATS_.+'";
slog " * 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" && slog " > no AP found" && exit;
slog " > got 'SATS_$AP_HASH'";
test -z "$AP_HASH" && failexit;
slog " > done (got 'SATS_$AP_HASH')";
# [2] Calculate WIFI PASS from SSID hash
# [3] Calculate WIFI PASS from SSID hash
#========================================================#
slog " * 3. Processing WPA2 passphrase"
slog " * 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`;
slog " > done";
# [3] Update 'wpa_supplicant' configuration
# [4] Update 'wpa_supplicant' configuration
#========================================================#
echo -e "network={\n\tssid=\"SATS_$AP_HASH\"\n\tpsk=\"$PASS\"\n}" | sudo tee /etc/wpa_supplicant/$IFACE.conf;
slog "<<< Done\n";
# [5] Restart connection
#========================================================#
# (1) Starting wpa_supplicant #
slog " * 5. Starting wpa_supplicant"
sudo pkill wpa_supplicant;
sudo wpa_supplicant -B -Dwext -i$IFACE -c/etc/wpa_supplicant/$IFACE.conf \
&& slog " > done" \
|| failexit;
# (2) Starting dhclient #
slog " * 6. Starting dhclient"
if [ ! -z "`pgrep dhclient`" ]; then
slog " > already running";
else
sudo dhclient $IFACE \
&& slog " > done" \
|| failexit;
fi;
slog "<<< Finished\n";