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 #########################################################################
23 SCAN_OUTPUT=$(mktemp /tmp/scanresult.XXXXXX)
26 ######################################################################
28 VT_CURSOR_OFF=$'\033[?25l'
29 VT_CURSOR_ON=$'\033[?25h'
33 VT_UNDERLINE=$'\033[4m'
36 VT_BLACK_FG=$'\033[30m'
38 VT_GREEN_FG=$'\033[32m'
39 VT_YELLOW_FG=$'\033[33m'
40 VT_BLUE_FG=$'\033[34m'
41 VT_MAGENTA_FG=$'\033[35m'
42 VT_CYAN_FG=$'\033[36m'
43 VT_WHITE_FG=$'\033[37m'
45 VT_BLACK_BG=$'\033[40m'
47 VT_GREEN_BG=$'\033[42m'
48 VT_YELLOW_BG=$'\033[43m'
49 VT_BLUE_BG=$'\033[44m'
50 VT_MAGENTA_BG=$'\033[45m'
51 VT_CYAN_BG=$'\033[46m'
52 VT_WHITE_BG=$'\033[47m'
54 ######################################################################
56 function cleanup-before-quit () {
60 if [[ "${interface_was_up}" == "" ]]; then
61 echo "The interface was down."
62 ifconfig ${INTERFACE} down
66 function sigint-handler () {
73 iwconfig ${INTERFACE} ap off essid ""
74 iwlist ${INTERFACE} scan | \
75 sed -e "s/^[\t ]*//" | grep ^"Cell\|ESSID\|Quality\|Encryption" | \
77 -e "s/Cell [0-9]* - Address: \([0-9]*\)/AP,\1/" \
78 -e "s/^ *ESSID:\\\"\([^\\\"]*\)\\\".*$/ESSID,\1/" \
79 -e "s/Quality=\([0-9]*\).*$/QUALITY,\1/" \
80 -e "s/Encryption key:/ENCRYPTION,/" | \
83 if($1 == "ESSID") { essid=$2 }
84 else if($1 == "AP") { ap=$2 }
85 else if($1 == "QUALITY") { quality=$2; }
86 else if($1 == "ENCRYPTION") {
89 printf("%d,%s,%s,%s\n",quality,open,essid,ap);
91 if('${SHOW_CLOSED}') {
93 printf("%d,%s,%s,%s\n",quality,open,essid,ap);
98 sort -rn > ${SCAN_OUTPUT}
100 number_of_aps=$(wc -l ${SCAN_OUTPUT} | awk '{print $1}')
102 chmod a+rw ${SCAN_OUTPUT}
105 function redisplay () {
109 echo -n " ${VT_UNDERLINE}Hotspot selection${VT_RESET}"
110 if [[ $SHOW_CLOSED == 0 ]]; then
111 echo " (show only open networks)"
116 echo " [s,r] scan [m] switch the free-only view and scan [q] quit"
117 echo " [e] select essid + dhcp [a] selects essid/ap + dhcp [k] kill dhcp and quit"
120 for i in $(ifconfig -s | grep -E -v ^lo\|Iface\|wmaster | awk '{print $1}'); do
121 echo " Warning: $i is up"
125 if [[ ${number_of_aps} == "0" ]]; then
126 echo " ${VT_RED_FG}${VT_BOLD}No access point detected.${VT_RESET}"
129 awk < ${SCAN_OUTPUT} 'BEGIN { nb=1; FS="," }
131 if(nb == '$current_ap') {
136 if($2 == "(closed)") {
137 printf("'${VT_RED_FG}'");
138 printf("% 3s %s \"%s\" %s'${VT_RESET}'\n", $1, $2, $3, $4);
140 printf("'${VT_GREEN_FG}'");
141 printf("% 3s %s \"%s\" %s'${VT_RESET}'\n", $1, $2, $3, $4);
148 function get-selected-essid-ap () {
149 cat ${SCAN_OUTPUT} | \
150 awk 'BEGIN { nb=1; FS="," }
152 if(nb == '$current_ap') {
153 printf("%s,%s\n", $3, $4);
159 function kill-dhcp () {
160 PID=$(pidof dhclient)
161 if [[ ${PID} ]]; then
162 echo "Killing dhclient"
163 dhclient -x ${INTERFACE}
167 ######################################################################
169 function process-key () {
172 e) # Selects essid + dhcp
173 SELECTED=$(get-selected-essid-ap)
174 ESSID=$(echo ${SELECTED} | cut -f 1 -d,)
175 echo "Running DHCP on ${INTERFACE} for ESSID ${ESSID}."
176 iwconfig ${INTERFACE} ap auto essid "${ESSID}"
179 dhclient ${INTERFACE}
180 # if [[ $? == 0 ]]; then
181 # iwconfig ${INTERFACE} > ~/.hotspot-ap
183 ifconfig ${INTERFACE} mtu ${MTU}
188 "return"|a) # Selects essid/ap + dhcp
189 SELECTED=$(get-selected-essid-ap)
190 ESSID=$(echo ${SELECTED} | cut -f 1 -d,)
191 AP=$(echo ${SELECTED} | cut -f 2 -d,)
192 echo "Running DHCP on ${INTERFACE} for ESSID ${ESSID} and AP ${AP}."
193 iwconfig ${INTERFACE} ap ${AP} essid "${ESSID}"
196 dhclient ${INTERFACE}
197 # if [[ $? == 0 ]]; then
198 # iwconfig ${INTERFACE} > ~/.hotspot-ap
200 ifconfig ${INTERFACE} mtu ${MTU}
206 echo "Killing DHCP and bringing down ${INTERFACE}."
208 ifconfig ${INTERFACE} down
212 r|s) # re-scanning the access points
218 m) # Switching the mode to show the protected spots
219 SHOW_CLOSED=$((1-SHOW_CLOSED))
226 current_ap=$((current_ap+1))
227 if [[ $current_ap -gt $number_of_aps ]]; then
228 current_ap=$number_of_aps
234 current_ap=$((current_ap-1))
235 if [[ $current_ap -lt 1 ]]; then
251 ######################################################################
254 if [[ $1 == "-k" ]]; then
255 echo "Killing DHCP and bringing down ${INTERFACE}."
257 ifconfig ${INTERFACE} down
261 stty_state=$(stty -g)
263 trap sigint-handler SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM
265 stty -echo -icrnl -icanon -xcase min 1 time 0
267 echo ${VT_CURSOR_OFF}
271 current_keymap=default
273 interface_was_up=$(ifconfig | grep ${INTERFACE})
275 ifconfig ${INTERFACE} up
281 while [[ ${cont} == 1 ]]; do
283 CHAR=$(dd bs=1 count=1 2>/dev/null)
285 case ${current_keymap} in
286 default) ##################################################
306 current_keymap=escape
314 escape) ##################################################
317 process-key "esc-esc"
320 current_keymap=cursor
323 current_keymap=default
328 cursor) ##################################################
330 current_keymap=default
345 echo "Unknown keymap. This is an internal and weird bug."