#!/bin/bash

# Defining text attributes
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
NC=$(tput sgr0) # Reset text attributes

# Defining the site codes and their respective names
sites=(
    "69 Alnwick"
    "2 Ashington"
    "3 BarnardCastle"
    "74 Berwick"
    "20 BishopAuckland"
    "6 Carlisle"
    "8 ChesterLeStreet"
    "71 Consett"
    "10 Cramlington"
    "62 Darlington"
    "13 Durham"
    "15 GatesheadTrinity"
    "17 GosforthHighStreet"
    "18 Hartlepool"
    "7 Hawes"
    "19 Hexham"
    "42 Knaresborough"
    "28 Middlesbrough"
    "29 Morpeth"
    "36 NorthShields"
    "75 NorthumberlandSt"
    "99 P&S"
    "72 Penrith"
    "73 Ponteland"
    "40 SouthShields"
    "41 Stokesley"
    "43 Sunderland"
    "45 WestDenton"
    "46 Whickham"
    "48 WhitleyBay"
    "12 Wooler"
    "5 Yarm"
)

# Defining subnet prefixes and their respective VLAN ids
subnets=(
    "10.222. Data 10"
    "10.223. Voice 20"
    "10.224. Other 30"
    "10.4. CCTV 150"
    "10.251. TCR 151"
)

ping_test() {
  site_code="$1"
  site_name="$2"
  subnet_prefix="$3"
  vlan_id="$4"
  subnet_name="$5"
  evx_names=("EVX 1" "EVX 2")

  for i in 0 1; do
    ip_to_test="${subnet_prefix}${site_code}.$(($i+2))"

    (ip vrf exec ccf6_01 ping -c 100 -i 0.01 "$ip_to_test" &>/dev/null && {
        printf "%-15s %-18s %-10s %s\n" "$ip_to_test" "$site_name" "VLAN:$vlan_id" "${evx_names[$i]} ${GREEN}SDWAN VLAN Online${NC}" >> $results_file
    } || {
        printf "%-15s %-18s %-10s %s\n" "$ip_to_test" "$site_name" "VLAN:$vlan_id" "${evx_names[$i]} ${RED}SDWAN VLAN Unreachable${NC}" >> $results_file
    }) &
  done
  
  # Wait for all launched background tasks to finish
  wait
}

results_file="test_results.txt"
> $results_file

echo
echo "Testing site VLAN connectivity, please wait 20-30 seconds...."
echo

for site in "${sites[@]}"; do
    site_code=$(echo "$site" | awk '{print $1}')
    site_name=$(echo "$site" | awk '{print $2}')

    for subnet in "${subnets[@]}"; do
        subnet_prefix=$(echo "$subnet" | awk '{print $1}')
        subnet_name=$(echo "$subnet" | awk '{print $2}')
        vlan_id=$(echo "$subnet" | awk '{print $3}')

        ping_test "$site_code" "$site_name" "$subnet_prefix" "$vlan_id" "$subnet_name" &
    done
done

# Wait for all launched pings to finish
wait

# Display the sorted results
sort -k2 $results_file

echo
echo "Pinging DHCP Relays"
(ip vrf exec ccf6_01 ping -c 4 192.168.159.50 &>/dev/null && printf "${GREEN}DHCP Relay 1 - Online${NC}\n" || printf "${RED}DHCP Relay 1 - Unreachable${NC}\n") &
(ip vrf exec ccf6_01 ping -c 4 192.168.149.150 &>/dev/null && printf "${GREEN}DHCP Relay 2 - Online${NC}\n" || printf "${RED}DHCP Relay 2 - Unreachable${NC}\n") &

# Wait for all launched pings to finish
wait

echo
echo "Public IP Test:"
public_ip=$(ip vrf exec ccf6_01 curl -s ifconfig.co/ip)
if [ -z "$public_ip" ]; then
echo "${RED}Public IP Test - Unsuccessful${NC}"
else
echo "${GREEN}Public IP is $public_ip${NC}"
fi

echo
echo "Breakout Test:"
(ip vrf exec ccf6_01 ping -c 4 1.1.1.1 &>/dev/null && printf "${GREEN}Breakout Test - Successful${NC}\n" || printf "${RED}Breakout Test - Unsuccessful${NC}\n")

# Wait for the breakout test to finish
wait

exit 0
