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 () {
75 iwconfig ${INTERFACE} ap off essid ""
76 iwlist ${INTERFACE} scan | \
77 sed -e "s/^[\t ]*//" | grep ^"Cell\|ESSID\|Quality\|Encryption" | \
79 -e "s/Cell [0-9]* - Address: \([0-9]*\)/AP,\1/" \
80 -e "s/^ *ESSID:\\\"\([^\\\"]*\)\\\".*$/ESSID,\1/" \
81 -e "s/Quality=\([0-9]*\).*$/QUALITY,\1/" \
82 -e "s/Encryption key:/ENCRYPTION,/" | \
85 if($1 == "ESSID") { essid=$2 }
86 else if($1 == "AP") { ap=$2 }
87 else if($1 == "QUALITY") { quality=$2; }
88 else if($1 == "ENCRYPTION") {
91 printf("%d,%s,%s,%s\n",quality,open,essid,ap);
93 if('${SHOW_CLOSED}') {
95 printf("%d,%s,%s,%s\n",quality,open,essid,ap);
100 sort -rn > ${SCAN_OUTPUT}
102 number_of_aps=$(wc -l ${SCAN_OUTPUT} | awk '{print $1}')
104 chmod a+rw ${SCAN_OUTPUT}
107 function redisplay () {
111 echo -n " ${VT_UNDERLINE}Hotspot selection${VT_RESET}"
112 if [[ $SHOW_CLOSED == 0 ]]; then
113 echo " (show only open networks)"
118 echo " [s,r] scan [m] switch the free-only view and scan [q] quit"
119 echo " [e] select essid + dhcp [a] selects essid/ap + dhcp [k] kill dhcp and quit"
122 for i in $(ifconfig -s | grep -E -v ^lo\|Iface\|wmaster | awk '{print $1}'); do
123 echo " Warning: $i is up"
127 if [[ ${number_of_aps} == "0" ]]; then
128 echo " ${VT_RED_FG}${VT_BOLD}No access point detected.${VT_RESET}"
131 awk < ${SCAN_OUTPUT} 'BEGIN { nb=1; FS="," }
133 if(nb == '$current_ap') {
138 if($2 == "(closed)") {
139 printf("'${VT_RED_FG}'");
140 printf("% 3s %s \"%s\" %s'${VT_RESET}'\n", $1, $2, $3, $4);
142 printf("'${VT_GREEN_FG}'");
143 printf("% 3s %s \"%s\" %s'${VT_RESET}'\n", $1, $2, $3, $4);
150 function get-selected-essid-ap () {
151 cat ${SCAN_OUTPUT} | \
152 awk 'BEGIN { nb=1; FS="," }
154 if(nb == '$current_ap') {
155 printf("%s,%s\n", $3, $4);
161 function kill-dhcp () {
162 PID=$(pidof dhclient)
163 if [[ ${PID} ]]; then
164 echo "Killing dhclient"
165 dhclient -x ${INTERFACE}
169 ######################################################################
171 function process-key () {
174 e) # Selects essid + dhcp
175 SELECTED=$(get-selected-essid-ap)
176 ESSID=$(echo ${SELECTED} | cut -f 1 -d,)
177 echo "Running DHCP on ${INTERFACE} for ESSID \"${ESSID}\"."
178 iwconfig ${INTERFACE} ap auto essid "${ESSID}"
181 dhclient ${INTERFACE}
182 # if [[ $? == 0 ]]; then
183 # iwconfig ${INTERFACE} > ~/.hotspot-ap
185 ifconfig ${INTERFACE} mtu ${MTU}
190 "return"|a) # Selects essid/ap + dhcp
191 SELECTED=$(get-selected-essid-ap)
192 ESSID=$(echo ${SELECTED} | cut -f 1 -d,)
193 AP=$(echo ${SELECTED} | cut -f 2 -d,)
194 echo "Running DHCP on ${INTERFACE} for ESSID \"${ESSID}\" and AP ${AP}."
195 iwconfig ${INTERFACE} ap ${AP} essid "${ESSID}"
198 dhclient ${INTERFACE}
199 # if [[ $? == 0 ]]; then
200 # iwconfig ${INTERFACE} > ~/.hotspot-ap
202 ifconfig ${INTERFACE} mtu ${MTU}
208 echo "Killing DHCP and bringing down ${INTERFACE}."
210 ifconfig ${INTERFACE} down
214 r|s) # re-scanning the access points
219 m) # Switching the mode to show the protected spots
220 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."