Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Adding static routes to a network' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Adding static routes to a network
Authored by: djdawson on Apr 05, '06 07:27:19AM
I was faced with a similar problem when I added second ethernet interface to my work machine to connect to a test network. I needed to move the default route to the other interface, and add a bunch of static routes out the various interfaces for full connectivity. However, I used launchd and created a script I put in /etc to do this. Here's my launchd .plist file, which I put in /Library/LaunchDaemons/net.routes.static.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://
www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
         <key>Label</key>
         <string>net.routes.static</string>
         <key>ProgramArguments</key>
         <array>
                 <string>/etc/routes.sh</string>
         </array>
         <key>RunAtLoad</key>
         <true/>
         <key>ServiceDescription</key>
         <string>Install various static routes to support multiple NIC cards</string>
</dict>
</plist>
And here's the shell script I slapped together and put in /etc/routes.sh:

#!/bin/sh
#
#
# We need to trap on TERM signals, according to Apple's launchd docs:
#
trap 'exit 1' 15

#
# Issue a log message so we know when we started
#
syslog -s -l 1 routes.sh: Starting...

#
# Use the "ipconfig waitall" command to wait for all the
# interfaces to come up:
#
ipconfig waitall

#
# Find out how many interfaces are up.
# Look only for IPv4 interfaces and ignore "lo0"
# We expect to see 2 - "en0" and "en1":
#
numup=$(ifconfig -u | egrep "inet " | egrep -v 127\.0\.0\.1 | wc -l)

#
# If there are no interfaces, sleep for 10 seconds and try again
#
if [ ${numup} -eq 0 ]; then
   # No real interfaces up yet, so take a nap
   # Issue a log message so we know what happened
   syslog -s -l 1 routes.sh: No interfaces - sleeping for 10 seconds...
   sleep 10

   #
   # Check for interfaces again
   # If there still aren't any, just give up and exit with a status of 1
   #
   numup=$(ifconfig -u | egrep "inet " | egrep -v 127\.0\.0\.1 | wc -l)

   if [ ${numup} -eq 0 ]; then
     # No network found - bail out
     # Issue a log message so we know what happened
     syslog -s -l 1 routes.sh: Still no interfaces - bailing out.
     exit 1
   fi
fi

#
# If we're here, there's at least one real IP interace up.
# Put the list of "up" interfaces in "$ints" and then add
# the appropriate routes for each on in turn.  Note that this
# list includes "lo0", so we'll just ignore it rather than try
# to edit it out of the list.  This also prevents the list from
# being empty and possibly causing other problems.  For now we
# only expect to see "en0" and "en1", so if we see any others
# we'll just ignore them, too.
#
ints=$(ifconfig -lu)

for eth in $ints; do
   case $eth in

     lo0)
          # Skip lo0 - localhost interface
     ;;

     en0)
          # Add routes for en0 - main interface
          #
          # Issue a log message
          syslog -s -l 1 routes.sh: Adding static routes for en0...
          #
          #
          route add  10.0.0.0/8         172.28.56.1
          route add  172.16.0.0/12      172.28.56.1
          route add  192.168.0.0/16     172.28.56.1
     ;;

     en1)
          # Issue a log message
          syslog -s -l 1 routes.sh: Adding static routes for en1...
          #
          # First we need to delete any exisitng default route
          #
          def=$(netstat -rn | grep default | awk '{print $1, $2}')
          if [ "$def" != "" ]; then
            route delete $def
          fi

          # Adding routes for en1 - second interface
          #
          route add  default            172.31.254.1
          route add  172.31.255.0/24    172.31.254.1
          route add  172.31.252.0/24    172.31.254.60
     ;;

     *)
          # Found some other interface(s) - skip
     ;;

   esac
done

#
# Sleep for a while so Launchd won't think we never ran
#
sleep 10

#
# Issue a log message so we'll know when we finished
#
syslog -s -l 1 routes.sh: Done.

#
# Exit with a clean status
#
exit 0
I seems to work fine, but I'm sure it could be cleaned up in a few areas. HTH

[ Reply to This | # ]
Adding static routes to a network
Authored by: dlgraves on Apr 05, '06 03:34:46PM

I think this might answer a question I posted earlier -- can you use this to assign traffic to a particular interface (en0, en1) by either application or by port?

I have access to two interfaces, one is my school's wireless network and one is my cable modem account. I want to be able to dedicate one interface to gaming, and use the other for surfing, email, streaming, etc.

thanks
Lucas



[ Reply to This | # ]
Adding static routes to a network
Authored by: gjanssen on Mar 14, '08 07:27:12AM
I found neither of the other two static route hints were working for me.
Adding the route to /etc/rc.local failed to ever actually add the route. I was adding my route manually without a problem, so figured the trouble adding this at boot was related to the networking not being sufficiently "up" when the /etc/rc.local is executed.

I created a script to be executed 3 minutes after the rc.local is run. This has worked perfectly for me with my 10.4.11 host.
If you don't want the email after booting, you can skip that if you wish.

% cat /etc/rc.local
at -f /scripts/addroute now + 3 minutes
%

% cat /scripts/addroute
#!/bin/csh -f
route -n add -net 172.16.1.0/24 192.168.1.128 | mail -s "add route after reboot on $HOST" admin@company.com
%


[ Reply to This | # ]