#!/bin/bash # Generate interface files for a common topology # # Example usage: # vn-generateinterfaces --topology cs --hosts 10 # creates a client/server topology with 10 clients, 1 router and 1 server # # vn-generateinterfaces --topology dumbbell --hosts 5 # creates a dumbbell topology with 5 hosts, 1 router, 5 hosts # # Assumptions: # # Topologies: # lan: n hosts # cs: n hosts --- 1 router --- 1 server # dumbbell: n hosts --- 1 router --- n hosts # Default values topname="lan" topnum="99" numhosts=1 outdir="./" # Script parameters # Base IP addresses for each subnet # Currently only 2 subnets supported and must be /24 # Assumes roouter interfaces are both .1 neta_networkbase="192.168.1." neta_hostbase=10 netb_networkbase="192.168.2." netb_hostbase=20 netmask="255.255.255.0" # Read input parameters while [ "$1" != "" ]; do case $1 in -t | --topology ) shift; topname=$1; ;; -h | --hosts ) shift; numhosts=$1; ;; -n | --number ) shift; topnum=$1; ;; -o | --out ) shift; outdir=$1; ;; * ) usage exit 1 esac shift done # Node number for router # Client server if [ "${topname}" = "cs" ]; then nodemax=$(( ${numhosts} + 2 )); routernode=$(( ${numhosts} + 1 )); # Dumbbell elif [ "${topname}" = "dumbbell" ]; then nodemax=$(( ${numhosts} * 2 + 1 )); routernode=$(( ${numhosts} + 1 )); # LAN else nodemax=${numhosts}; routernode=0; # No router fi side="left" netaddress="${neta_networkbase}0" bcaddress="${neta_networkbase}255" hostbase=${neta_hostbase}; networkbase=${neta_networkbase} vboxnet="neta" # Create left and right side nodes for (( node=1; node<=${nodemax}; node++ )); do noden=${node} if [ ${noden} -lt 10 ]; then noden="0${noden}"; fi nodeiffile="${outdir}/interfaces-topology-${topnum}-node-${noden}" iphostaddr=$(( ${hostbase} + ${node} )); ipaddress="${networkbase}${iphostaddr}" if [ ${node} -eq ${routernode} ]; then ipaddress="${neta_networkbase}1" fi echo "# Interface file auto-generated by vn-generateinterfaces" > ${nodeiffile} echo "auto lo" >> ${nodeiffile} echo "iface lo inet loopback" >> ${nodeiffile} echo " " >> ${nodeiffile} echo "auto eth0" >> ${nodeiffile} echo "iface eth0 inet dhcp" >> ${nodeiffile} echo " " >> ${nodeiffile} echo "# VBoxNetwork: ${vboxnet}" >> ${nodeiffile} echo "auto eth1" >> ${nodeiffile} echo "iface eth1 inet static" >> ${nodeiffile} echo -e "\taddress ${ipaddress}" >> ${nodeiffile} echo -e "\tnetmask ${netmask}" >> ${nodeiffile} echo -e "\tnetwork ${netaddress}" >> ${nodeiffile} echo -e "\tbroadcast ${bcaddress}" >> ${nodeiffile} # Router node if [ ${node} -eq ${routernode} ]; then netaddress="${netb_networkbase}0" bcaddress="${netb_networkbase}255" hostbase=$(( ${netb_hostbase} - ${numhosts} - 1 )); networkbase=${netb_networkbase} vboxnet="netb" ipaddress="${netb_networkbase}1" side="right" echo " " >> ${nodeiffile} echo "# VBoxNetwork: ${vboxnet}" >> ${nodeiffile} echo "auto eth2" >> ${nodeiffile} echo "iface eth2 inet static" >> ${nodeiffile} echo -e "\taddress ${ipaddress}" >> ${nodeiffile} echo -e "\tnetmask ${netmask}" >> ${nodeiffile} echo -e "\tnetwork ${netaddress}" >> ${nodeiffile} echo -e "\tbroadcast ${bcaddress}" >> ${nodeiffile} else echo -e "\tpost-up route add -net 192.168.0.0 netmask 255.255.0.0 gw ${networkbase}1 dev eth1" >> ${nodeiffile} echo -e "\tpre-down route del -net 192.168.0.0 netmask 255.255.0.0 gw ${networkbase}1 dev eth1" >> ${nodeiffile} fi done