Netkit A bash script I wrote to quick build the requested Netkit labs for the final of the Computer  Networks  Infrastuctures class last july. I’m really pleased with those exams where you can  separate concepts  from specific and likely-to-change knowledge that may become useless or  forgotten soon.  Especially if smart professors allow you to automate or give less emphasis to the  second part, which  should be anyway known, but focusing on the first.

This bash script implements an input driven lab generator for Netkit, the “poor man’s system to experiment computer networking” developed and mantained at Roma Tre University.
The script prompt the user with questions and expects answers in indicated formats (pretty easy to get from examples). At the end, if feed with correct input, it should build up a working lab with network configuration based on BGP and RIP as IGP. It doesn’t configure prefix-lists or route-maps but examples are provided under comments.
This may be really useful for students at the Computer Networks Infrastructures class since most of the labs requested in finals may be generated with this script in minutes (while you are given about an hour, so you got lots of time to set up more complex stuffs and to check them).

[ Download ]

#!/bin/bash
#
#########################################################
# Simple bash script to generate netkit labs structure.	#
# Author: Alessandro Manfredi                           #
#########################################################
#
echo;
echo '-----------------------------------------------------';
echo "Write down or sketch these data:"
echo "- collision domains names [ as uppercase letters ]"
echo "- addresses and netmasks for each network (DMZ included)"
echo "- ip address for each interface of each router"
echo '-----------------------------------------------------';
#
LAB_DESCRIPTION='Lab description goes here'
LAB_VERSION='Lab version'
LAB_AUTHOR='Your Full Name Here'
LAB_EMAIL='your@email.here'
LAB_FOLDER='netkit-labl-folder-here'
#
# I suppose you want at least 2 machines
#
if [ $# -lt 2 ];then echo;echo "Use: \$ $0  as10r1 as10r2 [ asXXrYY ... ]";echo;exit;fi
echo;
echo '-----------------------------------------------------';
echo "--- Starting Lab Generation to folder $LAB_FOLDER ";
echo '-----------------------------------------------------';
echo;
#
################# Create lab folder and init lab.conf file ##############################
#
mkdir $LAB_FOLDER; cd $LAB_FOLDER; touch lab.conf
echo "LAB_DESCRIPTION=\"$LAB_DESCRIPTION\""	>> lab.conf
echo "LAB_VERSION=\"$LAB_VERSION\""		>> lab.conf
echo "LAB_AUTHOR=\"$LAB_AUTHOR\""		>> lab.conf
echo "LAB_EMAIL=\"$LAB_EMAIL\""			>> lab.conf
echo '' >> lab.conf
#
################# For each router #######################################################
#
for i in $@; do
	echo;
	TEMP_AS_R=${i:2:${#i}};
	ASN=`echo $TEMP_AS_R | tr 'r' ' ' | awk '{print $1}'`;
	RTN=`echo $TEMP_AS_R | tr 'r' ' ' | awk '{print $2}'`;
	touch $i.startup;
	echo "---Starting $i configuration---";echo;
	echo -n "How many interfaces must have $i ? "; read ETHS;
	for j in `seq 0 $(($ETHS-1))`; do
		echo -n "Which will be the collision domain for $i.eth$j ? [enter a letter] "; read CDM;
		echo -n "Which will be the ip address for $i.eth$j ? [enter an ip address] "; read IPA;
		echo -n "Which will be the netmask for the collison domain $CDM ? [enter a netmask] "; read NTMT;
		case $NTMT in
			/30|30) NTM=255.255.255.252	;;
			/29|29) NTM=255.255.255.248	;;
			/28|28) NTM=255.255.255.240	;;
			/27|27) NTM=255.255.255.224	;;
			/26|26) NTM=255.255.255.192	;;
			/25|25) NTM=255.255.255.128	;;
			/24|24) NTM=255.255.255.0	;;
			/23|23) NTM=255.255.254.0 	;;
			/22|22) NTM=255.255.252.0 	;;
			/21|21) NTM=255.255.248.0 	;;
			/20|20) NTM=255.255.240.0 	;;
			/19|19) NTM=255.255.224.0 	;;
			/18|18) NTM=255.255.192.0 	;;
			/17|17) NTM=255.255.128.0 	;;
			/16|16) NTM=255.255.0.0		;;
			/15|15) NTM=255.254.0.0 	;;
			/14|14) NTM=255.252.0.0 	;;
			/13|13) NTM=255.248.0.0 	;;
			/12|12) NTM=255.240.0.0 	;;
			/11|11) NTM=255.224.0.0 	;;
			/10|10) NTM=255.192.0.0 	;;
			/9|9) 	NTM=255.128.0.0 	;;
			/8|8) 	NTM=255.0.0.0		;;
			/7|7) 	NTM=254.0.0.0 		;;
			/6|6) 	NTM=252.0.0.0 		;;
			/5|5) 	NTM=248.0.0.0 		;;
			/4|4) 	NTM=240.0.0.0 		;;
			/3|3) 	NTM=224.0.0.0 		;;
			/2|2) 	NTM=192.0.0.0 		;;
			/1|1) 	NTM=128.0.0.0 		;;
			*)  	NTM=$NTMT		;;
		esac
		echo;
		echo "---Setting $i.eth$j to $IPA $NTM on collision domain $CDM---";
		echo "$i[$j]=\"$CDM\"" >> lab.conf;
		echo "/sbin/ifconfig eth$j $IPA netmask $NTM up" >> $i.startup ;
		echo;echo "---end $i.eth$j configuration---";echo;
	done;
	echo "/etc/init.d/zebra start" >> $i.startup ; echo "" >> lab.conf;
	# Directory and Files for the router config
	mkdir $i; mkdir $i/etc; mkdir $i/etc/zebra; cd $i/etc/zebra;
	# Daemons file
	touch daemons; echo "zebra=yes" >> daemons;
	# RIP
	echo -n "Will $i have RIP Enabled ? [y/n] "; read RIP;
	case $RIP in
		y) echo "ripd=yes" >> daemons;;
		*) echo "ripd=no" >> daemons;;
	esac;
	# BGP
	echo -n "Will $i be a BGP router ? [y/n] "; read BGP;
	case $BGP in
		y) echo "bgpd=yes" >> daemons;;
		*) echo "bgpd=no" >> daemons;;
	esac;
	#
	echo;
	echo "---Also applying default settings: ospfd, ospfd6 and ripngd are disabled---";
	echo "ospfd=no" >> daemons;echo "ospf6d=no" >> daemons;echo "ripngd=no" >> daemons;
	#
	# BEGIN of BGP configuration
	#
	case $BGP in
	y)
		echo;
		echo "---Beginning of BGP configuration---";
		echo;
		touch bgpd.conf;
		echo "hostname $i-bgpd"					>> bgpd.conf
		echo "password zebra"					>> bgpd.conf
		echo "enable password zebra"				>> bgpd.conf
		echo "!"						>> bgpd.conf
		echo "router bgp $ASN"					>> bgpd.conf
		echo "!"						>> bgpd.conf
		echo -n "How many networks shall $i announce via BGP (dmz included, if needed) ? "; read NBGPN;
		for t in `seq 1 $NBGPN`; do
			echo -n "Enter network #$t as address/netmask [es 10.0.0.0/24] : "; read NT;
			echo "network $NT"				>> bgpd.conf
		done
		echo;
		echo "!"						>> bgpd.conf
		echo -n "How many BGP neighbor will $i have ? "; read NNEIGH;
		for n in `seq 1 $NNEIGH`; do
			echo -n "Enter neighbor #$n ip address : "	; read NIP;
			echo -n "Enter neighbor #$n as-number : "	; read NAS;
			echo -n "Enter neighbor #$n description : "	; read NDS;
			echo "neighbor $NIP remote-as $NAS"		>> bgpd.conf
			echo "neighbor $NIP description $NDS"		>> bgpd.conf
			echo "!"					>> bgpd.conf
			#
			####### Prototypes of prefix-lists and route-maps are commented ############
			#
			#echo "!neighbor $NIP prefix-list "	>> bgpd.conf
			#echo "!"							>> bgpd.conf
			#echo "!neighbor $NIP route-map  "		>> bgpd.conf
			#echo "!"							>> bgpd.conf
			#echo "!ip prefix-list permit a.b.c.d/n"		>> bgpd.conf
			#echo "!ip prefix-list deny any"			>> bgpd.conf
			#echo "!"							>> bgpd.conf
			#echo "!ip prefix-list deny a.b.c.d/n"		>> bgpd.conf
			#echo "!ip prefix-list permit any"		>> bgpd.conf
			#echo "!"							>> bgpd.conf
			#echo "!route-map  "				>> bgpd.conf
			#echo "!	match ip address "			>> bgpd.conf
			#echo "!	set metric "				>> bgpd.conf
			#echo "!	set local-preference "			>> bgpd.conf
			#echo "!	set as-path prepend   "		>> bgpd.conf
			#echo "!"							>> bgpd.conf
			#echo "!access-list  permit x.y.z.w/n"		>> bgpd.conf
			#echo "!"							>> bgpd.conf
                        #################################################################
			#
		done
		echo;
		echo "---End of BGP Configuration---"
		echo;
			;;
		*) 	;;
	esac;
	#
	# END of BGP configuration
	#
	# BEGIN of RIP configuration
	#
	case $RIP in
		y)
			echo;
			echo "---Beginning of RIP configuration---";
			echo;
			touch ripd.conf;
			echo "hostname $i-ripd" 	>> ripd.conf
			echo "password zebra"	 	>> ripd.conf
			echo "!"		 	>> ripd.conf
			echo "router rip"		>> ripd.conf
			echo "!"		 	>> ripd.conf
			RIPNET=N
			for k in `seq 0 $(($ETHS-1))`; do
				echo -n "Should $i 'talk' rip on interface eth$k ? [y/n] "; read RIPNET;
				case $RIPNET in
					y) echo "network eth$k"	>> ripd.conf ;;
				esac
			done
			echo "!"		 	>> ripd.conf
			echo -n "Should RIP redistribute connected ? [y/n] "; read RIPRDC;
			case $RIPRDC in
				y) echo "redistribute connected"	>> ripd.conf ;;
			esac
			if [ $BGP == "y" ];then
				echo -n "Should RIP redistribute bgp ? [y/n] "; read RIPRDBGP;
				case $RIPRDBGP in
					y) echo "redistribute bgp"		>> ripd.conf ;;
				esac
			fi
			echo "!"		 	>> ripd.conf
			echo;
			echo "---End of RIP configuration---"
			;;
		*) 	;;
	esac;
	#
	# END of RIP configuration
	#
	echo;
	#
	# Go back to the lab folder
	#
	cd ../../..
done
#
################# Structure Completed, Start with Report ##########################
#
echo;
echo '-----------------------------------------------------';
echo "Lab Structure Completed!";
echo '-----------------------------------------------------';
echo "Here is the tree of the lab folder $LAB_FOLDER :";echo;
find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g';echo;
echo '-----------------------------------------------------';
#
# Check lab.conf and startup files, show each interface on each collision domain
#
for i in {A..Z};do
	tail -n $((`wc -l lab.conf | awk '{print$ 1}'`-4)) lab.conf | egrep "\"$i\"" &>/dev/null
	case $? in
	0)	echo "On collion domain $i you defined the interfaces :"
		IFACES=$(tail -n $((`wc -l lab.conf | awk '{print$ 1}'`-4)) lab.conf | \
                egrep "\"$i\"" | egrep -o "^as[0-9]+r[0-9]+\[[0-9]\]")
		for j in $IFACES;do
			ASRT=`echo $j | tr '[' ' ' | awk '{print $1}'`
			IFNM=`echo $j | tr '[' ' ' | tr ']' ' ' | awk '{print $2}'`
			IFACE="$ASRT.eth$IFNM"
			IPADDR=`cat $ASRT.startup | grep eth$IFNM | awk '{print $3}'`
			IPMASK=`cat $ASRT.startup | grep eth$IFNM | awk '{print $5}'`
			echo "> $IFACE with ip-address $IPADDR and netmask $IPMASK";
		done
		echo;
	;;
	*);;
	esac
done
#
# Done
#
echo '-----------------------------------------------------';
echo 'Please check the configuration of bgpd and ripd files';
echo '-----------------------------------------------------';
echo 'This script comes with NO-RESPONSABILITY of the author';
echo 'have a nice lab =)                             *.n0on3';
echo '-----------------------------------------------------';
echo;echo;

Have a nice day =)






Bookmark and Share