3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation. #
8 # This program is distributed in the hope that it will be useful, but #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
11 # General Public License for more details. #
13 # You should have received a copy of the GNU General Public License #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
16 # Written by and Copyright (C) Francois Fleuret #
17 # Contact <francois@fleuret.org> for comments & bug reports #
18 #########################################################################
20 INTERFACE=${WIFI_INTERFACE}
22 [[ ${INTERFACE} ]] || INTERFACE=wlan0
26 SCAN_OUTPUT=$(mktemp /tmp/scanresult.XXXXXX)
29 ######################################################################
31 VT_CURSOR_OFF=$'\033[?25l'
32 VT_CURSOR_ON=$'\033[?25h'
36 VT_UNDERLINE=$'\033[4m'
39 VT_BLACK_FG=$'\033[30m'
41 VT_GREEN_FG=$'\033[32m'
42 VT_YELLOW_FG=$'\033[33m'
43 VT_BLUE_FG=$'\033[34m'
44 VT_MAGENTA_FG=$'\033[35m'
45 VT_CYAN_FG=$'\033[36m'
46 VT_WHITE_FG=$'\033[37m'
48 VT_BLACK_BG=$'\033[40m'
50 VT_GREEN_BG=$'\033[42m'
51 VT_YELLOW_BG=$'\033[43m'
52 VT_BLUE_BG=$'\033[44m'
53 VT_MAGENTA_BG=$'\033[45m'
54 VT_CYAN_BG=$'\033[46m'
55 VT_WHITE_BG=$'\033[47m'
57 ######################################################################
59 function cleanup-before-quit () {
63 if [[ "${interface_was_up}" == "" ]]; then
64 echo "The interface was down."
65 ifconfig ${INTERFACE} down
69 function sigint-handler () {
78 iwconfig ${INTERFACE} ap off essid ""
79 iwlist ${INTERFACE} scan | \
80 sed -e "s/^[\t ]*//" | grep ^"Cell\|ESSID\|Quality\|Encryption" | \
82 -e "s/Cell [0-9]* - Address: \([0-9]*\)/AP,\1/" \
83 -e "s/^ *ESSID:\\\"\([^\\\"]*\)\\\".*$/ESSID,\1/" \
84 -e "s/Quality=\([0-9]*\).*$/QUALITY,\1/" \
85 -e "s/Encryption key:/ENCRYPTION,/" | \
88 if($1 == "ESSID") { essid=$2 }
89 else if($1 == "AP") { ap=$2 }
90 else if($1 == "QUALITY") { quality=$2; }
91 else if($1 == "ENCRYPTION") {
94 printf("%d,%s,%s,%s\n",quality,open,essid,ap);
96 if('${SHOW_CLOSED}') {
98 printf("%d,%s,%s,%s\n",quality,open,essid,ap);
103 sort -rn > ${SCAN_OUTPUT}
105 number_of_aps=$(wc -l ${SCAN_OUTPUT} | awk '{print $1}')
107 chmod a+rw ${SCAN_OUTPUT}
110 function redisplay () {
114 echo -n " ${VT_UNDERLINE}Hotspot selection${VT_RESET}"
115 if [[ $SHOW_CLOSED == 0 ]]; then
116 echo " (show only open networks)"
121 echo " [s,r] scan [m] switch the free-only view and scan [q] quit"
122 echo " [e] select essid + dhcp [a] selects essid/ap + dhcp [k] kill dhcp and quit"
125 for i in $(ifconfig -s | grep -E -v ^lo\|Iface\|wmaster | awk '{print $1}'); do
126 echo " Warning: $i is up"
130 if [[ ${number_of_aps} == "0" ]]; then
131 echo " ${VT_RED_FG}${VT_BOLD}No access point detected.${VT_RESET}"
134 awk < ${SCAN_OUTPUT} 'BEGIN { nb=1; FS="," }
136 if(nb == '$current_ap') {
141 if($2 == "(closed)") {
142 printf("'${VT_RED_FG}'");
143 printf("% 3s %s \"%s\" %s'${VT_RESET}'\n", $1, $2, $3, $4);
145 printf("'${VT_GREEN_FG}'");
146 printf("% 3s %s \"%s\" %s'${VT_RESET}'\n", $1, $2, $3, $4);
153 function get-selected-essid-ap () {
154 cat ${SCAN_OUTPUT} | \
155 awk 'BEGIN { nb=1; FS="," }
157 if(nb == '$current_ap') {
158 printf("%s,%s\n", $3, $4);
164 function kill-dhcp () {
165 PID=$(pidof dhclient)
166 if [[ ${PID} ]]; then
167 echo "Killing dhclient"
168 dhclient -x ${INTERFACE}
172 ######################################################################
174 function process-key () {
177 e) # Selects essid + dhcp
178 SELECTED=$(get-selected-essid-ap)
179 ESSID=$(echo ${SELECTED} | cut -f 1 -d,)
180 echo "Running DHCP on ${INTERFACE} for ESSID \"${ESSID}\"."
181 iwconfig ${INTERFACE} ap auto essid "${ESSID}"
184 dhclient ${INTERFACE}
185 # if [[ $? == 0 ]]; then
186 # iwconfig ${INTERFACE} > ~/.hotspot-ap
188 ifconfig ${INTERFACE} mtu ${MTU}
193 "return"|a) # Selects essid/ap + dhcp
194 SELECTED=$(get-selected-essid-ap)
195 ESSID=$(echo ${SELECTED} | cut -f 1 -d,)
196 AP=$(echo ${SELECTED} | cut -f 2 -d,)
197 echo "Running DHCP on ${INTERFACE} for ESSID \"${ESSID}\" and AP ${AP}."
198 iwconfig ${INTERFACE} ap ${AP} essid "${ESSID}"
201 dhclient ${INTERFACE}
202 # if [[ $? == 0 ]]; then
203 # iwconfig ${INTERFACE} > ~/.hotspot-ap
205 ifconfig ${INTERFACE} mtu ${MTU}
211 echo "Killing DHCP and bringing down ${INTERFACE}."
213 ifconfig ${INTERFACE} down
217 r|s) # re-scanning the access points
222 m) # Switching the mode to show the protected spots
223 SHOW_CLOSED=$((1-SHOW_CLOSED))
229 current_ap=$((current_ap+1))
230 if [[ $current_ap -gt $number_of_aps ]]; then
231 current_ap=$number_of_aps
237 current_ap=$((current_ap-1))
238 if [[ $current_ap -lt 1 ]]; then
254 ######################################################################
257 if [[ $1 == "-k" ]]; then
258 echo "Killing DHCP and bringing down ${INTERFACE}."
260 ifconfig ${INTERFACE} down
264 trap cleanup-before-quit EXIT
266 stty_state=$(stty -g)
268 trap sigint-handler SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM
270 stty -echo -icrnl -icanon -xcase min 1 time 0
272 echo ${VT_CURSOR_OFF}
276 current_keymap=default
278 interface_was_up=$(ifconfig | grep ${INTERFACE})
280 ifconfig ${INTERFACE} up
286 while [[ ${cont} == 1 ]]; do
288 CHAR=$(dd bs=1 count=1 2>/dev/null)
290 case ${current_keymap} in
291 default) ##################################################
311 current_keymap=escape
319 escape) ##################################################
322 process-key "esc-esc"
325 current_keymap=cursor
328 current_keymap=default
333 cursor) ##################################################
335 current_keymap=default
350 echo "Unknown keymap. This is an internal and weird bug."