slitaz-tools rev 863 5.8.2

slitaz-config: add wifi-setup (so now user can widely config slitaz via: editing files, Ncurses boxes, GTK boxes, web interface
author Christophe Lincoln <pankso@slitaz.org>
date Sun Apr 20 20:39:49 2014 +0200 (2014-04-20)
parents a7c35e7e1fce
children ebe542616431
files tinyutils/slitaz-config
line diff
     1.1 --- a/tinyutils/slitaz-config	Sun Apr 20 17:26:40 2014 +0200
     1.2 +++ b/tinyutils/slitaz-config	Sun Apr 20 20:39:49 2014 +0200
     1.3 @@ -72,6 +72,128 @@
     1.4  	sleep 4
     1.5  }
     1.6  
     1.7 +# Catch ESSIDs and format output for Ncurses Dialog or GTK Yad tree. 
     1.8 +# We get the list of networks by Cell and without spaces. 
     1.9 +# Usage: detect_wifi --output=gtk (default output to dialog)
    1.10 +detect_wifi() {
    1.11 +	. /etc/network.conf
    1.12 +	ifconfig $WIFI_INTERFACE up
    1.13 +	if [ -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
    1.14 +		for i in $(iwlist $WIFI_INTERFACE scan | sed s/"Cell "/Cell-/ | grep "Cell-" | awk '{print $1}')
    1.15 +		do
    1.16 +			scan=$(iwlist $WIFI_INTERFACE scan last | \
    1.17 +				awk '/(Cell|ESS|Qual|Encry|IE: WPA|WPA2)/ {print}' | \
    1.18 +				sed s/"Cell "/Cell-/ | grep -A 5 "$i")
    1.19 +			essid=$(echo $scan | cut -d '"' -f 2)
    1.20 +
    1.21 +			if echo "$scan" | grep -q Quality; then
    1.22 +				quality=$(echo $scan | sed 's/.*Quality=\([^ ]*\).*/\1/' | sed 's/.*Quality:\([^ ]*\).*/\1/')
    1.23 +			else
    1.24 +				quality="$(_n '-----')"
    1.25 +			fi
    1.26 +
    1.27 +			crypto=$(echo $scan | sed 's/.*key:\([^ ]*\).*/\1/')
    1.28 +			# Check encryption type
    1.29 +			if echo "$scan" | grep -q WPA*; then
    1.30 +				crypto="WPA"
    1.31 +			fi
    1.32 +
    1.33 +			# Connected or not connected...
    1.34 +			if ifconfig | grep -A 1 $WIFI_INTERFACE | \
    1.35 +				grep -q inet && iwconfig $WIFI_INTERFACE | \
    1.36 +				grep ESSID | grep -q -w "$essid"; then
    1.37 +				status=connected
    1.38 +			else
    1.39 +				status="--"
    1.40 +			fi
    1.41 +			
    1.42 +			# Output
    1.43 +			case "$output" in
    1.44 +				gtk)
    1.45 +					echo -e "$( _n 'any')\n$(_n 'N/A')\n$(_n 'none')\n$(_n '-')"
    1.46 +					echo -e "$essid\n$quality\n$crypto\n$status" ;;
    1.47 +				*) 
    1.48 +					echo "$essid" "QA_${quality}_KEY_${crypto}_${status}" ;;
    1.49 +			esac
    1.50 +		done
    1.51 +	fi
    1.52 +}
    1.53 +
    1.54 +# Wireless config so users dont have to edit any config files on post
    1.55 +# install to get connected. If the wired connection is used it will auto
    1.56 +# connect with DHCP so no need for a dialog frontend.
    1.57 +wifi_setup() {
    1.58 +	. /etc/network.conf
    1.59 +	dialog \
    1.60 +		--clear --title "$title" \
    1.61 +		--ok-label "Select" \
    1.62 +		--menu "\n$(gettext 'Connect to a Wi-Fi network')" \
    1.63 +		${height} ${width} 14 \
    1.64 +"any" "Quality_N/A" \
    1.65 +$(detect_wifi) 2>${tmp}
    1.66 +	
    1.67 +	# Handle options
    1.68 +	case "${?}" in
    1.69 +		1|255) rm -rf ${tmpdir} && exit 0 ;;
    1.70 +		0) essid=$(cat $tmp) ;;
    1.71 +	esac
    1.72 +	
    1.73 +	# Check if we need to prompt user for an encrypted network
    1.74 +	scan=$(iwlist $WIFI_INTERFACE scan | \
    1.75 +		awk '/(Cell|ESS|Qual|Encry|IE: WPA|WPA2)/ {print}' | \
    1.76 +		sed s/"Cell "/Cell-/ | grep -A 5 "$essid")
    1.77 +	if echo "$scan" | sed 's/.*key:\([^ ]*\).*/\1/' | grep -q WPA*; then
    1.78 +		dialog --title "{ Wi-Fi Password }" --colors \
    1.79 +			--inputbox "\nEnter wifi key (password) for \Zb\Z4${essid}" \
    1.80 +			12 ${width} 2>${tmp}
    1.81 +		key=$(cat $tmp)
    1.82 +		[ "$key" == "" ] && return 0
    1.83 +	fi
    1.84 +	
    1.85 +	# Configure connection
    1.86 +	{
    1.87 +		echo "XXX" && echo 30
    1.88 +		echo -e "\nShuting down network interfaces..."
    1.89 +		echo "XXX"
    1.90 +		stopd network.sh >/dev/null 2>&1 && sleep 2
    1.91 +		
    1.92 +		echo "XXX" && echo 30
    1.93 +		echo -e "\nConfiguring: /etc/network.conf..."
    1.94 +		echo "XXX"
    1.95 +		# WIFI_KEY_TYPE=any should work for WEP/WPA*
    1.96 +		sed -i \
    1.97 +			-e s"/^WIFI_ESSID=.*/WIFI_ESSID=\"$essid\""/ \
    1.98 +			-e s"/^WIFI_KEY=.*/WIFI_KEY=\"$key\"/" \
    1.99 +			-e s"/^WIFI_KEY_TYPE=.*/WIFI_KEY_TYPE=\"any\"/" \
   1.100 +			/etc/network.conf
   1.101 +		sleep 1
   1.102 +		
   1.103 +		echo "XXX" && echo 60
   1.104 +		echo -e "\nRestarting wifi interface..."
   1.105 +		echo "XXX"
   1.106 +		startd network.sh >/dev/null 2>&1 && sleep 2
   1.107 +		
   1.108 +		echo "XXX" && echo 90
   1.109 +		echo -e "\nChecking connection..."
   1.110 +		ip=$(ifconfig | fgrep -A 1 "encap:Ethernet" | fgrep "inet" | \
   1.111 +			cut -d ":" -f 2 | awk '{print $1}')
   1.112 +		echo "XXX"
   1.113 +		sleep 1
   1.114 +		
   1.115 +		echo "XXX" && echo 100
   1.116 +		if [ "$ip" ]; then
   1.117 +			echo -e "\nIP address: $ip"
   1.118 +		else
   1.119 +			echo -e "\nUnable to connect..."
   1.120 +		fi
   1.121 +		echo "XXX" && sleep 2
   1.122 +		
   1.123 +	} | dialog --title "{ Wi-Fi Config }" --gauge "" 8 ${width} 0
   1.124 +	
   1.125 +	#iwlist $WIFI_INTERFACE scan essid $essid last
   1.126 +	echo "CONF: $WIFI_INTERFACE $essid $passwd ${essid_crypto}"
   1.127 +}
   1.128 +
   1.129  # Main Dialog menu
   1.130  main_box() {
   1.131  	dialog \
   1.132 @@ -80,7 +202,8 @@
   1.133  		--menu "" ${height} ${width} 14 \
   1.134  "keyboard"       "$(gettext 'System keyboard setting')" \
   1.135  "locale"         "$(gettext 'System language setting')" \
   1.136 -"add-user"    "$(gettext 'Add a new user')" \
   1.137 +"wifi-setup"     "$(gettext 'Wi-Fi network settings')" \
   1.138 +"add-user"       "$(gettext 'Add a new user')" \
   1.139  "root-passwd"    "$(gettext 'Change root password')" \
   1.140  "set-date"       "$(gettext 'Set system date from the web')" \
   1.141  "quit"           "$(gettext 'Exit from SliTaz Config')" 2>${tmp}
   1.142 @@ -96,6 +219,7 @@
   1.143  	case "$action" in
   1.144  		keyboard) tazkeymap ;;
   1.145  		locale) tazlocale ;;
   1.146 +		wifi-setup) wifi_setup ;;
   1.147  		add-user) add_user ;;
   1.148  		root-passwd) root_passwd ;;
   1.149  		set-date) set_date ;;
   1.150 @@ -118,5 +242,5 @@
   1.151  esac
   1.152  
   1.153  # Clean exit
   1.154 -rm -rf ${tmpdir}
   1.155 +#rm -rf ${tmpdir}
   1.156  exit 0