#!/bin/bash
# 
# by: Joshua M. Hoffman (20100927)
#
# Grading script for the OSHU case study
# this script is to be installed and run on desktopX.example.com

# Set environment variables and globals
. /usr/local/lib/labtool.shlib
trap on_exit EXIT

DEBUG=false
PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin
SERVERX_VM="vserver"

# Check to make sure we are running as root
check_root

# Make sure we are running on desktopX
check_host "desktop"

# Determine the value of X (as in serverX)
X="$(ifconfig | grep 'inet addr:192.168.0.' | cut -d: -f2 | awk '{ print $1 }' | cut -d. -f4)"
serverX_IP="192.168.0.$[ 100 + $X ]"

# this check is unneccesary and breaks VT delivery. --bowe
#
# # verify that serverX is running
# virsh dominfo $SERVERX_VM &> log
# 
# if [[ "$?" -gt "0" ]]; then
#     echo "serverX is not running and cannot be checked"
#     echo "Please check your configuration and try again"
#     exit
# fi

# ping serverX (should fail)
if ping -c2 $serverX_IP &>log ; then
    echo "serverX is responding to pings, please check your configuration and try again"
    exit
else
    echo "serverX is ignoring pings = PASS"
fi

# get mac of serverX, fails if serverX has more than one nic!
serverX_mac="$(virsh dumpxml $SERVERX_VM | grep -o '..:..:..:..:..:..')"

# check if mac of 10.42.10.X matches
serverX_alias_ip="10.42.10.$X"
# Before ping, need to add an alias of the 10.42.10 to the desktopX
# machine.   Lab is blocking pings, so ping is not good.  Maybe a half open
# ssh connection?
ip addr add 10.42.10.$[ 100 + $X ]/24 dev br0 &>log
check_tcp_port $serverX_alias_ip 22 &>log
#ping -c1 $serverX_alias_ip &>log 
serverX_alias_ip_mac="$(arp -a $serverX_alias_ip | grep -o '..:..:..:..:..:..')"

if [[ "$serverX_mac" == "$serverX_alias_ip_mac" ]]; then
    echo "serverX has an alias ip on the 10.42.10.0/24 network = PASS"
else
    echo "serverX does not have an alias ip on the 10.42.10.0/24 network"
    echo "Please check your configuration and try again."
fi

# Reset IP alias
ifdown br0
ifup br0

exit
