Virtual Firewall Booting and Shutdown with VMware Automation

Introduction

VMware Automation has always piqued my interest, but I lacked a personal project to experiment with until I encountered a significant problem. The issue at hand was the excessive heat generated by my servers and the subsequent high consumption of electricity. While I initially mitigated the problem by installing external fans on the server rack, I knew that this was not a complete solution.

After a period of time, a lightbulb moment occurred. Why not automate the process of shutting down the servers when I leave the office and automatically starting them up when I return? This idea stemmed from the fact that I had network appliances, such as firewalls, running on these servers to control the office network. It made sense to power down this network when it was not needed and activate it on demand.

Motivated by this vision, I embarked on a personal project to leverage the power of VMware Automation. In this article, I will share a sample automation workflow that showcases the immense capabilities of VMware Automation in addressing network management challenges and achieving optimal resource utilization.

Through this technical article, we will dive into the intricacies of VMware Automation, exploring how it can empower administrators to seamlessly control and optimize their IT infrastructure. The sample automation provided will serve as a practical demonstration of how VMware Automation can revolutionize network management, enabling administrators to achieve greater efficiency, cost savings, and flexibility.

Let’s dive in and discover the wonders of VMware Automation in network management!

Automation Script

@echo off
d:
cd D:\Programs\putty
echo "###Disable DHCP Mode & Apply Static IP###"
netsh interface ipv4 set address name="Ethernet" static 192.168.x.x 255.255.x.x 192.168.x.x
timeout 5
netsh int ip reset
timeout 5
echo "###Disable Maintenance Mode & Boot Network###"
plink.exe -ssh <user>@192.168.x.x -pw <password> -batch esxcli system maintenanceMode set --enable false

for /f "skip=1 tokens=1" %%i in ('plink.exe -ssh <user>@192.168.x.x -pw <password> -batch vim-cmd vmsvc/getallvms') do (
    echo %%i
    for /f "usebackq tokens=*" %%j in (`plink.exe -ssh <user>@192.168.x.x -pw <password> -batch vim-cmd vmsvc/power.getstate %%i`) do (
        echo %%j
        if "%%j"=="Powered off" (
            echo "###VM is powered off###"
            echo "###Execute VM Boot On###"
            plink.exe -ssh <user>@192.168.x.x -pw <password> -batch vim-cmd vmsvc/power.on %%i
        )
    )
)

echo "###Enable DHCP Mode###"
netsh interface ipv4 set address name="Ethernet" source=dhcp
echo "###Network is Activated, Check Connectivity with Google DNS.###"

:CHECK_CONNECTION
echo "Proceeding with traceroute..."
tracetcp 8.8.8.8 | find "Connection established to 8.8.8.8" > nul
if %errorlevel% neq 0 (
    echo "Tracing route to 8.8.8.8..."
    timeout /t 5 /nobreak
    goto :CHECK_CONNECTION
)

echo "Tracing complete."

:loop
set /p input=Enter "shutdown now" to shutdown network:
if /i "%input%"=="shutdown now" (
    goto continue
) else (
    goto loop
)

:continue
echo "###Start Shutdown Network###"

for /f "skip=1 tokens=1" %%i in ('plink.exe -ssh <user>@192.168.x.x -pw <password> -batch vim-cmd vmsvc/getallvms') do (
    echo %%i
    for /f "usebackq tokens=*" %%j in (`plink.exe -ssh <user>@192.168.x.x -pw <password> -batch vim-cmd vmsvc/power.getstate %%i`) do (
        echo %%j
        if "%%j"=="Powered on" (
            echo "###VM is powered on###"
            echo "###Execute VM Shutdown###"
            plink.exe -ssh <user>@192.168.x.x -pw <password> -batch vim-cmd vmsvc/power.shutdown %%i
        )
    )
)

timeout /t 5 /nobreak
echo "###Wait for VMs to Shutdown###"
plink.exe -ssh <user>@192.168.x.x -pw <password> -batch esxcli system maintenanceMode set --enable true

timeout /t 5 /nobreak
echo "###VMs Closed, Execute Shutdown###"
plink.exe -ssh <user>@192.168.x.x -pw <password> -batch esxcli system shutdown poweroff --reason "CloseOfficeNetwork"
Note: Please make sure to replace any sensitive information, such as IP addresses or passwords, with appropriate values before executing the script.

Explanation and Workflow

Now, let’s take a closer look at the automation script provided. This script leverages various commands and tools to achieve automatic network booting and shutdown in a VMware environment. Let’s break down the script’s functionality step by step:

  1. Disable DHCP Mode & Apply Static IP:
    This section of the script ensures that the network interface is configured with a static IP address. By using the netsh command, it sets the IP address, subnet mask, and default gateway values to the desired static values.
  2. Disable Maintenance Mode & Boot Network:
    In this part, the script utilizes the plink.exe command to establish an SSH connection with the VMware server. It then executes the esxcli system maintenanceMode set –enable false command to disable the maintenance mode and enable the network booting process.
  3. Check VM Power States and Boot:
    The script retrieves a list of all virtual machines using the vim-cmd vmsvc/getallvms command. It then iterates through each VM and checks its power state using the vim-cmd vmsvc/power.getstate command. If a VM is powered off, the script powers it on by executing the vim-cmd vmsvc/power.on command.
  4. Enable DHCP Mode:
    Once the network has successfully booted, the script restores DHCP mode using the netsh command, allowing the network interface to obtain an IP address automatically.
  5. Automatic Connectivity/User Command Checks:
    At this point, the script prompts the user to enter the command “shutdown now” to initiate the network shutdown process if the network confirmed is online using tracetcp. The script uses a loop to continuously prompt the user until the specified command is entered.
  6. Shutdown Network:
    Upon receiving the command “shutdown now,” the script proceeds to shut down the network. It follows a similar procedure as before, checking the power state of each VM and initiating a graceful shutdown using the vim-cmd vmsvc/power.shutdown command if the VM is powered on.
  7. Wait for VMs to Shutdown:
    After initiating the VM shutdown process, the script waits for a brief period using the timeout command to allow the VMs to complete the shutdown process gracefully.
  8. Enable Maintenance Mode:
    Once the VMs have shut down, the script enables the maintenance mode using the esxcli system maintenanceMode set –enable true command, ensuring the VMware server is in a controlled state.
  9. Execute Final Shutdown:
    Finally, the script executes the final shutdown of the VMware server using the esxcli system shutdown poweroff command. It includes a reason parameter, “CloseOfficeNetwork,” to provide context for the shutdown operation.

Live Video

Conclusion

By utilizing this automation script, administrators can effortlessly control the network booting and shutdown processes, resulting in improved efficiency, reduced energy consumption, and optimized resource utilization.

Thanks VMWare

Leave a comment