netcardconfig: don't fail with bash >=4.4 when scanning for networks
[grml-network.git] / sbin / netcardconfig
index 8561dae..c9dd5a9 100755 (executable)
@@ -151,6 +151,18 @@ remauto(){
   fi
 }
 
+scanwlan(){
+  i=0
+  ifconfig $DV up
+  iwlist $DV scanning | grep "ESSID\|Quality" | sed -e "s/^.*ESSID:\"\|\"$//g" | tac > "$TMP"
+  while read line
+  do
+    WARRAY[i++]=$line
+  done < "$TMP"
+  unset i
+  rm -f "$TMP"
+}
+
 configiface() {
   [ ! -r /etc/network/interfaces ] && touch /etc/network/interfaces
   DEVICE=${NETDEVICES[$DV]}
@@ -242,23 +254,50 @@ configiface() {
       rm -f "$TMP"
     fi
 
-    $DIALOG --inputbox "$MESSAGEW4 $DEVICENAME $MESSAGEW5" 15 50 "$ESSID" 2>"$TMP" || bailout 1
-    read ESSID <"$TMP" ; rm -f "$TMP"
-    [ -z "$ESSID" ] && ESSID="any"
+    # ask to scan or manually set wireless network
+    if $DIALOG --yesno "$MESSAGEWSCAN1" 8 45 ; then
+      RESCAN=1
+
+      while [ $RESCAN -eq 1 ]
+      do
+        scanwlan
+        $DIALOG --extra-button --extra-label "Rescan" --menu "$MESSAGEWSCAN2" 20 70 15 "${WARRAY[@]}" 2>"$TMP"
+        case $? in
+          0)
+            RESCAN=0
+            ;;
+          3)
+            ;;
+          *)
+            bailout 1
+            ;;
+        esac
+      done
+
+      read ESSID <"$TMP" ; rm -f "$TMP"
+      [ -z "$MODE" ] && MODE="Managed"
+
+    else
 
-    $DIALOG --inputbox "$MESSAGEW6 $DEVICENAME $MESSAGEW7" 15 50 "$NWID" 2>"$TMP" || bailout 1
-    read NWID <"$TMP" ; rm -f "$TMP"
+      $DIALOG --inputbox "$MESSAGEW4 $DEVICENAME $MESSAGEW5" 15 50 "$ESSID" 2>"$TMP" || bailout 1
+      read ESSID <"$TMP" ; rm -f "$TMP"
+      [ -z "$ESSID" ] && ESSID="any"
 
-    $DIALOG --inputbox "$MESSAGEW8 $DEVICENAME $MESSAGEW9" 15 50 "$MODE" 2>"$TMP" || bailout 1
-    read MODE <"$TMP" ; rm -f "$TMP"
-    [ -z "$MODE" ] && MODE="Managed"
+      $DIALOG --inputbox "$MESSAGEW6 $DEVICENAME $MESSAGEW7" 15 50 "$NWID" 2>"$TMP" || bailout 1
+      read NWID <"$TMP" ; rm -f "$TMP"
 
-    $DIALOG --inputbox "$MESSAGEW10 $DEVICENAME $MESSAGEW11" 15 50 "$CHANNEL" 2>"$TMP" || bailout 1
-    read CHANNEL <"$TMP" ; rm -f "$TMP"
+      $DIALOG --inputbox "$MESSAGEW8 $DEVICENAME $MESSAGEW9" 15 50 "$MODE" 2>"$TMP" || bailout 1
+      read MODE <"$TMP" ; rm -f "$TMP"
+      [ -z "$MODE" ] && MODE="Managed"
+
+      $DIALOG --inputbox "$MESSAGEW10 $DEVICENAME $MESSAGEW11" 15 50 "$CHANNEL" 2>"$TMP" || bailout 1
+      read CHANNEL <"$TMP" ; rm -f "$TMP"
+
+      if [ -z "$CHANNEL" ]; then
+        $DIALOG --inputbox "$MESSAGEW12 $DEVICENAME $MESSAGEW13" 15 50 "$FREQ" 2>"$TMP" || bailout 1
+        read FREQ <"$TMP" ; rm -f "$TMP"
+      fi
 
-    if [ -z "$CHANNEL" ]; then
-      $DIALOG --inputbox "$MESSAGEW12 $DEVICENAME $MESSAGEW13" 15 50 "$FREQ" 2>"$TMP" || bailout 1
-      read FREQ <"$TMP" ; rm -f "$TMP"
     fi
 
     WPAON=0
@@ -327,16 +366,17 @@ configiface() {
                           }
                   }
                   ' /etc/network/interfaces)
-          while [ -z "$WPASECRET" ] ; do
+          FIRST_RUN=0  # show the wpasecret input box at least once
+          while ( [ -z "$WPASECRET" ] || [ "$FIRST_RUN" ] ) ; do
             $DIALOG --inputbox "$MESSAGEW23 $ESSID" 15 50 "$WPASECRET" 2>"$TMP" || bailout 1
             read WPASECRET <"$TMP"
             if [ -z "$WPASECRET" ] ; then
               $DIALOG --msgbox "Sorry, empty password not allowed, please retry." 0 0 || bailout 1
             fi
+            FIRST_RUN=""
           done
 
-          # make sure backslashes inside passphrase are handled correct
-          WPASECRET=$(sed -e 's/\\/\\/g' "$TMP") && rm -r "$TMP"
+          rm -f "$TMP"
 
           case $WPA_DEV in
                hostap)
@@ -399,7 +439,7 @@ configiface() {
         /gateway/{if(found){gateway=$NF}}
         /dns-nameservers/{if(found){dnsnameservers=$NF}}
         END{print address" "netmask" "broadcast" "gateway" "dnsnameservers}' /etc/network/interfaces >"$TMP"
-      read IP NM BC DG <"$TMP"
+      read IP NM BC DG NS <"$TMP"
       rm -f "$TMP"
     fi
 
@@ -486,7 +526,8 @@ MESSAGEW22="Enable WPA support?"
 MESSAGEW23="Enter the WPA passphrase (passphrase must be 8..63 characters) for"
 MESSAGEW25="Would you like to store your wep key in it's own private file ("
 MESSAGEW26=")?   If you say no, your wep key will be stored in /etc/network/interfaces and will be readable by any account on your system.  You may want to 'chmod 600 /etc/network/interfaces' if you answer no to this question"
-
+MESSAGEWSCAN1="Scan for available wireless networks?"
+MESSAGEWSCAN2="Please choose a wireless network from below:"
 
 NETDEVICESCOUNT=0
 if [ -r /proc/net/dev ] ; then
@@ -499,11 +540,11 @@ if [ -z "$WLAN" ] ; then
   WLAN_OLD=$(tail -n +3 /proc/net/wireless 2>/dev/null|awk -F: '{print $1}')
   # /proc/net/wireless does not work e.g. with iwl3945 on kernel 2.6.33 anymore
   WLAN_NEW=$(for i in /sys/class/net/* ; do ( [ -d $i/wireless ] || [ -d $i/phy80211 ] ) && basename $i ; done)
-  set -a WLAN_IWCONFIG=""
+  set -a WLAN_IWCONFIG
   for DEV in $LAN ; do
-      iwconfig $DEV 2>/dev/null 1>&2 && WLAN_IWCONFIG+="$DEV"
+    iwconfig $DEV 2>/dev/null 1>&2 && WLAN_IWCONFIG+=($DEV)
   done
-  WLAN=$(echo $WLAN_OLD $WLAN_NEW ${WLAN_IWCONFIG[*]} | sort -u)
+  WLAN=$(echo $WLAN_OLD $WLAN_NEW ${WLAN_IWCONFIG[@]} | tr ' ' '\n' | sort -u)
 fi
 
 unset LAN_DEVICES WLAN_DEVICES FIREWIRE_DEVICES NETDEVICES WLAN_IWCONFIG
@@ -529,7 +570,7 @@ while read dev mac; do
   fi
 #echo "Made to ${NETDEVICES[$NETDEVICESCOUNT]}"
   ((NETDEVICESCOUNT++))
-done < <(ifconfig -a|grep Ethernet|grep -v ^vmnet|awk '! /^\s/{print $1" "$5}')
+done < <(ip link show | awk ' !/^\s/{sub(/:/,"",$2);IFACE=$2} /link.ether/{if (IFACE !~/^vmnet/) print IFACE" "$2}')
 for dev in $LAN; do
   if [ "$(ethtool -i $dev 2>/dev/null|awk '/^bus-info:/{print $2}')" == "ieee1394" ]; then
     isauto="0"