home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2 / Openstep-4.2-Intel-User.iso / usr / template / client / etc / rc.net < prev    next >
Text File  |  1995-11-17  |  4KB  |  164 lines

  1. #!/bin/sh
  2. #
  3. #  /etc/rc.net
  4. #
  5. #  Configure all known interfaces according to the rules in /etc/iftab
  6. #
  7. #  Copyright (C) 1992 by NeXT Computer, Inc.  All rights reserved.
  8. #
  9. #  Usage: rc.net [-d] [-h] [interface...]
  10. #
  11.  
  12. iftab=/etc/iftab
  13.  
  14. #
  15. #  Let ^C's interrupt the commands run hereunder (eg, ifconfig), not
  16. #  the shell script itself.
  17. #
  18. trap "true" 2
  19.  
  20. #
  21. #  Handle command options
  22. #
  23. while [ -n "$1" ]; do
  24.     case "$1" in
  25.     -d) DEBUG=-YES-; shift;;
  26.     -h) SETHOSTNAME=-YES-; shift;;
  27.     *) iflist="$*"; break;;
  28.     esac
  29. done
  30.  
  31. #
  32. #  Debug versions of actual configuration commands
  33. #
  34.  
  35. docmd() {
  36.     if [ "$DEBUG" = -YES- ]; then
  37.     echo "#" "$@"
  38.     else
  39.     "$@"
  40.     fi
  41. }
  42.  
  43. #
  44. #  A procedure to use /etc/hostconfig to configure a given interface
  45. #
  46. #  Usage: hostconfig <interface>
  47. #
  48. hostconfig() {
  49.     if [ -n "$INETADDR" -a "$INETADDR" != -NO- ]; then
  50.     cmd="ifconfig $1 inet $INETADDR"
  51.     if [ -n "$IPNETMASK" ]; then
  52.         cmd="$cmd netmask $IPNETMASK"
  53.     fi
  54.     if [ -n "$IPBROADCAST" -a "$IPBROADCAST" != -AUTOMATIC- ]; then
  55.         cmd="$cmd broadcast $IPBROADCAST"
  56.     fi
  57.     cmd="$cmd -trailers up"
  58.     docmd $cmd
  59.     fi
  60. }
  61.  
  62. #
  63. #  Read /etc/hostconfig for backwards compatibility
  64. #
  65. if [ -f /etc/hostconfig ]; then
  66.   . /etc/hostconfig
  67. fi
  68.  
  69. #
  70. #  Ask ifconfig for all available interface names if none were given
  71. #  on the command line.
  72. #
  73. if [ -z "$iflist" ]; then
  74.     iflist="`ifconfig -a | awk -F: '/^[a-z]/ {print $1}'`"
  75. fi
  76.  
  77. #
  78. #  Make sure that the local and ppp interfaces are done last,
  79. #  so that they never become the primary interface, 
  80. #  unless they are the only interfaces we have.
  81. #
  82. iflist=`echo $iflist | awk '
  83. BEGIN {
  84.     i=1;  ifs[i++] = "lo0"; ifs[i++] = "ppp0"; ifs[i] = "ppp1"; LAST_IFS = i;
  85.     for (i = 1; i <= LAST_IFS; i++)
  86.     ifStr = ifStr ":" ifs[i] ":";
  87. }
  88. {
  89.     for (i = 1; i <= NF; i++)
  90.     if (index(ifStr, ":" $i ":"))
  91.         delayIfs[$i] = 1;
  92.     else
  93.         print $i;
  94.     for (i = 1; i <= LAST_IFS; i++)
  95.     if (delayIfs[ifs[i]] == 1)
  96.         print ifs[i];
  97. }'`
  98. primif=`echo $iflist | awk '{print $1}'`
  99.  
  100. #
  101. #  Go for it -- configure each interface in our list
  102. #
  103. if [ -s $iftab ]; then
  104.     # Look for matching config information in /etc/iftab
  105.     for if in $iflist; do
  106.     (while read name af args; do
  107.         # Special hack for primary interface
  108.         if [ "X$name" = "X-1-" -a $if = $primif ]; then
  109.         name=$if
  110.         fi
  111.         # Skip '#' comments while trying to match on each device pattern
  112.         case $if in
  113.         \#*) ;;
  114.         $name)
  115.             # Found a matching interface; have we done this address
  116.             # family before?  (Check by inspecting a shell variable
  117.             # formed by the device name and the address family, for
  118.             # example: $en0_inet)
  119.             eval done=\${$if\_$af}
  120.             if [ -z "$done" ]; then
  121.             # A "!" escape will allow us to put any configuration
  122.             # command in iftab.  The config command may make use
  123.             # of the shell variables $if and $af to get the name
  124.             # of the current interface and address family.
  125.             case "$args" in
  126.                 -HOSTCONFIG-) hostconfig $if;;
  127.                 !*) eval docmd `echo $args | sed 's/^!//'`;;
  128.                 *)  docmd ifconfig $if $af $args;;
  129.             esac
  130.             eval $if\_$af=DONE
  131.             fi;;
  132.         esac
  133.     done) < $iftab
  134.     done
  135. else
  136.     # More backwards compatibility -- if we don't have an /etc/iftab,
  137.     # try using the stuff in /etc/hostconfig instead.
  138.     hostconfig en0
  139.     # Make sure that the loopback interface gets initialized
  140.     docmd ifconfig lo0 inet -AUTOMATIC- netmask -AUTOMATIC-
  141. fi
  142.  
  143. #
  144. #  Set the hostname if we're asked to do so.
  145. #
  146. if [ "$SETHOSTNAME" = -YES- ]; then
  147.     if [ -z "$HOSTNAME" ]; then
  148.     HOSTNAME=-AUTOMATIC-
  149.     fi
  150.     if [ "$HOSTNAME" != -NO- ]; then
  151.     echo "Setting hostname to $HOSTNAME"
  152.     docmd hostname $HOSTNAME
  153.     fi
  154. fi
  155.  
  156. #
  157. #  Finally, let nmserver know the fruits of our network configuration endeavor.
  158. #
  159. pid=`ps cax | egrep nmserver | awk '{print $1;}'`
  160. if [ -n "$pid" ]; then
  161.     echo "Reinitializing nmserver's network portion"
  162.     docmd kill -USR2 $pid
  163. fi
  164.