home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / linux / puppy-barebones-2.01r2.iso / pup_201.sfs / usr / sbin / net-setup.sh < prev    next >
Encoding:
Linux/UNIX/POSIX Shell Script  |  2006-04-13  |  29.0 KB  |  952 lines

  1. #!/bin/sh
  2. #(c) copyright Barry Kauler 2004 www.puppylinux.org
  3. #Puppy ethernet network setup script.
  4. #I got some bits of code from:
  5. # trivial-net-setup, by Seth David Schoen <schoen@linuxcare.com> (c)2002
  6. # and the little executables and, or, dotquad from the
  7. # lnx-bbx Linux project. ipcalc app from Redhat 7.
  8. # Thanks to GuestToo and other guys on the Puppy Forum for bug finding.
  9. # Rarsa (Raul Suarez) reorganized the code and cleaned up the user interface
  10.  
  11. CURDIR="`pwd`"
  12. APPDIR=`dirname "$0"`
  13. cd "${APPDIR}"
  14. APPDIR="`pwd`"
  15. cd "${CURDIR}"
  16.  
  17.  
  18. #================================================================================
  19. #============= FUNCTIONS USED IN THE SCRIPT ==============
  20. #================================================================================
  21. . ${APPDIR}/wag-profiles.sh
  22.  
  23. showMainWindow()
  24. {
  25.     while [ "$MAIN_RESPONSE" != "19" ] && [ "$MAIN_RESPONSE" != "abort" ]
  26.     do
  27.  
  28.         buildMainWindow
  29.  
  30.         I=$IFS; IFS=""
  31.         for STATEMENTS in  $(gtkdialog --program Puppy_Network_Setup); do
  32.             eval $STATEMENTS
  33.         done
  34.         IFS=$I
  35.  
  36.         MAIN_RESPONSE=${EXIT}
  37.  
  38.         INTERFACE=`echo $EXIT | grep "Interface_" | cut -d_ -f 2`
  39.  
  40.         [ ! -z ${INTERFACE} ] &&    MAIN_RESPONSE=13
  41.  
  42.         case $MAIN_RESPONSE in
  43.             10) showLoadModuleWindow ;;
  44.             17) saveNewModule ;;
  45.             18) unloadNewModule ;;
  46.             13) showConfigureInterfaceWindow ${INTERFACE} ;;
  47.             21) showHelp  ;;
  48.         esac
  49.  
  50.     done
  51.  
  52. } # end of showMainWindow
  53.  
  54.  
  55. #================================================================================
  56. refreshMainWindowInfo ()
  57. {
  58.     findLoadedModules
  59.  
  60.   #we need to know what ethernet interfaces are there...
  61.   INTERFACE_NUM=`ifconfig -a | grep "Link encap:Ethernet" | wc -l | awk '{print $1}'`
  62.   INTERFACES="`ifconfig -a | grep "Link encap:Ethernet" | cut -f 1 -d " " | tr "\n" " "`"
  63.   INTERFACEBUTTONS=""
  64.  
  65.   for INTERFACE in ${INTERFACES}
  66.   do
  67.     INTERFACEBUTTONS="
  68.             ${INTERFACEBUTTONS}
  69.             <button>
  70.                 <label>${INTERFACE}</label>
  71.                 <action>EXIT=Interface_${INTERFACE}</action>
  72.             </button>"
  73.   done
  74.  
  75.   LOADEDETH=`cat /tmp/loadedeth.txt | tr "\n" " "`
  76.  
  77.   if [ $INTERFACE_NUM -eq 0 ];then
  78.     echo "Puppy does not see any active ethernet interfaces.
  79.  
  80. If you have one or more ethernet cards (interfaces) in the PC and you want
  81. to use them, then driver modules will have to be loaded. This is supposed to be
  82. autodetected and the correct driver loaded when Puppy boots up, but it hasn't
  83. happened in this case. Never mind, you can do it manually!" > /tmp/net-setup_MSGINTERFACES.txt
  84.  
  85.   fi
  86.   if [ $INTERFACE_NUM -eq 1 ];then
  87.     echo "Puppy sees $INTERFACE_NUM active ethernet interface, that is, actually responding
  88. even if it is not yet doing what you want!.
  89. To test or configure the interface, click on the corresponding
  90. interface button."  > /tmp/net-setup_MSGINTERFACES.txt
  91.   fi
  92.   if [ $INTERFACE_NUM -gt 1 ];then
  93.     echo "Puppy sees $INTERFACE_NUM active ethernet interfaces , that is, actually responding
  94. even if they are not yet doing what you want!.
  95. To test or configure an interface, click on the corresponding interface button."  > /tmp/net-setup_MSGINTERFACES.txt
  96.   fi
  97.  
  98.     echo "Puppy has done a quick check to see what ethernet driver modules are currently
  99. loaded. Here they are:
  100.  ${LOADEDETH}" > /tmp/net-setup_MSGMODULES.txt
  101.  
  102. } # end refreshMainWindowInfo
  103.  
  104. #================================================================================
  105. buildMainWindow ()
  106. {
  107.     echo "${TOPMSG}" > /tmp/net-setup_TOPMSG.txt
  108.  
  109.  
  110.     export Puppy_Network_Setup="
  111.         <vbox>
  112.             <text><label>\"`cat /tmp/net-setup_TOPMSG.txt`\"</label></text>
  113.             <frame Driver Modules>
  114.                 <text><label>\"`cat /tmp/net-setup_MSGMODULES.txt`\"</label></text>
  115.                 ${MODULEBUTTONS}
  116.             </frame>
  117.             <frame Interfaces>
  118.                 <hbox>
  119.                 <text><label>\"`cat /tmp/net-setup_MSGINTERFACES.txt`\"</label></text>
  120.                 <vbox>
  121.                     <text><label>\" \"</label></text>
  122.                     <pixmap><input file>/usr/share/pixmaps/net-setup_btnsize.png</input></pixmap>
  123.                     ${INTERFACEBUTTONS}
  124.                 </vbox>
  125.                 </hbox>
  126.             </frame>
  127.             <hbox>
  128.                 <vbox>
  129.                     <pixmap><input file>/usr/share/pixmaps/net-setup_btnsize.png</input></pixmap>
  130.                     <button><label>Help</label><action>Exit=21</action></button>
  131.                 </vbox>
  132.                 <vbox>
  133.                     <pixmap><input file>/usr/share/pixmaps/net-setup_btnsize.png</input></pixmap>
  134.                     <button><label>Exit</label><action>Exit=19</action></button>
  135.                 </vbox>
  136.             </hbox>
  137.         </vbox>"
  138. }
  139.  
  140. #================================================================================
  141. showLoadModuleWindow()
  142. {
  143.     echo -n "" > /tmp/ethmoduleyesload.txt
  144.     MODULELIST=`cat /etc/networkmodules | sort | tr "\n" " "`
  145.  
  146.     echo "Xdialog --left --wmclass \"etherwiz\" --title \"Puppy Ethernet Wizard\" --stdout \
  147.      --menubox \"This is the hardware in your PC that seems to be an ethernet interface:
  148. `cat /proc/pci | grep -i "ether" | cut -d: -f 2`
  149.     (Note, USB and PCMCIA ethernet hardware is not shown above, only PCI.)
  150.  
  151. And these are the driver modules already loaded:
  152. ${LOADEDETH}
  153.  
  154. Choose auto-probe, custom driver, or driver from list.\nNote, the custom choice allows you to enter a module name followed by optional\nparameters. For ISA cards, this may be mandatory.\nExample1: ne io=0x000, Example1: arlan  io=0x300 irq=11\n(Example1 works for most ISA cards and does some autoprobing of io and irq) \" 0 0 9 \
  155.      auto \"AUTO PROBE ALL DRIVERS\" \
  156.      specify \"SPECIFY A CUSTOM DRIVER\" \
  157.      $MODULELIST 2> /dev/null" > /tmp/net-setup-modulelist.sh
  158.  
  159.     LOADMODULE_RESPONSE="`sh /tmp/net-setup-modulelist.sh`"
  160.  
  161.   case "${LOADMODULE_RESPONSE}" in
  162.     "")
  163.             TOPMSG="REPORT ON LOADING OF MODULE: No module was loaded" ;;
  164.     "auto")
  165.             autoLoadModule ;;
  166.     "specify")
  167.             loadSpecificModule ;;
  168.     *)
  169.             tryLoadModule ${LOADMODULE_RESPONSE} ;;
  170.   esac
  171.  
  172.   NEWLOADED="`cat /tmp/ethmoduleyesload.txt`"
  173.   NEWLOADf1="`echo -n "$NEWLOADED" | cut -f 1 -d " "`" #remove any extra params.
  174.   if [ "${NEWLOADED}" ];then
  175.         refreshMainWindowInfo
  176.  
  177.     TOPMSG="REPORT ON LOADING OF MODULE: Module '$NEWLOADf1' successfully loaded"
  178.     MODULEBUTTONS="
  179.                     <hbox>
  180.                     <text><label>\"
  181. If you can see a new active interface showing up above click the 'Save'
  182. button and proceed to configure the interface. The 'Save' will save the
  183. selection so that Puppy will automatically load $NEWLOADf1 at bootup.\"</label></text>
  184.                         <vbox>
  185.                             <text><label>\" \"</label></text>
  186.                             <pixmap><input file>/usr/share/pixmaps/net-setup_btnsize.png</input></pixmap>
  187.                             <button><label>Save</label><action>EXIT=17</action></button>
  188.                         </vbox>
  189.                     </hbox>
  190.                     <hbox>
  191.                     <text><label>\"
  192. If you cannot see a new active interface showing up above, it means that
  193. the interface is not recognized by the newly loaded module. Click the
  194. 'Unload' button and try to load another module. The 'Unload', will unload
  195. the module as well as forget about loading it at bootup.\"</label></text>
  196.                         <vbox>
  197.                             <text><label>\" \"</label></text>
  198.                             <pixmap><input file>/usr/share/pixmaps/net-setup_btnsize.png</input></pixmap>
  199.                             <button><label>Unload</label><action>EXIT=18</action></button>
  200.                         </vbox>
  201.                     </hbox>"
  202.     else
  203.       BGCOLOR="#ffc0c0"
  204.       TOPMSG="REPORT ON LOADING OF MODULE: No module was loaded"
  205.     fi
  206. } # end of showLoadModuleWindow
  207.  
  208. #================================================================================
  209. tryLoadModule ()
  210. {
  211.     MODULE_NAME=$1
  212.     if modprobe ${MODULE_NAME}
  213.     then
  214.         Xdialog --left --wrap --stdout --title "Puppy Ethernet Wizard: hardware" --msgbox "Module ${MODULE_NAME} has loaded successfully.\nThat does not mean it will actually work though!\nAfter clicking OK, back on the main window if you see a new active interface\nproceed to configure it." 0 0
  215.         echo -n "${MODULE_NAME}" > /tmp/ethmoduleyesload.txt
  216.     else
  217.         Xdialog --stdout --msgbox "Loading ${MODULE_NAME} failed; try a different driver." 0 0
  218.     fi
  219. } # end tryLoadModule
  220.  
  221. #================================================================================
  222. loadSpecificModule ()
  223. {
  224.     RESPONSE=$(Xdialog --stdout --title "Puppy Ethernet Wizard: hardware" --inputbox "Please type the name of a specific module to load\n(extra parameters allowed, but don't type tab chars)." 0 0 mymodule 2> /dev/null)
  225.     if [ $? -eq 0 ];then
  226.         tryLoadModule ${RESPONSE}
  227.     fi
  228. } # end loadSpecificModule
  229.  
  230. #================================================================================
  231. autoLoadModule ()
  232. {
  233.     #this is the autoloading...
  234.     SOMETHINGWORKED=false
  235.     #clear
  236.     for CANDIDATE in $PCIMODULES
  237.     do
  238.  
  239.         #if have pcmcia, do not try loading the others...
  240.         MDOIT="no"
  241.         echo "$CANDIDATE" | grep "_cs"
  242.         if [ $? -eq 0 ];then #=0 found.
  243.             if [ "$MPCMCIA" = "yes" ];then
  244.                 MDOIT="yes"
  245.             fi
  246.         else
  247.             if [ ! "$MPCMCIA" = "yes" ];then
  248.                 MDOIT="yes"
  249.             fi
  250.         fi
  251.  
  252.         #also, do not try if it is already loaded...?
  253.         cat /tmp/loadedeth.txt | grep "$CANDIDATE"
  254.         if [ $? -eq 0 ];then
  255.             MDOIT="no"
  256.         fi
  257.  
  258.         #in case of false-hits, ignore anything already tried this session...
  259.         cat /tmp/logethtries.txt | grep $CANDIDATE
  260.         if [ $? -eq 0 ];then #=0 found.
  261.             MDOIT="no"
  262.         fi
  263.  
  264.         if [ "$MDOIT" = "yes" ];then
  265.             echo; echo "*** Trying $CANDIDATE."
  266.             if modprobe $CANDIDATE
  267.             then
  268.                 SOMETHINGWORKED=true
  269.                 WHATWORKED=$CANDIDATE
  270.                 #add it to the log for this session...
  271.                 echo "$CANDIDATE" >> /tmp/logethtries.txt
  272.                 break
  273.             fi
  274.         fi
  275.  
  276.     done
  277.     sleep 2
  278.     if $SOMETHINGWORKED
  279.     then
  280.         Xdialog --left --wrap --msgbox "Success loading the $WHATWORKED module. That does not mean it will actually work though!\nAfter clicking OK, back on the main window if you see a new active interface\nproceed to configure it.\n\nNOTE: it is possible that a module loads ok, but it is a false hit, that is, does\nnot actually work with your network card. In that case, try autoprobing again. This\nscript will remember the previous attempts (until you exit this script) and will\njump over them.\nIf you do get false hits, let us know about it on the Puppy Discussion Forum!" 0 0
  281.         echo -n "$WHATWORKED" > /tmp/ethmoduleyesload.txt
  282.     else
  283.         MALREADY="`cat /tmp/loadedeth.txt`"
  284.         Xdialog --msgbox "No module loaded successfully.\n\nNote however that these modules are already loaded:\n${MALREADY}" 0 0
  285.         return 1
  286.     fi
  287. } # end autoLoadModule
  288.  
  289. #================================================================================
  290. findLoadedModules ()
  291. {
  292.   echo -n " " > /tmp/loadedeth.txt
  293.  
  294.   ALL_MOD=`lsmod | cut -f 1 -d " "`
  295.  
  296.     COUNT_MOD=0
  297.   for MOD in $ALL_MOD
  298.   do
  299.         COUNT_MOD=`expr $COUNT_MOD + 1`
  300.   done
  301.  
  302.     (
  303.         for MOD in $ALL_MOD
  304.         do
  305.             echo "X"
  306.             echo "$PCIMODULES" | grep -E "^${MOD}$" >> /tmp/loadedeth.txt
  307.             if [ $? -eq 0 ];then
  308.              echo -n " " >> /tmp/loadedeth.txt #space separation
  309.             fi
  310.         done
  311.   ) | Xdialog --title "Puppy Ethernet Wizard" --progress "Checking loaded modules" 0 0 $COUNT_MOD
  312.  
  313. } # end of findLoadedModules
  314.  
  315. #================================================================================
  316. testInterface()
  317. {
  318.   INTERFACE=$1
  319.  
  320.     (
  321.         UNPLUGGED="false"
  322.         ifconfig $INTERFACE | grep " UP " > /dev/null
  323.         if [ ! $? -eq 0 ];then #=0 if found
  324.             ifconfig $INTERFACE up
  325.         fi
  326.         #BK1.0.7 improved link-beat detection...
  327.         echo "X"
  328.         if [ "`ifplugstatus ${INTERFACE} | grep "link beat detected"`" = "" ];then
  329.             sleep 2
  330.             echo "X"
  331.             if [ "`ifplugstatus-0.25 ${INTERFACE} | grep "link beat detected"`" = "" ];then
  332.                 sleep 2
  333.                 echo "X"
  334.                 if [ "`ifplugstatus ${INTERFACE} | grep "link beat detected"`" = "" ];then
  335.                     sleep 2
  336.                     echo "X"
  337.                     if [ "`ifplugstatus-0.25 ${INTERFACE} | grep "link beat detected"`" = "" ];then
  338.                         UNPLUGGED="true"
  339.                     fi
  340.                 fi
  341.             fi
  342.         fi
  343.         echo "${UNPLUGGED}" > /tmp/net-setup_UNPLUGGED.txt
  344.   ) | Xdialog --title "Puppy Ethernet Wizard" --progress "Testing Interface ${INTERFACE}" 0 0 4
  345.  
  346.   UNPLUGGED=$(cat /tmp/net-setup_UNPLUGGED.txt)
  347.  
  348.   if [ "$UNPLUGGED" != "false" ];then #BK1.0.7
  349.     #no cable plugged in, no network connection possible...
  350.     ifconfig $INTERFACE down
  351.     BGCOLOR="#ffc0c0"
  352.     if [ ${IS_WIRELESS} ] ; then
  353.       TOPMSG="REPORT ON TEST OF $INTERFACE CONNECTION:
  354. 'Unable to connect to a wireless network'
  355.  
  356. Verify that the wireless network is available and
  357. that you have provided the correct wireless parameters."
  358.     else
  359.       TOPMSG="REPORT ON TEST OF $INTERFACE CONNECTION:
  360. 'Unable to connect to the network'
  361.  
  362. Verify that the network is available and
  363. that the ethernet cable is plugged in."
  364.     RETTEST=1
  365.     fi
  366.   else
  367.     BGCOLOR="#e0ffe0"
  368.     TOPMSG="REPORT ON TEST OF $INTERFACE CONNECTION:
  369. 'Puppy was able to find an alive network'
  370.  
  371. You can proceed to acquire an IP address."
  372.         RETTEST=0
  373.   fi
  374.  
  375.   return ${RETTEST}
  376. } # end of testInterface
  377.  
  378. #================================================================================
  379. showConfigureInterfaceWindow()
  380. {
  381.   INTERFACE=$1
  382.  
  383.   initializeConfigureInterfaceWindow
  384.  
  385.   RETVALUE=""
  386.   # 1=Close window 19=Back Button 22=Save configuration
  387.   while [ "$RETVALUE" != "1" ] && [ "$RETVALUE" != "19" ] && [ "$RETVALUE" != "22" ]
  388.   do
  389.  
  390.     buildConfigureInterfaceWindow
  391.  
  392.     I=$IFS; IFS=""
  393.     for STATEMENTS in  $(gtkdialog --program Puppy_Network_Setup); do
  394.       eval $STATEMENTS
  395.     done
  396.     IFS=$I
  397.  
  398.     RETVALUE=${EXIT}
  399.     [ ! "${RETVALUE}" != "abort" ] &&    RETVALUE=1
  400.  
  401.     RETSETUP=99
  402.     case $RETVALUE in
  403.        1 | 19) # close window
  404.           TOPMSG="NETWORK CONFIGURATION OF $INTERFACE CANCELED!
  405. Try again, or click 'Exit' to give up for now."
  406.           ;;
  407.       10) # AutoDHCP
  408.           dhcpSetup
  409.           RETSETUP=$?
  410.           ;;
  411.       11) # StaticIP
  412.           manualSetup
  413.           RETSETUP=$?
  414.           ;;
  415.       13) # Test
  416.           testInterface ${INTERFACE}
  417.           RETSETUP=$?
  418.           ;;
  419.       14) # Wireless
  420.           configureWireless ${INTERFACE}
  421.           ;;
  422.       21) # Help
  423.           showHelp
  424.           ;;
  425.     esac
  426.  
  427.     if [ $RETVALUE -eq 10 ] || [ $RETVALUE -eq 11 ] ; then
  428.       if [ $RETSETUP -ne 0 ] ; then
  429.         TOPMSG="NETWORK CONFIGURATION OF $INTERFACE UNSUCCESSFUL!
  430. Try again, or click 'Back' to give up for now."
  431.       else
  432.         RETVALUE=1
  433.         Xdialog --yesno "NETWORK CONFIGURATION OF $INTERFACE SUCCESSFUL!
  434.  
  435. Do you want to save this configuration?
  436.  
  437. If you want to keep this configuration for next boot: click 'Yes'.
  438. If you just want to use this configuration for this session: click 'No'." 0 0
  439.         if [ $? -eq 0 ] ; then
  440.           saveInterfaceSetup ${INTERFACE}
  441.           TOPMSG="NETWORK CONFIGURATION OF $INTERFACE SUCCESSFUL!
  442. The configuration has been saved to file /etc/${INTERFACE}mode.
  443. This file is read at bootup by /etc/rc.d/rc.network
  444.  
  445. If there are no more interfaces to setup and configure, just click 'Exit' to get out."
  446.         else
  447.           TOPMSG="NETWORK CONFIGURATION OF $INTERFACE SUCCESSFUL!
  448. The configuration was not saved for next boot.
  449.  
  450. If there are no more interfaces to setup and configure, just click 'Exit' to get out."
  451.         fi
  452.       fi
  453.     fi
  454.  
  455.   done
  456.  
  457. } # end showConfigureInterfaceWindow
  458.  
  459. #================================================================================
  460. buildConfigureInterfaceWindow ()
  461. {
  462.     export Puppy_Network_Setup="
  463.         <vbox>
  464.             <frame Configure ${INTERFACE}>
  465.                 <text><label>\"${TOPMSG}\"</label></text>
  466.                 ${WIRELESSSECTION}
  467.                 <hbox>
  468.                     <text><label>\"${TESTMSG}\"</label></text>
  469.                     <vbox>
  470.                         <text><label>\" \"</label></text>
  471.                         <pixmap><input file>/usr/share/pixmaps/net-setup_btnsize.png</input></pixmap>
  472.                         <button><label>Test ${INTERFACE}</label><action>Exit=13</action></button>
  473.                     </vbox>
  474.                 </hbox>
  475.                 <hbox>
  476.                     <text><label>\"${DHCPMSG}\"</label></text>
  477.                     <vbox>
  478.                         <text><label>\" \"</label></text>
  479.                         <pixmap><input file>/usr/share/pixmaps/net-setup_btnsize.png</input></pixmap>
  480.                         <button><label>Auto DHCP</label><action>Exit=10</action></button>
  481.                     </vbox>
  482.                 </hbox>
  483.                 <hbox>
  484.                     <text><label>\"${STATICMSG}\"</label></text>
  485.                     <vbox>
  486.                         <text><label>\" \"</label></text>
  487.                         <pixmap><input file>/usr/share/pixmaps/net-setup_btnsize.png</input></pixmap>
  488.                         <button><label>Static IP</label><action>Exit=11</action></button>
  489.                     </vbox>
  490.                 </hbox>
  491.             </frame>
  492.             <hbox>
  493.                 <vbox>
  494.                     <pixmap><input file>/usr/share/pixmaps/net-setup_btnsize.png</input></pixmap>
  495.                     <button><label>Help</label><action>Exit=21</action></button>
  496.                 </vbox>
  497.                 ${SAVE_SETUP_BUTTON}
  498.                 <vbox>
  499.                     <pixmap><input file>/usr/share/pixmaps/net-setup_btnsize.png</input></pixmap>
  500.                     <button><label>Back</label><action>Exit=19</action></button>
  501.                 </vbox>
  502.             </hbox>
  503.         </vbox>"
  504. }
  505.  
  506. #================================================================================
  507. initializeConfigureInterfaceWindow ()
  508. {
  509.     TOPMSG="OK, let's try to configure ${INTERFACE}."
  510.  
  511.     TESTMSG="
  512. Make sure that your ${INTERFACE} interface is connected into an 'alive'
  513. network by clicking on the 'Test' button.
  514. After you confirm that, you can configure the interface."
  515.  
  516.     DHCPMSG="
  517. The absolutely easiest way to configure the network is to be on
  518. a LAN that has a DHCP server. This will enable Puppy to
  519. query the server at bootup and automatically be assigned an IP
  520. address. The'dhcpcd' client daemon program is launched and
  521. network access happens automatically."
  522.  
  523.     STATICMSG="
  524. If a DHCP server is not available, you will have to do everything
  525. manually by setting a static IP, but this script will make it easy
  526. (I would like to thank the guys at the LNX-BBC Linux project,
  527.  whose 'trivial-net-setup' script helped me enormously here)."
  528.  
  529.     checkIfIsWireless ${INTERFACE}
  530.  
  531.     if [ ${IS_WIRELESS} ] ; then
  532.         WIRELESSSECTION="
  533.                     <hbox>
  534.                         <text><label>\"
  535. Puppy found that ${INTERFACE} is a wireless interface.
  536. If you want to conect using a wireless network, you must set the
  537. wireless parameters before trying to configure it either with
  538. DHCP or Static IP.\"</label></text>
  539.                         <vbox>
  540.                             <text><label>\" \"</label></text>
  541.                             <pixmap><input file>/usr/share/pixmaps/net-setup_btnsize.png</input></pixmap>
  542.                             <button><label>Wireless</label><action>Exit=14</action></button>
  543.                         </vbox>
  544.                     </hbox>
  545.                     "
  546.     else
  547.         WIRELESSSECTION=""
  548.     fi
  549.     SAVE_SETUP_BUTTON=""
  550. }
  551.  
  552. #================================================================================
  553. checkIfIsWireless ()
  554. {
  555.   INTERFACE=$1
  556.   IS_WIRELESS=""
  557.  
  558.     cat /proc/net/wireless | grep -q "${INTERFACE}"
  559.     [ $? -eq 0 ] && IS_WIRELESS="true"
  560. }
  561.  
  562. #================================================================================
  563. configureWireless()
  564. {
  565.     INTERFACE=$1
  566.     showProfilesWindow ${INTERFACE}
  567.     if [ $? -eq 0 ] ; then
  568.         testInterface ${INTERFACE}
  569.     else
  570.         TOPMSG="WIRELESS CONFIGURATION OF $INTERFACE CANCELED!
  571. To connect to a wireless network you have to select a profile to use'. "
  572.     fi
  573. }
  574.  
  575. #================================================================================
  576. dhcpSetup()
  577. {
  578.     ifconfig ${INTERFACE} down
  579.     {
  580.         if ps wax | grep dhcpcd | grep "$INTERFACE" >/dev/null
  581.         then
  582.             # Must kill old dhcpcd first
  583.             dhcpcd -k "$INTERFACE"
  584.             rm /etc/dhcpc/dhcpcd-${INTERFACE}.pid 2>/dev/null #if left over from last session, causes trouble.
  585.             rm /etc/dhcpc/dhcpcd-${INTERFACE}.cache 2>/dev/null #ditto
  586.             rm /etc/dhcpc/dhcpcd-${INTERFACE}.info 2>/dev/null #ditto
  587.         fi
  588.         sleep 1
  589.         rm -f /etc/dhcpc/dhcpcd-${INTERFACE}.pid
  590.         if dhcpcd -d "$INTERFACE"
  591.         then
  592. #            echo -n "auto" > /etc/${INTERFACE}mode
  593.             HAS_ERROR=0
  594.         else
  595. #            rm -f /etc/${INTERFACE}mode
  596.             HAS_ERROR=1
  597.         fi
  598.         echo "${HAS_ERROR}" > /tmp/net-setup_HAS_ERROR.txt
  599.         echo "XXXX"
  600.     } | Xdialog --no-buttons --title "Puppy Ethernet Wizard: DHCP" --infobox "There may be a delay of up to 60 seconds while Puppy waits for the
  601. DHCP server to respond. Please wait patiently..." 0 0 0
  602.  
  603.   HAS_ERROR=$(cat /tmp/net-setup_HAS_ERROR.txt)
  604.  
  605.   if [ ${HAS_ERROR} -eq 0 ]
  606.   then
  607.     MODECOMMANDS="
  608. echo \"Trying to get IP address from DHCP server (60sec timeout)...\"
  609. rm /etc/dhcpc/dhcpcd-${INTERFACE}.pid 2>/dev/null #if left over from last session, causes trouble.
  610. rm /etc/dhcpc/dhcpcd-${INTERFACE}.cache 2>/dev/null #ditto
  611. rm /etc/dhcpc/dhcpcd-${INTERFACE}.info 2>/dev/null #ditto
  612. dhcpcd ${INTERFACE}
  613. "
  614.   else
  615.     MODECOMMANDS=""
  616.   fi
  617.  
  618.     return ${HAS_ERROR}
  619. } #end of dhcpSetup
  620.  
  621. #================================================================================
  622. manualSetup()
  623. {
  624.  
  625.   Xdialog --center --wrap --title "Puppy Ethernet Wizard: Static IP" --msgbox "Configuring manually...\n\nIn the questions that follow, please enter all addresses in dotted-quad format (a.b.c.d)\nother formats will not be recognized." 0 0
  626.   while true
  627.   do
  628.     readaddr "Enter your IP address (e.g. 216.88.157.147): "
  629.     if [ ! $? -eq 0 ];then
  630.       return 1
  631.     fi
  632.     IP="$RESULT"
  633.     DEFAULTMASK=$(ipcalc --netmask "$IP" | cut -d= -f2)
  634.     while [ -z "$NETMASK" ]
  635.     do
  636.       readaddr "Enter your netmask: " $DEFAULTMASK
  637.       if [ ! $? -eq 0 ];then
  638.         return 1
  639.       fi
  640.  
  641.       if [ "x$RESPONSE" = x ];then
  642.         NETMASK="$DEFAULTMASK"
  643.       else
  644.         if validip "$RESPONSE"    # This is wrong, because not all valid IP
  645.                 # addresses are valid netmasks.  But we can
  646.                 # catch some inconsistencies below.
  647.         then
  648.           NETMASK="$RESPONSE"
  649.         else
  650.           Xdialog --center --title "Puppy Ethernet Wizard: Static IP" --msgbox "Invalid entry, try again." 0 0
  651.         fi
  652.       fi
  653.     done
  654.  
  655.     BROADCAST=$(ipcalc -b $IP $NETMASK | cut -d= -f2)
  656.  
  657.     unset GATEWAY
  658.     GATEWAYOK=false
  659.     until $GATEWAYOK
  660.     do
  661.       unset RESULT
  662.       readaddr_blankok "
  663. Enter your default router (gateway) address (Enter for none):
  664.  
  665. Note that if you won't specify a gateway you will have access
  666. your local-area network, but you'll need to add a route
  667. manually if you want to use the Internet"
  668.       if [ ! $? -eq 0 ];then
  669.         return 1
  670.       fi
  671.       if [ -z "$RESULT" ];then
  672.         GATEWAYOK=true
  673.       else
  674.         if validip "$RESULT"
  675.         then
  676.           GATEWAY="$RESULT"
  677.           GATEWAYOK=true
  678.         else
  679.           xmessage -center -title "Puppy Ethernet Wizard: Static IP" "Invalid entry, you will have to try again."
  680.         fi
  681.       fi
  682.     done
  683.  
  684.     echo "You have GATEWAY=$GATEWAY" #test
  685.  
  686.  # Now let's do the nameserver
  687.  unset NAMESERVER
  688.  NAMESERVEROK=false
  689.  until $NAMESERVEROK
  690.  do
  691.   unset RESULT
  692.   readaddr_blankok "Enter the IP address of a nameserver for resolving DNS names to IP addresses\n(Enter for defaults, which should usually work on the public Internet):"
  693.   if [ ! $? -eq 0 ];then
  694.    return 1
  695.   fi
  696.   if [ -z "$RESULT" ];then
  697.    NAMESERVEROK=true
  698.   else
  699.    if validip "$RESULT"
  700.    then
  701.     NAMESERVER="$RESULT"
  702.     NAMESERVEROK=true
  703.    else
  704.     xmessage -center -title "Puppy Ethernet Wizard: Static IP" "Invalid entry, you will have to try again."
  705.    fi
  706.   fi
  707.  done
  708.  
  709.  # Check that network is right
  710.  HOSTNUM=$(dotquad "$IP")
  711.  MASKNUM=$(dotquad "$NETMASK")
  712.  GATENUM=$(dotquad "$GATEWAY")
  713.  CASTNUM=$(dotquad "$BROADCAST")
  714.  
  715.  HOSTNET=$(and "$MASKNUM" "$HOSTNUM")
  716.  GATENET=$(and "$MASKNUM" "$GATENUM")
  717.  
  718.  if [ -z "$GATEWAY" ];then
  719.   # It is legitimate not to have a gateway at all.  In that case, it
  720.   # doesn't have a network. :-)
  721.   unset HOSTNET
  722.   unset GATENET
  723.  fi
  724.  
  725. #BK: the lnx-bbc people had ifconfig $INTERFACE inet $IP netmask $NETMASK broadcast $BROADCAST up
  726. #    but "inet" seems nonstandard. $IP is required without the "inet"...
  727.  
  728.     ifconfig ${INTERFACE} down
  729.  
  730.  CONVO=`cat <<MSG1
  731. ifconfig ${INTERFACE} ${IP} netmask ${NETMASK} broadcast ${BROADCAST} up
  732. `
  733.  CONVG=`cat <<MSG2
  734. route add -net default gw ${GATEWAY}
  735. `
  736.  if [ x"$HOSTNET" != x"$GATENET" ];then
  737.   Xdialog --center --wrap --title "Puppy Ethernet Wizard: Static IP" --msgbox "Your gateway $GATEWAY is not on this network!  Please try again.\n(You may have entered your netmask incorrectly, for instance.)" 0 0  0 0
  738.  else
  739.   # do the work BK: see note above.
  740.   ifconfig $INTERFACE $IP netmask $NETMASK broadcast $BROADCAST up
  741.   if [ $? -eq 0 ];then
  742. #   echo "$CONVO" > /etc/${INTERFACE}mode #used at bootup, see rc.network.
  743.    MODECOMMANDS="${CONVO}"
  744.    # Configure a nameserver, if we're supposed to.
  745.    # This now replaces any existing resolv.conf, which
  746.    # we will try to back up.
  747.    if [ "$NAMESERVER" ];then
  748.     mv -f /etc/resolv.conf /etc/resolv.conf.$$
  749.     echo "nameserver $NAMESERVER" > /etc/resolv.conf
  750.    fi
  751.  
  752.    # add default route, if we're supposed to
  753.    if [ "$GATEWAY" ];then
  754.     route add -net default gw "$GATEWAY"
  755.     if [ $? -eq 0 ];then #0=ok.
  756.      Xdialog --center --title "Puppy Ethernet Wizard: Static IP" --msgbox "Default route set through $GATEWAY." 0 0
  757. #     echo "$CONVG" >> /etc/${INTERFACE}mode #used at bootup.
  758.      MODECOMMANDS="${MODECOMMANDS}\n${CONVG}"
  759.     else
  760.      Xdialog --center --title "Puppy Ethernet Wizard: Static IP" --msgbox "Could not set default route through $GATEWAY.  Please try again.\n\nNote that Puppy has tried to do this:\n${CONVG}" 0 0
  761.      ifconfig "$INTERFACE" down
  762.      return 1
  763.     fi
  764.    fi
  765.  
  766.    return 0
  767.   else
  768.    Xdialog --center --title "Puppy Ethernet Wizard: Static IP" --msgbox "Interface configuration failed; please try again.\n\nIf configuration had succeeded, file /etc/${INTERFACE}mode would have\nbeen created, which is executed at bootup by /etc/rc.d/rc.network\nIf you really want to, you can manually create this file.\n\nWhat Puppy has just tried to do is this:\n${CONVO} \n\nIf you think that this is incorrect for your system, and you can come up\nwith something else that works, let me know and maybe I can modify this\nnetwork setup script." 0 0
  769.    ifconfig "$INTERFACE" down
  770. #   rm /etc/${INTERFACE}mode
  771.    MODECOMMANDS=""
  772.   fi
  773.  fi
  774.  
  775.  unset NETMASK    # Must unset NETMASK in order for the user to be prompted
  776.         # for it next time through.
  777.  
  778. done
  779. } #end of manualSetup
  780.  
  781. #================================================================================
  782. saveNewModule()
  783. {
  784.   # save newly loaded module
  785.   cat /etc/ethernetmodules | grep "$NEWLOADED"
  786.   if [ ! $? -eq 0 ];then
  787.     echo "$NEWLOADED" >> /etc/ethernetmodules
  788.   fi
  789.   TOPMSG="MODULE '$NEWLOADED' RECORDED IN /etc/ethernetmodules
  790. Puppy will read this when booting up."
  791.     setDefaultMODULEBUTTONS
  792. }
  793.  
  794.  
  795. #================================================================================
  796. unloadNewModule()
  797. {
  798.   # unload newly loaded module
  799.   modprobe -r $NEWLOADED
  800.   cat /etc/ethernetmodules | grep -v "$NEWLOADED" > /etc/ethernetmodules
  801.   TOPMSG="MODULE '$NEWLOADED' UNLOADED.
  802. Also, '$NEWLOADED' removed from /etc/ethernetmodules (if it was there)."
  803.  
  804.   setDefaultMODULEBUTTONS
  805.  
  806.     refreshMainWindowInfo
  807.  
  808. }
  809.  
  810. #================================================================================
  811. validip() {
  812. # uses dotquad.c to parse $1 as a dotted-quad IP address
  813. if dotquad "$1" >/dev/null
  814. then
  815.  return 0
  816. else
  817.  return 1
  818. fi
  819. } #end of validip function
  820.  
  821. #================================================================================
  822. readaddr() {
  823. while true
  824. do
  825.  foo=$(Xdialog --center --wrap --stdout --title "Puppy Ethernet Wizard" --inputbox "$1" 0 0 $2 2>/dev/null)
  826.  # cancel?
  827.  if [ $? -ne 0 ];then
  828.   return 1
  829.  fi
  830.  #dotquad $foo
  831.  if validip "$foo"
  832.  then
  833.   if [ ! "$foo" = "216.88.157.147" ];then
  834.    #RESULT="$foo"
  835.    #RESPONSE="$foo"
  836.    #return
  837.    break
  838.   else
  839.    Xdialog --center --wrap --title "Puppy Ethernet Wizard: networking" --msgbox "216.88.157.147 was provided only as an example.\nPlease enter your own IP address instead!" 0 0
  840.   fi
  841.  fi
  842. done
  843. RESULT="$foo"
  844. RESPONSE="$foo"
  845. } #end of readaddr function
  846.  
  847. #================================================================================
  848. readaddr_blankok() {
  849. while true
  850. do
  851.  foo=$(Xdialog --center --wrap --stdout --title "Puppy Ethernet Wizard: networking" --inputbox "$1" 0 0 $2 2>/dev/null)
  852.  # cancel?
  853.  if [ $? -ne 0 ];then
  854.   return 1
  855.  fi
  856.  if [ -z "$foo" ];then
  857.   RESULT=""
  858.   RESPONSE=""
  859.   return 0
  860.  fi
  861.  if validip "$foo"
  862.  then
  863.   if [ "$foo" != "216.88.157.147" ];then
  864.    RESULT="$foo"
  865.    return 0
  866.   else
  867.    Xdialog --center --wrap --title "Puppy Ethernet Wizard: networking" --msgbox "216.88.157.147 was provided only as an example.\nPlease enter your own IP address instead!" 0 0
  868.   fi
  869.  fi
  870. done
  871. RESULT="$foo"
  872. RESPONSE="$foo"
  873. } #end of readaddr_blankok function
  874.  
  875. #================================================================================
  876. setDefaultMODULEBUTTONS ()
  877. {
  878.   MODULEBUTTONS="
  879.               <hbox>
  880.                     <text><label>\"If it appears the driver module for an ethernet card isn't loaded,
  881. click on the 'Load a driver' button.\"</label></text>
  882.                     <vbox>
  883.                         <pixmap><input file>/usr/share/pixmaps/net-setup_btnsize.png</input></pixmap>
  884.                         <button><label>Load driver</label><action>Exit=10</action></button>
  885.                     </vbox>
  886.                 </hbox>"
  887. }
  888.  
  889.  
  890. #================================================================================
  891. saveInterfaceSetup()
  892. {
  893.   INTERFACE=$1
  894.  
  895.   if [ ! ${IS_WIRELESS} ] ; then
  896.     rm -f /etc/${INTERFACE}wireless
  897.   fi
  898.  
  899.   if [ -e "/tmp/wag-profiles_iwconfig.sh" ] ; then
  900.     cp /tmp/wag-profiles_iwconfig.sh /etc/${INTERFACE}wireless
  901.   fi
  902.  
  903.   echo -e "${MODECOMMANDS}" > /etc/${INTERFACE}mode
  904.  
  905. }
  906.  
  907. #================================================================================
  908. showHelp()
  909. {
  910.   dillo /usr/share/doc/HOWTO-network.htm &
  911. }
  912.  
  913.  
  914. #================================================================================
  915. #=============== START OF SCRIPT BODY ====================
  916. #================================================================================
  917.  
  918.  
  919. # Cleanup older temp files
  920. rm -f /tmp/ethmoduleyesload.txt
  921. rm -f /tmp/loadedeth.txt
  922. rm -f /tmp/wag-profiles_iwconfig.sh
  923.  
  924. > /tmp/logethtries.txt
  925.  
  926. # Do we have pcmcia hardware?...
  927. cat /proc/pci | grep -i "cardbus" > /dev/null
  928. if [ $? -eq 0 ];then
  929.   MPCMCIA="yes"
  930. else
  931.   cat /proc/pci | grep -i "pcmcia" > /dev/null
  932.   if [ $? -eq 0 ];then
  933.     MPCMCIA="yes"
  934.    fi
  935. fi
  936. PCIMODULES=`cat /etc/networkmodules | cut -f 1 -d " "`
  937.  
  938. setDefaultMODULEBUTTONS
  939.  
  940. refreshMainWindowInfo
  941.  
  942. BGCOLOR="#ffe0e0" #light red.
  943. TOPMSG="Hi, networking is not always easy to setup, but let's give it a go!"
  944.  
  945. MAIN_RESPONSE=""
  946.  
  947. showMainWindow
  948.  
  949. #================================================================================
  950. #================ END OF SCRIPT BODY =====================
  951. #================================================================================
  952.