2012-11-01 14:47:35 +01:00
|
|
|
#!/usr/bin/env zsh
|
2012-06-20 19:07:54 +02:00
|
|
|
################################################################################
|
|
|
|
## Custom network initialization script.
|
2012-10-29 18:04:34 +01:00
|
|
|
## 2012-10-29
|
2012-06-20 19:07:54 +02:00
|
|
|
################################################################################
|
2013-01-01 16:15:21 +01:00
|
|
|
## Deps: wpa_supplicant, ccrypt (optional)
|
2012-11-01 14:47:35 +01:00
|
|
|
|
|
|
|
## We need to use wpa_supplicant's unencrypted config file. We store it in a
|
|
|
|
## variable to keep it secure, but the 'wpa_supplicant' command requires a
|
|
|
|
## file. We cannot use a pipe for that, because in that case the config would be
|
|
|
|
## accessible unencrypted. So we need to use an internal path with a syntax like
|
|
|
|
## <(...) which is not specified by POSIX. Ksh, bash and zsh can handle it.
|
|
|
|
|
2012-09-04 15:30:20 +02:00
|
|
|
## Use 'wpa_passphrase essid $PW >> /etc/wpa_supplicant.conf', where PW is a
|
|
|
|
## variable containing the password. You can set PW securely by using a shell
|
|
|
|
## built-in like 'read -s PW'.
|
2012-11-01 14:47:35 +01:00
|
|
|
|
2012-09-04 15:30:20 +02:00
|
|
|
## There is an Emacs plugin for editing crypted files directly. See 'man
|
|
|
|
## ccrypt'.
|
|
|
|
|
2013-01-01 16:15:21 +01:00
|
|
|
# if [ -z "$(command -v ccrypt)" ]; then
|
|
|
|
# echo "You need to have 'ccrypt' installed."
|
|
|
|
# exit
|
|
|
|
# fi
|
2012-06-20 19:07:54 +02:00
|
|
|
|
|
|
|
if [ $(id -u) -ne 0 ]; then
|
|
|
|
echo "You must be root to run this script."
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
NET_INTERFACE=wlan0
|
2013-01-01 16:15:21 +01:00
|
|
|
WPA_SUPPLICANT_CONF="$(cat /etc/wpa_supplicant.conf)"
|
2012-06-20 19:07:54 +02:00
|
|
|
TIMEOUT_LIMIT=500
|
|
|
|
|
|
|
|
pkill wpa_supplicant
|
|
|
|
|
2012-10-29 18:04:34 +01:00
|
|
|
if [ "$OSTYPE" = "linux-gnu" ] ; then
|
|
|
|
## Clean running processes if any.
|
|
|
|
pkill dhcpcd
|
|
|
|
|
|
|
|
## In case network inteface is not up.
|
|
|
|
ip link set ${NET_INTERFACE} up
|
2012-06-20 19:07:54 +02:00
|
|
|
|
|
|
|
# Associate if needed.
|
2013-02-04 11:59:10 +01:00
|
|
|
if [ "$1" = "-f" ] || [ -n "$(iwconfig ${NET_INTERFACE} | grep 'Not-Associated')" ]; then
|
2012-09-04 15:30:20 +02:00
|
|
|
wpa_supplicant -B -i ${NET_INTERFACE} -D wext -c <(echo "${WPA_SUPPLICANT_CONF}")
|
2012-06-20 19:07:54 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
## Wait until wpa_supplicant has finished association.
|
|
|
|
i=0
|
2012-09-01 15:32:47 +02:00
|
|
|
while [ -n "$(iwconfig ${NET_INTERFACE} | grep 'off/any')" ] && [ $i -lt $TIMEOUT_LIMIT ] ; do
|
2012-06-20 19:07:54 +02:00
|
|
|
i=$(($i+1))
|
|
|
|
done
|
2012-07-10 19:47:23 +02:00
|
|
|
|
2012-10-29 18:04:34 +01:00
|
|
|
## Get IP.
|
|
|
|
dhcpcd ${NET_INTERFACE}
|
|
|
|
|
|
|
|
else
|
|
|
|
## BSD
|
|
|
|
## Same comments as for Linux.
|
|
|
|
|
|
|
|
pkill dhclient
|
|
|
|
pkill wpa_supplicant
|
|
|
|
|
|
|
|
ifconfig wlan0 up
|
|
|
|
|
|
|
|
if [ -n "$(ifconfig ${NET_INTERFACE} | grep 'ssid ""')" ]; then
|
|
|
|
wpa_supplicant -B -i ${NET_INTERFACE} -c <(echo "${WPA_SUPPLICANT_CONF}")
|
|
|
|
fi
|
|
|
|
|
|
|
|
i=0
|
|
|
|
while [ -n "$(ifconfig ${NET_INTERFACE} | grep 'ssid ""')" ] && [ $i -lt $TIMEOUT_LIMIT ] ; do
|
|
|
|
i=$(($i+1))
|
|
|
|
done
|
2012-07-12 20:08:34 +02:00
|
|
|
|
2012-10-29 18:04:34 +01:00
|
|
|
dhclient ${NET_INTERFACE}
|
|
|
|
fi
|