#!/usr/bin/env zsh ################################################################################ ## Custom network initialization script. ## 2012-10-29 ################################################################################ ## Deps: wpa_supplicant, ccrypt (optional) ## 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. ## 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'. # if [ -z "$(command -v ccrypt)" ]; 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="$(cat /etc/wpa_supplicant.conf)" TIMEOUT_LIMIT=500 pkill wpa_supplicant 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 # 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 ## 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 dhclient ${NET_INTERFACE} fi