54 lines
1.4 KiB
Bash
Executable File
54 lines
1.4 KiB
Bash
Executable File
#!/bin/bash
|
|
################################################################################
|
|
## Custom network initialization script.
|
|
## 2012-09-04
|
|
################################################################################
|
|
## Deps: wpa_supplicant, ccrypt
|
|
##
|
|
## 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'.
|
|
##
|
|
## There is an Emacs plugin for editing crypted files directly. See 'man
|
|
## ccrypt'.
|
|
|
|
type ccrypt > /dev/null
|
|
if [ $? -ne 0 ]; then
|
|
echo "You need to have 'ccrypt' installed."
|
|
exit
|
|
fi
|
|
|
|
if [ $(id -u) -ne 0 ]; then
|
|
echo "You must be root to run this script."
|
|
exit
|
|
fi
|
|
|
|
NET_INTERFACE=wlan0
|
|
WPA_SUPPLICANT_CONF="$(ccat /etc/wpa_supplicant.conf.cpt)"
|
|
TIMEOUT_LIMIT=500
|
|
|
|
## Clean running processes if any.
|
|
pkill dhcpcd
|
|
pkill wpa_supplicant
|
|
|
|
## In case network inteface is not up.
|
|
ip link set ${NET_INTERFACE} up
|
|
|
|
## Connection.
|
|
if [ $# -eq 0 ]; then
|
|
# Associate if needed.
|
|
if [ -n "$(iwconfig ${NET_INTERFACE} | grep 'Not-Associated')" ]; then
|
|
wpa_supplicant -B -i ${NET_INTERFACE} -D wext -c <(echo "${WPA_SUPPLICANT_CONF}")
|
|
fi
|
|
|
|
## Wait until wpa_supplicant has finished association.
|
|
i=0
|
|
while [ -n "$(iwconfig ${NET_INTERFACE} | grep 'off/any')" ] && [ $i -lt $TIMEOUT_LIMIT ] ; do
|
|
i=$(($i+1))
|
|
done
|
|
fi
|
|
|
|
## Get IP.
|
|
dhcpcd ${NET_INTERFACE}
|
|
|