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 () {
74 iwconfig ${INTERFACE} ap off essid ""
75 iwlist ${INTERFACE} scan | \
76 sed -e "s/^[\t ]*//" | grep ^"Cell\|ESSID\|Quality\|Encryption" | \
78 -e "s/Cell [0-9]* - Address: \([0-9]*\)/AP,\1/" \
79 -e "s/^ *ESSID:\\\"\([^\\\"]*\)\\\".*$/ESSID,\1/" \
80 -e "s/Quality=\([0-9]*\).*$/QUALITY,\1/" \
81 -e "s/Encryption key:/ENCRYPTION,/" | \
84 if($1 == "ESSID") { essid=$2 }
85 else if($1 == "AP") { ap=$2 }
86 else if($1 == "QUALITY") { quality=$2; }
87 else if($1 == "ENCRYPTION") {
90 printf("%d,%s,%s,%s\n",quality,open,essid,ap);
92 if('${SHOW_CLOSED}') {
94 printf("%d,%s,%s,%s\n",quality,open,essid,ap);
99 sort -rn > ${SCAN_OUTPUT}
101 number_of_aps=$(wc -l ${SCAN_OUTPUT} | awk '{print $1}')
103 chmod a+rw ${SCAN_OUTPUT}
106 function redisplay () {
110 echo -n " ${VT_UNDERLINE}Hotspot selection${VT_RESET}"
111 if [[ $SHOW_CLOSED == 0 ]]; then
112 echo " (show only open networks)"
117 echo " [s,r] scan [m] switch the free-only view and scan [q] quit"
118 echo " [e] select essid + dhcp [a] selects essid/ap + dhcp [k] kill dhcp and quit"
121 for i in $(ifconfig -s | grep -E -v ^lo\|Iface\|wmaster | awk '{print $1}'); do
122 echo " Warning: $i is up"
126 if [[ ${number_of_aps} == "0" ]]; then
127 echo " ${VT_RED_FG}${VT_BOLD}No access point detected.${VT_RESET}"
130 awk < ${SCAN_OUTPUT} 'BEGIN { nb=1; FS="," }
132 if(nb == '$current_ap') {
137 if($2 == "(closed)") {
138 printf("'${VT_RED_FG}'");
139 printf("% 3s %s \"%s\" %s'${VT_RESET}'\n", $1, $2, $3, $4);
141 printf("'${VT_GREEN_FG}'");
142 printf("% 3s %s \"%s\" %s'${VT_RESET}'\n", $1, $2, $3, $4);
149 function get-selected-essid-ap () {
150 cat ${SCAN_OUTPUT} | \
151 awk 'BEGIN { nb=1; FS="," }
153 if(nb == '$current_ap') {
154 printf("%s,%s\n", $3, $4);
160 function kill-dhcp () {
161 PID=$(pidof dhclient)
162 if [[ ${PID} ]]; then
163 echo "Killing dhclient"
164 dhclient -x ${INTERFACE}
168 ######################################################################
170 function process-key () {
173 e) # Selects essid + dhcp
174 SELECTED=$(get-selected-essid-ap)
175 ESSID=$(echo ${SELECTED} | cut -f 1 -d,)
176 echo "Running DHCP on ${INTERFACE} for ESSID ${ESSID}."
177 iwconfig ${INTERFACE} ap auto essid "${ESSID}"
180 dhclient ${INTERFACE}
181 # if [[ $? == 0 ]]; then
182 # iwconfig ${INTERFACE} > ~/.hotspot-ap
184 ifconfig ${INTERFACE} mtu ${MTU}
189 "return"|a) # Selects essid/ap + dhcp
190 SELECTED=$(get-selected-essid-ap)
191 ESSID=$(echo ${SELECTED} | cut -f 1 -d,)
192 AP=$(echo ${SELECTED} | cut -f 2 -d,)
193 echo "Running DHCP on ${INTERFACE} for ESSID ${ESSID} and AP ${AP}."
194 iwconfig ${INTERFACE} ap ${AP} essid "${ESSID}"
197 dhclient ${INTERFACE}
198 # if [[ $? == 0 ]]; then
199 # iwconfig ${INTERFACE} > ~/.hotspot-ap
201 ifconfig ${INTERFACE} mtu ${MTU}
207 echo "Killing DHCP and bringing down ${INTERFACE}."
209 ifconfig ${INTERFACE} down
213 r|s) # re-scanning the access points
218 m) # Switching the mode to show the protected spots
219 SHOW_CLOSED=$((1-SHOW_CLOSED))
225 current_ap=$((current_ap+1))
226 if [[ $current_ap -gt $number_of_aps ]]; then
227 current_ap=$number_of_aps
233 current_ap=$((current_ap-1))
234 if [[ $current_ap -lt 1 ]]; then
250 ######################################################################
253 if [[ $1 == "-k" ]]; then
254 echo "Killing DHCP and bringing down ${INTERFACE}."
256 ifconfig ${INTERFACE} down
260 stty_state=$(stty -g)
262 trap sigint-handler SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM
264 stty -echo -icrnl -icanon -xcase min 1 time 0
266 echo ${VT_CURSOR_OFF}
270 current_keymap=default
272 interface_was_up=$(ifconfig | grep ${INTERFACE})
274 ifconfig ${INTERFACE} up
280 while [[ ${cont} == 1 ]]; do
282 CHAR=$(dd bs=1 count=1 2>/dev/null)
284 case ${current_keymap} in
285 default) ##################################################
305 current_keymap=escape
313 escape) ##################################################
316 process-key "esc-esc"
319 current_keymap=cursor
322 current_keymap=default
327 cursor) ##################################################
329 current_keymap=default
344 echo "Unknown keymap. This is an internal and weird bug."