#!/bin/bash
# check-assn2.bash

# Author: Murray Saul
# Created: March 27, 2017   Updated: October 7, 2017

# Purpose: To generate data to be mailed to OPS335 instructor in order
#          to mark OPS335 assignment #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|bass|muskie|perch)"
then
  echo "You need to run your cloning-source \"minnow\", \"pike\", \"pickerel\", \"bass\", \"muskie\" and \"perch\" 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 #2 is being evaluated..."
echo "This make take a few minutes to complete..."


echo "Assignment 2 Evaluation Report - Date: $(date +'%A %B %d, %Y @ %H:%M:%S %P')" > a2-eval.txt
echo >> a2-eval.txt
# Email server evaluation
echo "----- EMAIL SERVER INFORMATION: -----" >> a2-eval.txt
echo >> a2-eval.txt
echo "bass POSTFIX MTA settings: " >> a2-eval.txt
ssh root@172.17.40.7 egrep "inet_interfaces" /etc/postfix/main.cf | grep -v "#" >> a2-eval.txt
ssh root@172.17.40.7 egrep "mydomain" /etc/postfix/main.cf | grep -v "#" >> a2-eval.txt
ssh root@172.17.40.7 egrep "myorigin" /etc/postfix/main.cf  | grep -v "#" >> a2-eval.txt
ssh root@172.17.40.7 egrep "relayhost" /etc/postfix/main.cf  | grep -v "#" >> a2-eval.txt
ssh root@172.17.40.7 egrep "mynetwork" /etc/postfix/main.cf  | grep -v "#" >> a2-eval.txt
ssh root@172.17.40.7 egrep "mailbox_command" /etc/postfix/main.cf  | grep -v "#" >> a2-eval.txt
echo >> a2-eval.txt
echo "POSTFIX MTA settings (muskie): " >> a2-eval.txt
ssh root@172.17.40.8 egrep "mydestination" /etc/postfix/main.cf  | grep -v "#" >> a2-eval.txt
echo >> a2-eval.txt
echo "MDA/LDA settings (muskie):" >> a2-eval.txt
echo "   /etc/dovecot/conf.d/10-mail.conf:" >> a2-eval.txt
ssh root@172.17.40.8 egrep "mail_location" /etc/dovecot/conf.d/10-mail.conf | grep -v "#" >> a2-eval.txt
echo >> a2-eval.txt
echo "LDA (AA - IMAP) settings (muskie):" >> a2-eval.txt
echo "   /etc/dovecot/conf.d/10-auth.conf:" >> a2-eval.txt
ssh root@172.17.40.8 egrep "disable_plaintext_auth" /etc/dovecot/conf.d/10-auth.conf  | grep -v "#" >> a2-eval.txt
echo >> a2-eval.txt
echo "   /etc/dovecot/conf.d/10-ssl.conf:" >> a2-eval.txt
ssh root@172.17.40.8 egrep "ssl" /etc/dovecot/conf.d/10-ssl.conf | grep -v "#" >> a2-eval.txt
echo >> a2-eval.txt
echo "DNS (MX) information:" >> a2-eval.txt
ssh root@172.17.40.3 cat /var/named/mydb-for-species.lake.ops >> a2-eval.txt
echo >> a2-eval.txt
echo >> a2-eval.txt

# Samba server evaluation

echo "----- SAMBA SERVER INFORMATION:----- " >> a2-eval.txt
echo >> a2-eval.txt
echo "SAMBA CONFIG FILE (perch):" >> a2-eval.txt
ssh root@172.17.40.9 cat /etc/samba/smb.conf >> a2-eval.txt

echo >> a2-eval.txt
echo "SAMBA USER ACCOUNTS (perch):" >> a2-eval.txt
ssh root@172.17.40.9 pdbedit -L -v >> a2-eval.txt

echo >> a2-eval.txt
echo >> a2-eval.txt

echo "FIREWALL POLICIES" >> a2-eval.txt
echo >> a2-eval.txt
echo "-------------------- IPTABLES STATUS SMTP (bass): --------------------" >> a2-eval.txt
ssh root@172.17.40.7 iptables -L >> a2-eval.txt
echo >> a2-eval.txt
echo "-------------------- IPTABLES STATUS IMAP (muskie): --------------------" >> a2-eval.txt
ssh root@172.17.40.8 iptables -L >> a2-eval.txt
echo >> a2-eval.txt
echo "-------------------- IPTABLES STATUS SAMBA (perch): --------------------" >> a2-eval.txt
ssh root@172.17.40.9 iptables -L >> a2-eval.txt
echo >> a2-eval.txt

# Less Deductions

echo >> a2-eval.txt
echo "LESS DEDUCTIONS" >> a2-eval.txt
echo >> a2-eval.txt
echo >> a2-eval.txt
echo "Correct hostnames:" >> a2-eval.txt
echo "   minnow hostname: $(ssh root@172.17.40.100 hostname)" >> a2-eval.txt
echo "   pike hostname: $(ssh root@172.17.40.3 hostname)" >> a2-eval.txt
echo "   pickerel hostname: $(ssh root@172.17.40.4 hostname)" >> a2-eval.txt
echo "   bass hostname: $(ssh root@172.17.40.7 hostname)" >> a2-eval.txt
echo "   muskie hostname: $(ssh root@172.17.40.8 hostname)" >> a2-eval.txt
echo "   perch hostname: $(ssh root@172.17.40.9 hostname)" >> a2-eval.txt
echo >> a2-eval.txt
echo "firewall status:" >> a2-eval.txt
echo "   minnow: $(ssh root@172.17.40.100 systemctl status iptables | egrep -i "(active|stop)")" >> a2-eval.txt
echo "   pike: $(ssh root@172.17.40.3 systemctl status iptables | egrep -i "(active|stop)")" >> a2-eval.txt
echo "   pickerel: $(ssh root@172.17.40.4 systemctl status iptables | egrep -i "(active|stop)")" >> a2-eval.txt
echo "   bass: $(ssh root@172.17.40.7 systemctl status iptables | egrep -i "(active|stop)")" >> a2-eval.txt
echo "   muskie: $(ssh root@172.17.40.8 systemctl status iptables | egrep -i "(active|stop)")" >> a2-eval.txt
echo "   perch: $(ssh root@172.17.40.9 systemctl status iptables | egrep -i "(active|stop)")" >> a2-eval.txt
echo >> a2-eval.txt
echo "yum update performed:" >> a2-eval.txt
echo "   minnow: $(ssh root@172.17.40.100 yum check-update && echo updated)" >> a2-eval.txt
echo "   pike: $(ssh root@172.17.40.3 yum check-update && echo updated)" >> a2-eval.txt
echo "   pickerel: $(ssh root@172.17.40.4 yum check-update && echo updated)" >> a2-eval.txt
echo "   bass: $(ssh root@172.17.40.7 yum check-update && echo updated)" >> a2-eval.txt
echo "   closet: $(ssh root@172.17.40.8 yum check-update && echo updated)" >> a2-eval.txt
echo "   perch: $(ssh root@172.17.40.9 yum check-update && echo updated)" >> a2-eval.txt
echo >> a2-eval.txt
echo "Services running:"  >> a2-eval.txt

echo "smtp status:" >> a2-eval.txt
echo "   bass (SMTP): $(ssh root@172.17.40.7 systemctl status postfix | egrep -i "(active|stop)")" >> a2-eval.txt
echo "   muskie (IMAP): $(ssh root@172.17.40.8 systemctl status dovecot | egrep -i "(active|stop)")" >> a2-eval.txt
echo "   perch (SAMBA): $(ssh root@172.17.40.9 systemctl status smb | egrep -i "(active|stop)")" >> a2-eval.txt

echo >> a2-eval.txt
echo "/etc/hosts NOT removed:" >> a2-eval.txt
ls  >> a2-eval.txt 2> /dev/null
echo "   minnow:" >> a2-eval.txt
ssh root@172.17.40.100 cat /etc/hosts  >> a2-eval.txt
ssh root@172.17.40.100 ls /home  >> a2-eval.txt 2> /dev/null
echo "   pike:" >> a2-eval.txt
ssh root@172.17.40.3 cat /etc/hosts  >> a2-eval.txt
ssh root@172.17.40.3 ls /home  >> a2-eval.txt 2> /dev/null
echo "   pickerel:" >> a2-eval.txt
ssh root@172.17.40.4 cat /etc/hosts  >> a2-eval.txt
ssh root@172.17.40.4 ls /home  >> a2-eval.txt 2> /dev/null
echo "   bass:" >> a2-eval.txt
ssh root@172.17.40.7 cat /etc/hosts  >> a2-eval.txt
ssh root@172.17.40.7 ls /home  >> a2-eval.txt 2> /dev/null
echo "   muskie:" >> a2-eval.txt
ssh root@172.17.40.8 cat /etc/hosts  >> a2-eval.txt
ssh root@172.17.40.8 ls /home  >> a2-eval.txt 2> /dev/null
echo "   perch:" >> a2-eval.txt
ssh root@172.17.40.9 cat /etc/hosts  >> a2-eval.txt
ssh root@172.17.40.9 ls /home  >> a2-eval.txt 2> /dev/null
echo >> a2-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 2

+



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

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

cat <<+

Submission of OPS335 assignment 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.

+







