Note: this script was tested in a pod environment and works in CentOS 5.2 - so this means it is likely to work on most red hat distributions (fedora, RHEL, CentOS) but has not been tested on fedora or RHEL.
First create a scripts directory in /
$ mkdir scripts
Now inside it create these files (works best by copying and pasting, I use vi so the command is:
$ vi /scripts/MAClist
(edit the MAC addresses as needed)
===============================================
#POD 1
#export P1W0= # Router's External Interface
#export P1W1=
#export P1W2=
#export P1W3=
#export P1W4=
#POD 2
export P2W0=00:16:76:35:81:38 # Router's External Interface
export P2W1=00:02:B3:1C:AE:CF # Internal - dhcp
export P2W2=00:16:76:35:83:3A # nfs
export P2W3=00:16:76:35:80:20 # dns
export P2W4=00:16:76:35:7E:66 # nis
#POD 3
#export P3W0= # Router's External Interface
#export P3W1=
#export P3W2=
#export P3W3=
#export P3W4=
===============================================
Next is the function file:
$ vi /scripts/functions
Located HERE with indentations
================================================
#!/bin/bash
getMAC() {
#
# this function will output all MAC addresses on the system
#
ifconfig -a | grep HWaddr | awk '{print $5}'
}
getInterface() {
# output all interface names (i.e. eth0, eth1) on seprate lines
iface=$(ifconfig -a | grep $thisMAC | awk '{print $1}')
}
isMAC() {
thisMAC=`getMAC`
#
# this function can only handle system with 1 NIC correctly
#
REQ=`echo $1 | tr '[a-z]' '[A-Z]'`
if [ "$REQ" = "${thisMAC}" ]
then
return 0
else
return 1
fi
}
existMAC() {
allMAC=`getMAC` # get all MAC in this system
#
# this function can handle system with more than one MAC address
#
iMAC=`echo $1 | tr '[a-z]' '[A-Z]'` # look for this MAC address
if [ "$iMAC" != "" ]
then
rMAC=`echo "$allMAC" | grep "$iMAC"`
if [ "$rMAC" = "$iMAC" ]
then
return 0 # do exist
else
return 1 # do not exist
fi
else
return 1 # non-exist MAC
fi
}
getDevName() {
MAC=$1 # to find out what device name is assign to this MAC
if [ "$MAC" = "" ]
then
: # OR echo need a MAC address
else
DevName=$(/sbin/ifconfig -a | grep HWaddr | grep $MAC | awk '{print $1}')
if [ "$DevName" = "" ]
then
: # OR echo No such MAC address
else
echo $DevName
fi
fi
}
================================================
And finally the main script:
$ vi /script/network.bash
Located HERE with indentations
================================================
#!/bin/bash
[ -f /scripts/functions ] && . /scripts/network/functions
#load getMAC(), isMAC(), existMAC(), and getDevName() functions
[ -f /scripts/MAClist ] && . /scripts/network/MAClist
# the file MAClist contains list of MAC addresses and which system they belong
# e.g. P1W0=00:16:2A:03:AB:1F:34 - external NIC interface in router on POD 1
# e.g. P1W1=00:39:00:32:23:FD:09 - internal NIC interface in router on POD 1
# e.g. P1W2=00:39:00:11:32:CC:10 - internal NIC interface in host 2 on POD 1
allMAC=`getMAC`
for thisMAC in $allMAC
do
for POD in 1 2 3 4 5 6 7 8 9 10
do
for WS in 0 1 2 3 4
do
A=P${POD}W${WS}
B=$(eval echo \$$A)
# find interfaces on machine
iface=$(ifconfig -a | grep $thisMAC | awk '{print $1}')
# set nis domain name
domainname nis.pod$POD.com
if [ "$thisMAC" = "$B" ]
then
if [ ${WS} -eq 0 ]
then
# WS 0 -> external interface
# get IP from Lab's DHCP server for the external interface
# setup NAT for the external interface
iptables -t nat -A POSTROUTING -o $iface -j MASQUERADE
# setup IP fowarding
echo 1 > /proc/sys/net/ipv4/ip_forward
echo
echo This is WS 0 \(Router\)
elif [ ${WS} -eq 1 ]
then
ifconfig $iface 172.16.$POD.$WS netmask 255.255.255.0 broadcast 172.16.$POD.255 up
echo This is WS 1
else
# WS 1,2,3,4 -> internal interface
# network 172.16.P.1 - 172.16.P.254 for POD P
ifconfig $iface 172.16.$POD.$WS netmask 255.255.255.0 broadcast 172.16.$POD.255 up
# assign default gateway for system with only 1 NIC
route add default gw 172.16.$POD.1
# set whatever you need for this NIC with thisMAC
echo This is WS 2-4
fi
echo "NIC on POD $POD WS $WS matched this MAC - $thisMAC" on interface $iface
echo
fi
done
done
done
================================================
Test your scripts by running the final script:
$ . /scripts/network.bash
May need to set permissions:
$ chmod 755 /scripts/*
If you find anything interesting or missing something from this tutorial feel free to leave a comment.
No comments:
Post a Comment