Re: [Q] Virtual hosts and routing...
On Fri, Jun 18, 1999 at 09:17:56AM -0400, David H. Silber wrote:
> # Set up a virtual interface.
> #
> # Usage:
> # virtualif <Interface> <IP number> <netmask> <broadcast> [reset]
> virtualif() {
I didn't like that my previous solution, because it only dealt reasonably
with one physical interface at a time. By making a virtual variable to
track the virtual interface for each physical interface, the virtualif()
function no longer requires that the administrator set up all of the
virtual interfaces for a given physical interface before setting up the
next physical interface. This allows the administrator who maintains
separate domains over the the same network to group entries in the
/etc/init.d/network file in a more logical manner. There is also no
longer a need to reset the counter manually.
#! /bin/sh
#
# virtualif() - Set up a virtual interface.
#
# Usage:
# virtualif <Interface> <IP number> <Netmask> <Broadcast>
#
# Example:
# virtualif eth0 192.168.246.1 255.255.255.128 192.168.246.127
# virtualif eth0 192.168.246.2 255.255.255.128 192.168.246.127
# virtualif eth0 192.168.246.3 255.255.255.128 192.168.246.127
# virtualif eth1 192.168.246.129 255.255.255.128 192.168.246.255
# virtualif eth1 192.168.246.130 255.255.255.128 192.168.246.255
# virtualif eth0 192.168.246.4 255.255.255.128 192.168.246.127
# virtualif eth0 192.168.246.5 255.255.255.128 192.168.246.127
# virtualif eth0 192.168.246.6 255.255.255.128 192.168.246.127
# virtualif eth1 192.168.246.131 255.255.255.128 192.168.246.255
# virtualif eth2 10.10.10.1 255.255.255.0 192.168.246.255
#
# virtualif() sets up a virtual network interface and adds a route so
# that local references to the IP can be routed. It keeps track of the
# next available virtual interface for each physical interface, so that
# you don't have to.
#
# Installation: Insert the virtualif() function at the top of the
# /etc/init.d/network file.
#
# $VCOUNTER contains the name of the counter of virtual interfaces for
# the current physical interface. This name is built from the string
# ``COUNTER'', concatenated with the name of the physical interface.
# By using eval, we can use this name as a variable. Since applying
# the name of a different physical interface to this scheme generates
# a different variable name, we can keep a separate counter for each
# physical interface.
#
# Version 1.1
#
# David H. Silber -- June 18, 1999
# dhs@orbits.com -- http://www.orbits.com/~dhs
#
# (I keep thinking that there ought to be a way to simplify this, but
# it escapes me. If you find a better way to do this, please send
# email to the address above.)
virtualif() {
INTERFACE=$1
IP=$2
NETMASK=$3
BROADCAST=$4
COUNTER='$COUNTER'
VCOUNTER=`echo COUNTER$INTERFACE`
if [ ! "`eval echo $\`echo $VCOUNTER\``" ]
then
eval "$VCOUNTER=0"
fi
# Build the virtual interface name string.
VIRTUAL=${INTERFACE}:`eval echo $\`echo $VCOUNTER\``
# The point of all of this is to:
echo "Creating virtual interface $VIRTUAL and local route for $IP."
/sbin/ifconfig ${VIRTUAL} ${IP} netmask ${NETMASK} broadcast ${BROADCAST}
/sbin/route add -host ${IP} ${VIRTUAL}
# Increment the virtual interface counter.
eval "$VCOUNTER=`/usr/bin/expr \`eval echo $\\\`echo $VCOUNTER\\\`\` + 1`"
}
--
David H. Silber -- http://www.orbits.com/~dhs/ -- dhs@orbits.com
For custom software, see: http://www.SilberSoft.com/
Palm OS / Linux Documentation: http://www.orbits.com/Palm/
Reply to: