#!/bin/bash

# check-assn1-p2.bash

# Author: Murray Saul
# Created: February 17, 2017   Updated: May 11, 2018

# Purpose: To generate data to be mailed to OPS335 instructor in order
#          to mark second portion of OPS335 assignment #1 - Part 2

# Error-checking prior to running shell script
# Check that user is logged on as root

if [ `id -u` -ne 0 ]
then
  echo "You must run this shell script as root" >&2
  exit 1
fi

# Check that mailx application has been installed

if ! which mailx > /dev/null 2> /dev/null 
then
  echo "You need to run \"yum install mailx\" prior to running this shell script" >&2
  exit 1
fi

if ! virsh list | egrep -iqs "(minnow|pike|pickerel|vm1)"
then
  echo "You need to run your cloning-source \"minnow\", \"pike\", \"pickerel\", and \"vm1\" VMs" >&2
  exit 2
fi



# Prompt for username and Full name

read -p "Please enter YOUR FULL NAME: " fullName

read -p "Please enter YOUR SENECA LOGIN USERNAME: " userID



# Generate Evaluation Report

clear
echo "Your Assignment #1 - Part 2 is being evaluated..."
echo "This make take a few minutes to complete..."

echo "Assignment 1 (Part 2) Evaluation Report - Date: $(date +'%A %B %d, %Y @ %H:%M:%S %P')" > a1p2-eval.txt
echo >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo "MASTER DNS DETAILS" >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo "Master DNS Server network info: " >> a1p2-eval.txt
ssh root@172.17.40.3 cat /etc/sysconfig/network-scripts/ifcfg-eth0 >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo "Master DNS Server named.conf info:" >> a1p2-eval.txt
echo "   allow-transfer to pickerel only: $(ssh root@172.17.40.3 grep allow-transfer /etc/named.conf)" >> a1p2-eval.txt
echo "   allow-query: $(ssh root@172.17.40.3 grep allow-query /etc/named.conf)" >> a1p2-eval.txt
echo "   allow-recursion: $(ssh root@172.17.40.3 grep allow-recursion /etc/named.conf)" >> a1p2-eval.txt
echo "   Is master DNS server: $(ssh root@172.17.40.3 grep type.*master /etc/named.conf)" >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo "Master DNS Resource Record (forward):" >> a1p2-eval.txt
ssh root@172.17.40.3 cat /var/named/mydb-for-*.ops >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo "Master DNS Resource Record (reverse):" >> a1p2-eval.txt
ssh root@172.17.40.3 cat /var/named/mydb-for-172.45.* >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo >> a1p2-eval.txt


echo SECONDARY DNS DETAILS >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo "Slave DNS Server network info: " >> a1p2-eval.txt
ssh root@172.17.40.4 cat /etc/sysconfig/network-scripts/ifcfg-eth0 >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo "Secondary DNS Server named.conf info:" >> a1p2-eval.txt
echo "   allow-transfer to pickerel only: $(ssh root@172.17.40.4 grep allow-query /etc/named.conf)" >> a1p2-eval.txt
echo "   recursion status: $(ssh root@172.17.40.4 grep recursion /etc/named.conf)" >> a1p2-eval.txt
echo "   Is slave DNS server: $(ssh root@172.17.40.4 grep type.*slave /etc/named.conf)" >> a1p2-eval.txt



echo >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo "FIREWALL DETAILS" >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo "-------------------- IPTABLES STATUS MASTER DNS: --------------------" >> a1p2-eval.txt
ssh root@172.17.40.3 iptables -L >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo "-------------------- IPTABLES STATUS SECONDARY DNS: --------------------" >> a1p2-eval.txt
ssh root@172.17.40.4 iptables -L >> a1p2-eval.txt
echo >> a1p2-eval.txt



echo >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo "LESS DEDUCTIONS" >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo "Correct hostnames:" >> a1p2-eval.txt
echo "   minnow hostname: $(ssh root@172.17.40.100 hostname)" >> a1p2-eval.txt
echo "   pike hostname: $(ssh root@172.17.40.3 hostname)" >> a1p2-eval.txt
echo "   pickerel hostname: $(ssh root@172.17.40.4 hostname)" >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo "firewall status:" >> a1p2-eval.txt
echo "   minnow: $(ssh root@172.17.40.100 systemctl status iptables | egrep -i "(active|stop)")" >> a1p2-eval.txt
echo "   pike: $(ssh root@172.17.40.3 systemctl status iptables | egrep -i "(active|stop)")" >> a1p2-eval.txt
echo "   pickerel: $(ssh root@172.17.40.4 systemctl status iptables | egrep -i "(active|stop)")" >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo "yum update performed:" >> a1p2-eval.txt
echo "   minnow: $(ssh root@172.17.40.100 yum check-update && echo updated)" >> a1p2-eval.txt
echo "   pike: $(ssh root@172.17.40.3 yum check-update && echo updated)" >> a1p2-eval.txt
echo "   pickerel: $(ssh root@172.17.40.4 yum check-update && echo updated)" >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo "named status:" >> a1p2-eval.txt
echo "   pike: $(ssh root@172.17.40.3 systemctl status named | egrep -i "(active|stop)")" >> a1p2-eval.txt
echo "   pickerel: $(ssh root@172.17.40.4 systemctl status named | egrep -i "(active|stop)")" >> a1p2-eval.txt
echo >> a1p2-eval.txt
echo "/etc/hosts NOT removed:" >> a1p2-eval.txt
echo "   minnow:" >> a1p2-eval.txt
ssh root@172.17.40.100 cat /etc/hosts  >> a1p2-eval.txt
echo "   pike:" >> a1p2-eval.txt
ssh root@172.17.40.3 cat /etc/hosts  >> a1p2-eval.txt
echo "   pickerel:" >> a1p2-eval.txt
ssh root@172.17.40.4 cat /etc/hosts  >> a1p2-eval.txt
echo >> a1p2-eval.txt

# Send report information to instructor

cat > message.txt <<+
If you have received this e-mail message, then
you have successfully submitted the remaining
information for your OPS335 assignment 1 (part 2)

+



mail -s "OPS335-a1p2-$fullName" colin.yip@senecacollege.ca < a1p2-eval.txt
mail -s "OPS335-a1p2-confirmation" "$userID@myseneca.ca"  < message.txt

rm  a1p2-eval.txt message.txt 2> /dev/null

cat <<+

Submission of OPS335 assignment 1 (part 2) completed.
A confirmation message should have been sent to your
Seneca email account. If you did NOT receive a confirmation
message, please check that you install the mailx package
or otherwise, contact your OPS335 instructor.

+







