shutdownHostViaSOAPAPICall.pl for ESX(i) licensed and free version

    This script was motivated by the blog post How to automatically shut down VMware ESXi gracefully during power failure using an APC UPS by Simon Seagrave with the work from James Pearce who demonstrated how to issue ShutdownHost_Task by issuing a SOAP request that emulated what the vSphere Client would  have sent to the ESXi host to shutdown. This was implemented in a set of  .bat scripts executed from a Windows system and can be tied into APC  UPS command or any UPS utility for that matter and this method actually  works on both licensed and free version of ESXi

    James was able to identify the SOAP request by Burp Suite sniffer, but this is nothing new as there's few other way to watch the vSphere Client traffic. Here are few others:

    Eric Sloof & Carter Shanklin - Using the vSphere Client
    LucD - Using Fiddler2

    You can also use tools like soapui that loads vSphere SDK WSDL and run a  sniffer to monitor local traffic to capture the transaction between the  vSphere Client and your hosts.

    At the end of the day, there's plenty of methods and all you really need  to understand is the exact SOAP requests that's being passed, in this  script, it will show you the exact setup that James has done with .bat  scripts but done using some SOAP libraries from Perl to make the code  much cleaner.

    Requirement:

    ESX(i) host w/running VMs
    vMA 4.0 or vSphere SDK for Perl (Windows or Linux)

    Process:

    • Connect, authenticate and retrieve valid cookie
    • Craft shutdown command, load cookie and set shutdown to your ESX(i) hosts
    • ESX(i) hosts shutdown, if VMs are part of startup/shutdown priority list, VMs will shutdown and then host will power down

     

    Usage:

    [vi-admin@scofield ~]$ ./shutdownHostViaSOAPAPICall.pl
    
    Usage: ./shutdownHostViaSOAPAPICall.pl [HOST_FILE]
    
    

     

     

    Sample Execution:

    1. Edit the script and fill in the $host_username and $host_password with the username/password set for your ESX(i) host:

    [vi-admin@scofield ~]$ vi shutdownHostViaSOAPAPICall.p
    

     

    2. Create a list of hosts to shutdown, the list will be processed in the order in which it's parsed:

    [vi-admin@scofield ~]$ cat hostlist
    esxi35u4.primp-industries.com
    esx4-1.primp-industries.com
    

     

    **** WARNING READ CAREFULLY -- THIS NEXT STEP WILL SHUTDOWN YOUR HOSTS -- WARNING READ CAREFULLY
    **** WARNING READ CAREFULLY -- THIS NEXT STEP WILL SHUTDOWN YOUR HOSTS -- WARNING READ CAREFULLY
    **** WARNING READ CAREFULLY -- THIS NEXT STEP WILL SHUTDOWN YOUR HOSTS -- WARNING READ CAREFULLY

    3. Execute the script and it'll start the shutdown process:
    shutdown.png

    If you watch your vSphere Client while this is happening, you'll see the  shutdown task is sent in immediately once it's been able to  authenticate and craft the shutdown response. Again, if you have setup a  priority startup/shutdown for your VMs, you will see the VMs power down  and then host shutting down afterwards.

    As you can see, it's pretty simple and you can easily tie this simple  script to be executed using any of the UPS utility software out there so  long as it can be hosted on a system that has vSphere SDK for Perl  installed or running on vMA. Here is another shutdown script using vMA  but requires ESX(i) host to be properly licensed: ESX/ESXi APC/APCUPSD Host Shutdown VI Perl Toolkit Script (ghettoShutdown.pl/upsVIShutdown.pl) -- [ DEPRECATED ], it should be trivial to implement something similar.

    Also note, this script works on both Linux or Windows, so long as you have vSphere SDK for Perl which is part of the vCLI installation