s243a

/etc/rc.d/rc.network

Apr 27th, 2015
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 26.90 KB | None | 0 0
  1. #!/bin/sh
  2. #LGPL Barry Kauler 2006,2007 www.puppylinux.com
  3. #v2.14 rc.network now a separate process.
  4. # re-written by Dougal, July 15th 2008 (to work with modified config structure
  5. # Update:            add PRIORITY_LIST support
  6. # Update: Jul. 20th: add support for static ip with wireless
  7. # Update: Jul. 23rd: fix bugs reported by Will: testInterface and "ls ..."
  8. # Update: Jul. 25th: add support for "connect" argument, to run after boot
  9. # Update: Jul. 26th: add success/failure messages
  10. # Update: Jul. 28th: add loop for sleeping at bootup
  11. # Update: Jul. 30th: if iwlist scan finds nothing, sleep 1 and try again
  12. #                    add setting of DEBUG_OUTPUT, so it doesn't default to /dev/null
  13. # Update: Aug.  1st: move configuration files into /etc/network-wizard
  14. # Update: Aug. 15th: change "open" to "Open" in wireless profile name...
  15. #                    add "IE:" and "wpa_ie" to SCAN_LIST, for encryption type
  16. #                    add support for start/stop/restart and create stop_all
  17. # Update: Aug. 16th: include Extra: lines in SCAN_LIST, for WPA/WPA2
  18. # Update: Aug. 26th: add gxmessage to wpa_connect
  19. # Update: Sep.  1st: add firewire code
  20. # Update: Sep. 13th: update ath5k exception to use "ath5k*"
  21. # Update: Sep. 15th: disable bringing interface down/up when setting static ip
  22. #                    add "dev $INTERFACE" to route commands
  23. #                    disable exiting on successful connection -- to allow more than 1 interface
  24. # Update: Sep. 16th: replace all `` subshells with $()
  25. # Update: Sep. 20th: add GOT_ONE="yes", for dialog at end (since not exiting anymore)
  26. # Update: Sep. 24th: add killing wpa_supplicant it validation fails!
  27. #                    use updated useWpaSupplicant instead of wpa_connect
  28. #                    add killing dhcpcd if it returns error (it daemonizes even if error)
  29. #                    use "stop_all -a" to do all the cleanup at the top
  30. # Update: Sep. 25th: use the new setupDHCP for dhcp setup
  31. # Update: Oct.  1st: add reloading of bcm43xx, since it seems to not work well otherwise
  32. # Update: Oct.  5th: add definition of PATH, in case we're run by acpid etc.
  33. #                    change sleep between ifplugstatus tests to 2sec (like wiz)+ add ethtool
  34. # Update: Oct. 18th: add an extra "sleep 10" for pcmcia NICs to initialize (if they exist...)
  35. # Update: Oct. 26th: add localization (only for gui messages)
  36. # Update: Oct. 28th: move the "stop_all -a" to after the "if [ "$ACTION" = "restart" ]"
  37. # Update: Oct. 31st: check for locale files both of type "ab_CD" and "ab"
  38. #                    move the route table flushing into stop_all
  39. # Update: Nov.  7th: add deleting old backups of resolve.conf
  40. # Update: Nov. 28th: remove bashism
  41. # Update: Dec. 10th: add "sleep 1" at the beginning of testInterface (rerwin's fix)
  42. # Update: Feb. 8th '09: implement suggestion from OldTxPup: ifconfig down/up after scanning
  43. #                    in stop_all, skip interfaces with wake-on-lan enabled
  44. #                    move iwlist code into a function, run_iw_scan, and add ejecting pcmcia
  45. # Update: Feb. 22nd: change the backing up of resolve.conf so it only saves as resolv.conf.old
  46. # Update: Mar. 19th: change to using tmp files for wireless scans (try and get max networks)
  47. # Update: Mar. 26th: add 5 second sleep between wireless scans for pcmcia cards
  48. # Update: Mar. 29th: move (pcmcia) 5 second sleep to start if run_iw_scan
  49. # Update: Apr.  1st: change pcmcia sleep detection to module name being *_cs...
  50. # Update: Apr.  2nd: fix finding profiles in prism case, add checkIsPCMCIA
  51. #091108 BK old ifplugstatus renamed to ifplugstatus-0.18.
  52. #
  53. # 29nov09: rewrite eth0 mac address by shinobar
  54. #  9feb10: avoid removing exist HARDWARE.conf, reset interface before rewriting
  55. # 20feb10: old and new ifplugstatus, moved rewriting eth0 mac address
  56. # 25feb10: fix was success message even setupDHCP failed, exit if one interface successed
  57. #  7mar10: stop even wake-on-lan enabled
  58.  
  59. ######### TODO ###########
  60. #- need to find out about static ip... can we check somehow (arp)? maybe use
  61. #  dhcpcd if it fails? (do we know if it fails?)
  62. #- let user prioritise networks/interfaces?
  63. #- might want to add something tho check if interface is connected already
  64. # (for running with the "restart" option)
  65.  
  66. # Add this to support standard arguments, while being compatible with Puppy
  67. case $1 in
  68.   stop) ACTION="stop" ;;
  69.   connect|restart) ACTION="restart" ;;
  70.   start|*) ACTION="start" ;; # also supports no args
  71. esac
  72.  
  73. # Add path, in case it is run by acpid or such
  74. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
  75.  
  76. # basic configuration info for interface
  77. # named $HWADDRESS.conf (assuming the HWaddress is more unique than interface name...)
  78. # mainly intended to know if interface has been "configured"...
  79. NETWORK_INTERFACES_DIR='/etc/network-wizard/network/interfaces'
  80. DEBUG_OUTPUT=/dev/stderr
  81.  
  82. # Dougal: add localization
  83. mo=net-setup.mo
  84. #lng=${LANG%.*}
  85. # always start by sourceing the English version (to fill in gaps)
  86. . "/usr/share/locale/en/LC_MESSAGES/$mo"
  87. if [ -f "/usr/share/locale/${LANG%.*}/LC_MESSAGES/$mo" ];then
  88.   . "/usr/share/locale/${LANG%.*}/LC_MESSAGES/$mo"
  89. elif [ -f "/usr/share/locale/${LANG%_*}/LC_MESSAGES/$mo" ];then
  90.   . "/usr/share/locale/${LANG%_*}/LC_MESSAGES/$mo"
  91. fi
  92.  
  93. #  Dougal: want to use functions from wag-profiles (cleanUpInterface...)
  94. #+ so source it
  95. . /usr/sbin/wag-profiles.sh
  96.  
  97. # A function to stop everything (at shutdown, for example)
  98. # Enable accepting a param to pass to ifconfig (i.e. -a)
  99. stop_all(){
  100.     for INTERFACE in $(ifconfig $1 | grep -F 'Link encap:Ethernet' | cut -f 1 -d " ") ; do
  101.         # Skip it wake-on-lan enabled # 7mar10 stop even wake-on-lan enabled
  102.         #ethtool $INTERFACE | grep -Fq 'Wake-on: g' && continue
  103.         # clean up (kills dhcpcd, wpa_supplicant etc.)
  104.         cleanUpInterface "$INTERFACE" >/dev/null 2>&1
  105.         ip route flush dev "$INTERFACE"
  106.         ifconfig "$INTERFACE" down
  107.     done
  108. }
  109.  
  110. # Dougal: add option to run with "stop" at shutdown...
  111. # (will enable to add things in the future)
  112. if [ "$ACTION" = "stop" ] ; then
  113.   stop_all
  114.   exit
  115. fi
  116.  
  117. # A couple of dialog messages to notify of sucess or failure
  118. success_message(){
  119.     if [ "$HAVEX" = "yes" ]; then
  120.       gxmessage -center -font "sans 16" -geometry 150x100 -borderless -timeout 1 -bg "green" -buttons "" -title "$L_TITLE_Success" "
  121.       $L_MESSAGE_Success!   " &
  122.     else
  123.       echo "$L_MESSAGE_Success!"
  124.     fi
  125. }
  126.    
  127. failure_message(){
  128.     if [ "$HAVEX" = "yes" ]; then
  129.       gxmessage -center -font "sans 16" -geometry 150x100 -borderless -timeout 1 -bg "red" -buttons "" -title "$L_TITLE_Failure" "
  130.         $L_MESSGAE_Failed.   " &
  131.     else
  132.       echo "$L_MESSGAE_Failed..."
  133.     fi
  134. }
  135.  
  136. # cutdown version of this function (can't just source net-setup.sh...)
  137. testInterface()
  138. {
  139.   #INTERFACE="$1"
  140.   echo -n "cheching if interface $INTERFACE is alive..."
  141.   #sleep 1 # rerwin's original solution
  142.   # no, move the sleep 1 so is only used for wireless
  143.   #091108 old ifplugstatus now -0.18, latest is just 'ifplugstatus'...
  144.   IFPLUGOLD=ifplugstatus
  145.   IFPLUGNEW=ifplugstatus
  146.   [ -x /sbin/ifplugstatus-0.18 ] && IFPLUGOLD=ifplugstatus-0.18
  147.   [ -x /sbin/ifplugstatus-0.25 ] && IFPLUGNEW=ifplugstatus-0.25
  148.   if ! $IFPLUGOLD "$INTERFACE" | grep -F -q 'link beat detected' ;then
  149.     sleep 1
  150.     if ! $IFPLUGNEW "$INTERFACE" | grep -F -q 'link beat detected' ;then
  151.       sleep 2
  152.       if ! $IFPLUGOLD "$INTERFACE" | grep -F -q 'link beat detected' ;then
  153.         sleep 2
  154.         if ! $IFPLUGNEW "$INTERFACE" | grep -F -q 'link beat detected' ;then
  155.           # add ethtool test, just in case it helps at times...
  156.           sleep 1
  157.           if ! ethtool "$INTERFACE" | grep -Fq 'Link detected: yes' ; then
  158.             echo "no"
  159.             return 1
  160.           fi
  161.         fi
  162.       fi
  163.     fi
  164.   fi
  165.   echo "yes"
  166.   return 0
  167. } # end of testInterface
  168.  
  169. # cutdown version of this function (can't just source net-setup.sh...)
  170. # config file will include IP_ADDRESS, NETMASK, DNS_SERVER[12], GATEWAY
  171. setupStaticIP()
  172. {
  173.     BROADCAST=$(ipcalc -b "$IP_ADDRESS" "$NETMASK" | cut -d= -f2)
  174.     echo "configuring interface $INTERFACE with static ip..."
  175.     #ifconfig "$INTERFACE" down
  176.    
  177.     # do the work
  178.     ifconfig "$INTERFACE" "$IP_ADDRESS" netmask "$NETMASK" broadcast "$BROADCAST" #up
  179.     if [ $? -eq 0 ];then
  180.         # Configure a nameserver, if we're supposed to.
  181.         # This now replaces any existing resolv.conf, which
  182.         # we will try to back up.
  183.         if [ "$DNS_SERVER1" != "0.0.0.0" ] ; then
  184.             # remove old backups
  185.             rm /etc/resolv.conf.[0-9][0-9]* 2>/dev/null
  186.             # backup previous one
  187.             mv -f /etc/resolv.conf /etc/resolv.conf.old
  188.             echo "nameserver $DNS_SERVER1" > /etc/resolv.conf
  189.             if [ "$DNS_SERVER2" != "0.0.0.0" ] ; then
  190.                 echo "nameserver $DNS_SERVER2" >> /etc/resolv.conf
  191.             fi
  192.         fi
  193.    
  194.         # add default route, if we're supposed to
  195.         if [ "$GATEWAY" ] ; then
  196.             route add -net default gw "$GATEWAY" #dev $INTERFACE
  197.             if [ $? -eq 0 ];then #0=ok.
  198.                 echo "Default route set through $GATEWAY."
  199.             else
  200.                 echo -e "Could not set default route through $GATEWAY.  Please try again.\nNote that Puppy has tried to do this:\nroute add -net default gw $GATEWAY" #dev $INTERFACE"
  201.                 #ifconfig "$INTERFACE" down
  202.                 return 1
  203.             fi
  204.         fi
  205.        
  206.         return 0
  207.     else
  208.         echo -e "Interface configuration failed; please try again.\nWhat Puppy has just tried to do is this:\nifconfig $INTERFACE $IP_ADDRESS netmask $NETMASK broadcast $BROADCAST up"
  209.         #ifconfig "$INTERFACE" down
  210.         return 1
  211.     fi
  212. } #end of setupStaticIP
  213.  
  214. #  Dougal: stick the wireless scan into a function, since we're doing some tricks
  215. #+ to try and ensure it works...
  216. run_iw_scan(){
  217.     # add sleeping a bit for pcmcia...
  218.     #grep -q "^pcmcia:" /sys/class/net/$INTERFACE/device/modalias &&    sleep 5
  219.     #case $INTMODULE in *_cs) sleep 5 ;; esac
  220.     checkIsPCMCIA
  221.     [ -n "$IsPCMCIA" ] && sleep 5
  222.     # no try runing twice and redirecting to files, then use bigger one...
  223.     # clean up any old files
  224.     rm /tmp/iwscan*.tmp 2>/dev/null
  225.     iwlist "$INTERFACE" scan >/tmp/iwscan1.tmp 2>&1
  226.     if [ -n "$IsPCMCIA" ] ; then
  227.       sleep 1
  228.     else
  229.       usleep 100000
  230.     fi
  231.     iwlist "$INTERFACE" scan >/tmp/iwscan2.tmp 2>&1
  232.     ScanListFile=$(du -b /tmp/iwscan*.tmp |sort -n | tail -n1 |cut -f2)
  233.     # now try scanning
  234.     #SCAN_LIST=$(iwlist "$INTERFACE" scan | grep 'Cell\|ESSID\|Mode\|Frequency\|Encryption\|Channel')
  235.     #SCAN_LIST=$(grep 'Cell\|ESSID\|Mode\|Frequency\|Encryption\|Channel' $ScanListFile)
  236.     #rm /tmp/iwscan*.tmp
  237.     ## no, use the file all the time, so just get the cell list
  238.     CELL_LIST=$(grep -Eo "Cell [0-9]+" $ScanListFile | cut -f2 -d " ")
  239.     # if it failed, try sleeping and trying again
  240.     #if [ -z "$SCAN_LIST" ] ; then
  241.     if [ -z "$CELL_LIST" ] ; then
  242.         # with pcmcia try ejecting and inserting (this is done in wag-profiles.sh)
  243.         if [ -n "$IsPCMCIA" ] ; then
  244.             pccardctl eject
  245.             pccardctl insert
  246.             sleep 1
  247.         fi
  248.         sleep 1
  249.         #SCAN_LIST=$(iwlist "$INTERFACE" scan | grep 'Cell\|ESSID\|Mode\|Frequency\|Encryption\|Channel')
  250.         iwlist "$INTERFACE" scan >/tmp/iwscan3.tmp 2>&1
  251.         ScanListFile="/tmp/iwscan3.tmp"
  252.         CELL_LIST=$(grep -Eo "Cell [0-9]+" $ScanListFile | cut -f2 -d " ")
  253.     fi
  254. }
  255.  
  256. # function like assignProfileData, but for use with CELL_* for open networks
  257. cellToProfile(){
  258.     # assign to PROFILE_ names...
  259.     PROFILE_ESSID="$CELL_ESSID"
  260.     PROFILE_FREQ="$CELL_FREQ"
  261.     PROFILE_CHANNEL="$CELL_CHANNEL"
  262.     PROFILE_AP_MAC="$CELL_AP_MAC"
  263.     PROFILE_MODE="$CELL_MODE"
  264.     PROFILE_NWID=""
  265.     PROFILE_KEY=""
  266. } # cellToProfile
  267.  
  268. # function to get ip address, via static ip or dhcp
  269. try_and_connect(){
  270.     local RETURN=1
  271.     if [ "$STATIC_IP" = "yes" ] ; then # static ip address
  272.       if setupStaticIP ; then
  273.         # need to quit, probably
  274.         RETURN=0
  275.       fi
  276.     else
  277.       setupDHCP #|| killDhcpcd "$INTERFACE" # 25feb10
  278.       RETURN=$?
  279.     fi
  280.     return $RETURN
  281. }
  282.  
  283. # 29nov by shinobar # 9feb10 do not remove $HWADDRESS.conf
  284. rewrite_mac_address () {
  285.   cat /etc/NETWORKING 2>/dev/null | grep -q -i 'yes' || return
  286.   INTERFACES=/etc/network-wizard/network/interfaces
  287.   HWADDRESS=$(LANG=C ifconfig eth0 | head -n 1 | tr -s ' ' | cut -d' ' -f5)
  288.   [ "$HWADDRESS" != "" ] || return
  289.      TMPFILE=/tmp/$(basename $0)_conf.tmp
  290.      rm -f "$TMPFILE"
  291.      IFCONFIG=$(ifconfig)
  292.      CONFS=$(find "$INTERFACES" -follow -type f -name '*.conf' -printf "%P ")
  293.      if [ "$CONFS" != "" ]; then
  294.        for F in $CONFS; do
  295.          echo "$IFCONFIG"|grep -q "$(basename $F .conf)" && continue
  296.          [ ! -f "$TMPFILE" ] && \
  297.            grep -q '^[[:blank:]]*STATIC_IP=.*yes' "$INTERFACES/$F" && \
  298.            cp "$INTERFACES/$F" "$TMPFILE"
  299.          #rm -f "$INTERFACES/$F"
  300.        done
  301.      fi
  302.      [ -f "$TMPFILE" ] && mv -f "$TMPFILE" "$INTERFACES/$HWADDRESS.conf" || \
  303.        echo "IS_WIRELESS=''" > "$INTERFACES/$HWADDRESS.conf"
  304. }
  305.  
  306. # Dougal: when running after boot, we don't need this
  307. if [ "$ACTION" = "restart" ] ; then # connect
  308.   pidof X >/dev/null && HAVEX="yes"
  309.   exec 1>/tmp/network-connect.log 2>&1
  310. else # below only done at boot
  311.   ifconfig lo 127.0.0.1
  312.   route add -net 127.0.0.0 netmask 255.0.0.0 lo
  313.   #rewrite_mac_address  # 9feb10 moved
  314.   # Dougal: add reloading of bcm43xx
  315.   if grep -Fq bcm43xx /proc/modules ; then
  316.     rmmod bcm43xx
  317.     sleep 1
  318.     modprobe bcm43xx
  319.     sleep 1
  320.   fi
  321.   # Wait a bit, so rc.local and profile finish (subtract 1 from MAXWAIT)
  322.   #sleep 1
  323.   #/etc/ethernetmodules has extra modules to load,
  324.   if [ -s /etc/ethernetmodules ];then #-s tests file not zero size.
  325.     echo "Loading extra ethernet driver modules..."
  326.     #each line of ethernetmodules has a module plus optional params...
  327.     # Dougal: use read to get both
  328.     {
  329.         read EMODULE
  330.         read FMODULE
  331.     }</etc/ethernetmodules
  332.     echo "loading $EMODULE module"
  333.     modprobe $EMODULE
  334.     if [ "$EMODULE" != "$FMODULE" ];then
  335.       echo "loading $FMODULE module"
  336.       modprobe $FMODULE
  337.     fi
  338.   fi
  339.   stop_all -a >/dev/null 2>&1   # 9feb10: reset before sense
  340.    # wait up to MAXWAIT seconds, in case some firmware is slow to load
  341.   MAXWAIT=12
  342.   # a list of all the interface config files
  343.   echo -n "Waiting for interfaces to initialize..."
  344.   CONFIGURED=$(cd $NETWORK_INTERFACES_DIR ; ls -1 *.conf 2>/dev/null |cut -d '.' -f1)
  345.   for I in $(seq 1 $MAXWAIT) ; do
  346.     EXIST=$(ifconfig -a | grep -F 'Link encap:')
  347.     for ONE in $CONFIGURED ; do
  348.       # see if it is detected
  349.       case $EXIST in
  350.         *$ONE*) ;; # ok
  351.         *) sleep 1 ; echo -n '.' ; continue 2 ;; # go to the next I
  352.       esac
  353.     done
  354.     # we only get here if all the configured interfaces are found...
  355.     break
  356.   done
  357.   # "close" the line started before the loop
  358.   echo
  359.   # add an extra wait for pcmcia NICs to initialize
  360.   #case "$(cat /sys/class/net/*/device/modalias)" in *pcmcia:*)
  361.   #  echo "pcmcia NIC detected, sleeping an extra 10 seconds"
  362.   #  sleep 10
  363.   #  ;;
  364.   #esac
  365. fi # if [ "$ACTION" = "restart" ] ; then
  366.  
  367. #  It appears that "cleaning up" an interface, while another one is already
  368. #+ configured with dhcpcd running, somehow messes up the ok interface...
  369. #+ so do all the cleaning at the beginning (note the -a for ifconfig)
  370. #  (this replaces the cleanUpInterface at the start of the big loop)
  371.  
  372. rewrite_mac_address  # 20feb10 moved here
  373. stop_all -a >/dev/null 2>&1
  374.  
  375. GOT_ONE=""
  376. # Dougal: read everything, so we get HWaddress, too
  377. while read INTERFACE A B C HWADDRESS
  378. do
  379.   if [ "$B" = "encap:UNSPEC" ] ; then
  380.     # check if it's firewire...
  381.     [ "$(cat /sys/class/net/$INTERFACE/type)" = "24" ] || continue
  382.   elif [ "$B" != "encap:Ethernet" ] ; then
  383.     continue
  384.   fi
  385.   echo "configuring network interface $INTERFACE"
  386.   if [ "$C" != "HWaddr" -o -z "$HWADDRESS" ] ; then
  387.     echo "interface $INTERFACE does not seem to have a HWaddress... skipping"
  388.     continue
  389.   fi
  390.   # see if already "configured"
  391.   if [ ! -f "${NETWORK_INTERFACES_DIR}/$HWADDRESS.conf" ] ; then
  392.     echo "interface $INTERFACE has not been configured... skipping"
  393.     continue
  394.   fi
  395.   # clean up, skip if fails (=ifconfig up failed)
  396.   ## cleanup moved to top, just raise here.
  397.   if ! ifconfig "$INTERFACE" up ; then
  398.     echo "failed to raise interface $INTERFACE... skipping"
  399.     continue
  400.   fi
  401.  
  402.   # unset static ip variables
  403.   unset STATIC_IP IP_ADDRESS NETMASK DNS_SERVER1 DNS_SERVER2 GATEWAY
  404.  
  405.   # source config
  406.   . "${NETWORK_INTERFACES_DIR}/$HWADDRESS.conf"
  407.  
  408.   # see if it's wireless
  409.   if [ -f "${WLAN_INTERFACES_DIR}/$HWADDRESS.conf" ] ; then
  410.     OPEN_NETWORKS=""
  411.     USE_WLAN_NG=""
  412.     # we need the driver name, in case it's ath5k
  413.     INTMODULE=$(readlink /sys/class/net/$INTERFACE/device/driver)
  414.     INTMODULE=${INTMODULE##*/}
  415.     . "${WLAN_INTERFACES_DIR}/$HWADDRESS.conf"
  416.     # this should tell us if to use wlan_ng etc.
  417.     if [ "$USE_WLAN_NG" = "yes" ] ; then ####### use wlanctl-ng ########
  418.       if runPrismScan "$INTERFACE" ; then #>/dev/null
  419.         # go over access points (POINTNUM set in function)
  420.         for P in $(seq 0 $POINTNUM) ; do
  421.           grep -Fq 'resultcode=success' /tmp/prism-scan$P || continue
  422.           getPrismCellParameters $P
  423.           # we should have CELL_ESSID, CELL_CHANNEL,
  424.           # CELL_AP_MAC, CELL_MODE, CELL_ENCRYPTION
  425.           # see if access point configured
  426.           #### need to see about the ecryption part: gives "true" or "no_value"
  427.           case $CELL_ENCRYPTION in
  428.             true) CELL_ENCRYPTION=WEP ;;
  429.             *) CELL_ENCRYPTION=Open ;;
  430.           esac
  431.           if [ -f "${PROFILES_DIR}/${CELL_AP_MAC}.${CELL_ENCRYPTION}.conf" ] ; then
  432.             # unset static ip variables
  433.             unset STATIC_IP IP_ADDRESS NETMASK DNS_SERVER1 DNS_SERVER2 GATEWAY
  434.             # source it
  435.             . "${PROFILES_DIR}/${CELL_AP_MAC}.${CELL_ENCRYPTION}.conf"
  436.             assignProfileData
  437.             # try connection
  438.             if useWlanctl && sleep 1 && testInterface ; then
  439.               # sleep a bit, just in case (net-setup.sh has 5 seconds!)
  440.               #sleep 1
  441.               # try and get ip address
  442.               if try_and_connect ; then
  443.                 # done! stop everything.
  444.                 rm /tmp/wlan-up /tmp/prism-* >/dev/null 2>&1
  445.                 success_message
  446.                 #exit
  447.                 # move to next interface (to allow more than one)
  448.                 GOT_ONE="yes"
  449.                 continue 2
  450.               else
  451.                 failure_message
  452.               fi
  453.             elif [ "$CELL_ENCRYPTION" = "Open" ] ; then # add to list of open networks
  454.               OPEN_NETWORKS="$OPEN_NETWORKS $P"
  455.             else # failure of useWlanctl
  456.               failure_message
  457.             fi #if useWlanctl &&
  458.           fi #if [ -f "${PROFILES_DIR}/${CELL_AP_MAC}.${CELL_ENCRYPTION}.conf" ] ; then
  459.         done # for P in
  460.         # if we got here, we are not connected yet, look at open networks
  461.         if [ -n "$OPEN_NETWORKS" ] ; then
  462.           WLAN_SECURE="opensystem"
  463.           PROFILE_KEY=""
  464.           # unset static ip variables
  465.           unset STATIC_IP IP_ADDRESS NETMASK DNS_SERVER1 DNS_SERVER2 GATEWAY
  466.           for P in $OPEN_NETWORKS ; do
  467.             grep -Fq 'resultcode=success' /tmp/prism-scan$P || continue
  468.             getPrismCellParameters $P
  469.             # set the PROFILE_ variables
  470.             PROFILE_ESSID="$CELL_ESSID"
  471.             if useWlanctl && sleep 1 && testInterface ; then
  472.               # sleep a bit, just in case (net-setup.sh has 5 seconds!)
  473.               #sleep 1
  474.               # try and get ip address
  475.               if try_and_connect ; then
  476.                 # done! stop everything.
  477.                 rm /tmp/wlan-up /tmp/prism-* >/dev/null 2>&1
  478.                 success_message
  479.                 #exit
  480.                 # move to next interface (to allow more than one)
  481.                 GOT_ONE="yes"
  482.                 continue 2
  483.               else
  484.                 failure_message
  485.               fi
  486.             else # failure of useWlanctl
  487.               failure_message
  488.             fi
  489.           done
  490.         fi
  491.         rm /tmp/wlan-up /tmp/prism-* >/dev/null 2>&1
  492.       else
  493.         echo "wireless scan with wlanctl-ng failed... skipping interface"
  494.         # clean up (kills dhcpcd etc)
  495.         cleanUpInterface "$INTERFACE"
  496.         ifconfig "$INTERFACE" down
  497.         # go to next interface
  498.         continue
  499.       fi # if runPrismScan
  500.    
  501.     else #################### use the usual iwconfig ####################
  502.       # scan for available networks
  503.       run_iw_scan
  504.       #if [ -n "$SCAN_LIST" ]; then
  505.       if [ -n "$CELL_LIST" ]; then
  506.         #  Dougal: something odd. seems like (at least with some drivers), we
  507.         #+ need to "refresh" after scanning...
  508.         ifconfig "$INTERFACE" down
  509.         ifconfig "$INTERFACE" up
  510.         #CELL_LIST=$(echo "$SCAN_LIST" | grep -Eo "Cell [0-9]+" | cut -f2 -d " ")
  511.         # let the user create a priority list of which networks to use first
  512.         # (will be included in wireless config file, contains mac addresses)
  513.         if [ "$PRIORITY_LIST" ] ; then
  514.           PRIOR_CELLS=""
  515.           for ONE in $PRIORITY_LIST ; do
  516.             # see if it was found, then move cell number to start...
  517.             #ACELL=$(echo "$SCAN_LIST" | grep -F "Address: $ONE" | awk '{print $2}')
  518.             ACELL=$(grep -F "Address: $ONE" $ScanListFile | awk '{print $2}')
  519.             if [ "$ACELL" ] ; then
  520.               PRIOR_CELLS="$PRIOR_CELLS $ACELL"
  521.               # remove from cell list (BASHISM!)
  522.               #CELL_LIST=${CELL_LIST/$ACELL/}
  523.               CELL_LIST=$(echo "$CELL_LIST" | sed "s/$ACELL//")
  524.             fi
  525.           done
  526.           CELL_LIST="$PRIOR_CELLS $CELL_LIST"
  527.         fi
  528.         for CELL in $CELL_LIST ; do
  529.           Get_Cell_Parameters $CELL
  530.           # we should have at least some of:
  531.           # CELL_ESSID, CELL_FREQ, CELL_CHANNEL, CELL_QUALITY,
  532.           # CELL_AP_MAC, CELL_MODE, CELL_ENCRYPTION
  533.           # see if it's open or not
  534.           if [ "$CELL_ENCRYPTION" = "on" ] ; then
  535.             # get only encrypted profiles (might have problems with globbing?)
  536.             for PROF in $(ls ${PROFILES_DIR}/${CELL_AP_MAC}.W[EP][PA]*.conf 2>/dev/null) ; do
  537.               SUCCESS=""
  538.               # unset static ip variables
  539.               unset STATIC_IP IP_ADDRESS NETMASK DNS_SERVER1 DNS_SERVER2 GATEWAY
  540.               # source it
  541.               . "$PROF"
  542.               # assign to PROFILE_ names...
  543.               assignProfileData
  544.               case $PROFILE_ENCRYPTION in
  545.                 WPA|WPA2)
  546.                   WPA_CONF="${WPA_SUPP_DIR}/${PROFILE_AP_MAC}.${PROFILE_ENCRYPTION}.conf"
  547.                   useWpaSupplicant "$WPA_CONF" && SUCCESS="yes"
  548.                   ;;
  549.                 WEP)
  550.                   useIwconfig && SUCCESS="yes"
  551.                   ;;
  552.               esac
  553.               if [ "$SUCCESS" = "yes" ] && sleep 1 && testInterface ; then
  554.                 # sleep a bit, just in case (net-setup.sh has 5 seconds!)
  555.                 #sleep 1
  556.                 # try and get ip address
  557.                 if try_and_connect ; then
  558.                   # done! stop everything.
  559.                   success_message
  560.                   # clean scan files
  561.                   rm /tmp/iwscan*.tmp 2>/dev/null
  562.                   #exit
  563.                   # move to next interface (to allow more than one)
  564.                   GOT_ONE="yes"
  565.                   continue 3
  566.                 else
  567.                   failure_message
  568.                 fi
  569.               else # iwconfig/wpa_supp failed
  570.                 failure_message
  571.               fi
  572.             done # for PROF in
  573.           else # encryption off
  574.             # see if it was configured
  575.             if [ -f "${PROFILES_DIR}/${CELL_AP_MAC}.Open.conf" ] ; then
  576.               # unset static ip variables
  577.               unset STATIC_IP IP_ADDRESS NETMASK DNS_SERVER1 DNS_SERVER2 GATEWAY
  578.               # source it
  579.               . "${PROFILES_DIR}/${CELL_AP_MAC}.Open.conf"
  580.               # assign to PROFILE_ names...
  581.               assignProfileData
  582.               # try connection
  583.               if useIwconfig && sleep 1 && testInterface ; then
  584.                 # sleep a bit, just in case (net-setup.sh has 5 seconds!)
  585.                 #sleep 1
  586.                 # try and get ip address
  587.                 if try_and_connect ; then
  588.                   # done! stop everything.
  589.                   success_message
  590.                   # clean scan files
  591.                   rm /tmp/iwscan*.tmp 2>/dev/null
  592.                   #exit
  593.                   # move to next interface (to allow more than one)
  594.                   GOT_ONE="yes"
  595.                   continue 2
  596.                 else
  597.                   failure_message
  598.                 fi
  599.               else
  600.                 failure_message
  601.               fi
  602.             else # add to list of open networks
  603.               OPEN_NETWORKS="$OPEN_NETWORKS $CELL"
  604.             fi
  605.           fi # if [ "$CELL_ENCRYPTION" = "on" ] ; then  
  606.         done # for CELL in
  607.         # if we got here, we are not connected yet, look at open networks
  608.         if [ "$OPEN_NETWORKS" ] ; then
  609.           # unset static ip variables
  610.           unset STATIC_IP IP_ADDRESS NETMASK DNS_SERVER1 DNS_SERVER2 GATEWAY
  611.           for CELL in $OPEN_NETWORKS ; do
  612.             Get_Cell_Parameters $CELL
  613.             # need to convert to PROFILE_...
  614.             cellToProfile
  615.             if useIwconfig && sleep 1 && testInterface ; then
  616.               # sleep a bit, just in case (net-setup.sh has 5 seconds!)
  617.               #sleep 1
  618.               # try and get ip address
  619.               if try_and_connect ; then
  620.                 success_message
  621.                 # clean scan files
  622.                 rm /tmp/iwscan*.tmp 2>/dev/null
  623.                 #exit
  624.                 # move to next interface (to allow more than one)
  625.                 GOT_ONE="yes"
  626.                 continue 2
  627.               else
  628.                 failure_message
  629.               fi
  630.             else
  631.               failure_message
  632.             fi
  633.           done #for CELL in $OPEN_NETWORKS
  634.         fi
  635.       else # if [ -n "$CELL_LIST" ]; then
  636.         echo "wireless scan with iwlist failed (no networks found)... skipping interface"
  637.         # clean up (kills dhcpcd etc)
  638.         cleanUpInterface "$INTERFACE"
  639.         ifconfig "$INTERFACE" down
  640.         # clean scan files
  641.         rm /tmp/iwscan*.tmp 2>/dev/null
  642.         # go to next interface
  643.         continue
  644.       fi
  645.     fi
  646.     # if we got here, config file exists, but nothing worked
  647.     echo "failed to configure wireless interface $INTERFACE... skipping"
  648.     ifconfig "$INTERFACE" down
  649.     continue
  650.   fi # if [ -f "${WLAN_INTERFACES_DIR}/$HWADDRESS.conf" ] ; then
  651.   # if we got here, it's probably not wireless
  652.   # try and get ip address
  653.   if testInterface ; then
  654.     # sleep a bit, just in case (net-setup.sh has 5 seconds!)
  655.     #sleep 1
  656.     # try and get ip address
  657.     if try_and_connect ; then
  658.       success_message
  659.       exit  # 25feb10
  660.       # move to next interface (to allow more than one)
  661.       GOT_ONE="yes"
  662.       continue
  663.     else
  664.       failure_message
  665.     fi
  666.   else
  667.     echo "interface $INTERFACE does not seem to be plugged in... skipping"
  668.     ifconfig "$INTERFACE" down
  669.     continue
  670.   fi
  671.   # if we got here, nothing worked, clean up
  672.   # clean up (kills dhcpcd etc)
  673.   cleanUpInterface "$INTERFACE"
  674.   ifconfig "$INTERFACE" down
  675. done<<EOF
  676. $( ifconfig -a | grep -F 'Link encap:' )
  677. EOF
  678.  
  679. #  if we got here and have x, it means we're running with the "connect"
  680. #+ argument and couldn't connect -- maybe no interfaces have been configured yet
  681. if [ "$HAVEX" = "yes" ] && [ "$GOT_ONE" != "yes" ] ; then
  682.   gxmessage -center -default "okay" -bg "orange" -title "$L_TITLE_Network_Wizard" "$L_MESSAGE_Failed_To_Connect"
  683. fi
  684.  
  685. exit
Advertisement
Add Comment
Please, Sign In to add comment