Remote shutdown script for Linux/Windows

I’ve been writing a lot of bash scripts recently to make my life a little easier. I have 15 VMs in my homelab running on ESXi. I had to shutdown my server recently to do some maintenance (i.e. adding more ram as it was running out and I didn’t particularly want to do SSD caching for the host) and had to manually shutdown all of my VMs. I started doing this and realised that there must be a better way of doing this and it turns out there is.

I’ve created a shutdown script on my Ubuntu box with ssh-pass. I am aware this is not best practice, it’s fine for my homelab.

So, here is the script!

#!/bin/bash

declare -a servers

# Declare the array of servers in the format of:
# ip/DNS_name;USERNAME;PASSWORD

Notice the semi-colon between each type

servers[0]='asterisk;root;PASSWORD'
servers[1]='10.0.1.10;user;PASSWORD'
servers[2]='merlin;user;PASSWORD'
# etc etc etc

# Define some colours
red="\e[31m"
end="\e[0m"
green="\e[32m"
bold="\e[1m"
normal="\e[0m"

# Loop through all of the servers
for i in "${servers[@]}"
do
    arr=(${i//;/ })
    # easy access variables...
    ip=${arr[0]}
    user=${arr[1]}
    pass=${arr[2]}

    # Is the server responding to a ping? Attempt to login
    if ping -w 2 -c 1 $ip > /dev/null
    then
    command="echo -e '${pass}' | sudo -S shutdown -h 0"
        output=$(sshpass -p "${pass}" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 ${user}@${ip} ${command} 2>&1)

        if [ "$output" == "[sudo] password for ${user}:" ] || [[ "$output" == "[sudo] password for ${user}: Shutdown scheduled"* ]]
        then
            echo -e "${green}Sent shutdown command to ${bold}${ip}${end}"
        else
            echo -e "${red}Failed to shutdown ${bold}${ip}${normal}${red}"
            echo -e "SSH ouput:${end} ${output}"
            echo ""
        fi
    else
        echo -e "${red}${bold}${ip}${end}${red} is not responding to a ping - not attempting to shutdown the host${end}"
    fi
done

# Custom shutdown script stuff
# Shutdown windows host
# Note: I've wrapped the things you need to change in curly braces, you don't need to include these in your script
bullfinch=$(net rpc -S {windows_ip_or_dns_name} -U {user}%{PASSWORD}' shutdown -t 1 -f 2>&1)

if [[ "$bullfinch" == *"Shutdown of remote machine succeeded"* ]]
then
    echo -e "${green}Bullfinch shut down successfully${end}"
else
    echo -e "${red}Bullfinch failed to be shutdown${end}"
fi

echo "now shutting down this host"
echo -e 'PASSWORD' | sudo -S shutdown -h 0
echo ""
echo -e "${green}Hosts shutdown. Don't forget to shutdown Open-e then the ESX host"