#!/bin/bash LOCATION=/data/cisco NUMOFRACK="1 2 3 4" TOTALRACKNUM=`echo $NUMOFRACK | tail -c -2` ########################################################################################################### #Sanity check on rack number and additional variable declaration. ########################################################################################################### if [ -z $2 ] then /bin/true elif [ $2 -gt $TOTALRACKNUM ] then echo "Invalid Rack identifier select. Please use one of the following: $NUMOFRACK" exit else RACKNUM=$2 RACKPID=`ps -ef | grep dyna | grep 720$RACKNUM | tr -s [:space:] " " | cut -d\ -f2` RACKUSER=`w -s -f | grep start\ $RACKNUM | tr -s [:space:] " " | cut -d\ -f1` fi ########################################################################################################### #Functions are declared here ########################################################################################################### RACKLOG () { TIME="`date +%R--%d-%m-%y`" case $1 in start ) echo "$TIME $USER started rack $RACKNUM" >> /data/cisco/logs/usage.log ;; stop ) echo "$TIME $USER stopped rack $RACKNUM" >> /data/cisco/logs/usage.log ;; esac } RACKSTART () { $LOCATION/dynamips-0.2.8-RC2-x86.bin -H 720$RACKNUM -l $LOCATION/$USER/dynamips.log & echo "Starting Rack $RACKNUM and loading Topology please wait." RACKLOG start sleep 5 $LOCATION/dynagen/dynagen $LOCATION/$USER.net } RACKKILL () { DYNAPID=`ps -ef | grep dyna | grep 720$RACKNUM | tr -s [:space:] " " | cut -d\ -f2` kill $DYNAPID 2> /dev/null && echo "Rack $RACKNUM has been stopped" RACKLOG stop } RACKRENUM () { if [ -e $LOCATION/$USER.net ] then echo "Reconfiguring your topology." else echo "It appears this is your first time using the rack. Please stand by while your topology is built." cp $LOCATION/template.net $LOCATION/$USER.net mkdir $LOCATION/$USER && chmod -R 755 $LOCATION/$USER chmod 644 $LOCATION/$USER.net fi mv $LOCATION/$USER.net $LOCATION/$USER.net.back sed -e "/\[localhost/s/720[1234]/720$RACKNUM/" -e "/udp/s/2[1234]/2$RACKNUM/" -e "/workingdir/s/template/$USER/" -e "/console/s/2[1234]/2$RACKNUM/" $LOCATION/$USER.net.back > $LOCATION/$USER.net rm $USER.net.back 2> /dev/null } RACKLIST () { for i in $NUMOFRACK do if [ -z `ps -ef | grep dyna | grep 720$i | tr -s [:space:] " " | cut -d\ -f2` ] then echo Rack $i is available for use. else STATUS="currently running" RACKUSER=`w -s -f | grep start\ $i | tr -s [:space:] " " | cut -d\ -f1` if [ -z $RACKUSER ] then echo "Rack $i is current running but does not seem to be in use." else echo "Rack $i is currenty running and in use by $RACKUSER" fi fi done } ########################################################################################################### #Control portion of script ########################################################################################################### case "$1" in start ) if [ -z $RACKPID ] then RACKRENUM RACKSTART RACKKILL else if [ -z $RACKUSER ] then echo "Rack $RACKNUM is current running but does not seem to be in use.Please try to stop this rack first, Then start it again." else echo "The rack you have selected is currently in use. Please select another rack from the following list:" RACKLIST fi fi ;; stop ) if [ -z $RACKPID ] then echo "$RACKNUM is not currently running, so cannot stop it." else RACKKILL fi ;; list ) RACKLIST ;; * ) echo "Please specify a valid action, (start, stop or list). Start and Stop need to be followed by a rack number" esac