TechHead

Virtualization blog also covering Cloud, Windows & Tech how-to

  • Home
  • Blog Categories
    • VMware
      • VMware “How To” Posts
      • VMware Troubleshooting
      • VMware Workstation & Fusion
      • VMware Backup & Replication
      • VMware vCloud Director
      • VMware Certification
      • VMware Horizon
      • VMware PowerCLI
      • VMware Security
      • VMware Training
      • VMware User Group & Community
      • VMware View
      • VMworld
      • VMware Useful Links
    • EMC
      • Virtual Storage Appliance (VSA)
      • vSphere VSI Plugin
    • HP
      • Proliant Servers
    • Microsoft
      • MS Windows Server
      • MS Hyper-V
      • MS Exchange
      • MS SQL Server
      • MS PowerShell
    • General Tech
    • Cloud
    • Networking
    • Iomega
    • Apple
    • Cisco
    • Linux
    • Internet & Web
    • WordPress
  • About Me
  • Home Labs
  • VMware Tools
  • Crypto Links
  • Cool Tech Links
  • Recommended Podcasts
    • vChat Podcast Episodes
You are here: Home / VMware / How to automatically shut down VMware ESXi gracefully during power failure using an APC UPS.

How to automatically shut down VMware ESXi gracefully during power failure using an APC UPS.

10 December 2009 by JamesP 92 Comments

VMware ESX ESXi - How to shut down using APC UPSIn this latest post by TechHead guest contributor James Pearce he covers a topic near and dear to many of us – how to get VMware ESX/ESXi and its VMs to shut down gracefully upon power failure to the host.  Tighter integration between a UPS and a VMware ESX/ESXi host is no doubt something that will become more mature over time though for now it can be an issue for many administrators especially those running the free version of ESXi.  So read on to find out how James overcame this issue in his virtualization lab.

 

A Nearly Free UPS

APC UPSI recently acquired an APC Smart UPS that was being chucked out from work (having never worked) for my home lab along with an ancient AP9606 management card. With the batteries changed the UPS burst into life – but after some messing about getting the right firmware on it, I was disappointed to find no easy way to get it to shutdown my ESXi box when it needed to.

 

VMware License Restriction

The VMware management appliance (vima) can shutdown only paid-for installations of ESXi (using apcupsd and VMware community member lamw’s scripts) – the necessary interfaces on the free version have been made read-only since ESXi v3.5 U3.

 

Burp Suite

VMware ESX ESXi - How to shut down using APC UPS I’ve been finding a lot of use recently for network sniffers, so thought I’d have a look at how the VMware vSphere Client works, as obviously that can shut down the host. As luck would have it, the client is nothing more than a glorified web browser with the slight complication that it’s talking over SSL – but that’s no problem for PortSwigger’s Burp suite in its transparent proxy mode.

The traffic captures revealed that only three frames would be needed to perform the shutdown (hello, authenticate, and shutdown). A little manipulation is needed to get the session keys in, but that is basically it. ESXi’s startup and shutdown policy will do the work suspending or shutting down individual VMs, as configured through the vSphere Client.

 

The Script – shutdown.bat

Using this newly found knowledge I’ve created a Windows batch file (with a few supporting text files which are basically HTTP requests) that takes the hostname, username and password as parameters and will then shut down the host cleanly. The script needs something to launch it – APC PowerChute Network Shutdown in my case – and a utility to send the commands over SSL, for which I’ve used Nmap ncat (which just needs to be installed).

I have put all the necessary script files into a single convenient zipped file which you can download from here – the scripts are fairly well commented so you should be able to follow what is happening.

 

APC PowerChute

A potential issue is that APC’s PowerChute Network Shutdown utility will always shut down the Windows machine it’s running on. I’ve therefore used a separate Windows management VM to host PowerChute and my script, since I wanted everything else just suspended.

PowerChute has an option to ‘run this command’ but it’s limited to 8.3 paths and won’t accept command line parameters. A separate batch file is needed (poweroff.bat) that runs the shutdown script with the parameters – but that could shut down other ESXi boxes as well if required. Also the PowerChute service needs to be run as local Administrator as the default Local System account doesn’t have sufficient rights.

 

Testing the Scripts

VMware ESX ESXi - How to shut down using APC UPS

Download the ZIP and extract the files – I’ve assumed the package will be extracted to c:scriptsesxi; update the path in poweroff.bat otherwise. Also the hostname, username and password also need to be specified in poweroff.bat.

Next install and configure PowerChute (in particular change the service user account) and enter the script in the ‘run this command’ box – I also increased the time allowed, but in practice it runs in a few seconds.

VMware ESX ESXi - How to shut down using APC UPS

Some waiting around can be avoided when testing by setting the UPS low-battery duration as high as it will go – just remember to change it back.

Next open up vSphere Client from a real machine, pull the UPS plug and once the battery get’s down to the specified number of minutes remaining, the script should run and the tasks will appear in vSphere Client.  Shortly afterwards the VM used to launch the script will itself shutdown under the control of PowerChute!

 

In Summary

The complete set of files can be downloaded here, and nMap ncat installation for Windows from here. Then a UPS management application is needed, for APC Smart UPSs use PowerChute for Windows.

The shutdown script includes logging and should report most errors. Bear in mind though that once a host is shutdown, it probably won’t be restarted when utility power is restored.

Burp Suite is a handy utility to bypass device limitations by enabling the scripting of management tasks that are only usually available through a web interface. I’ve used it to build scripts to regularly reboot home-spec routers every couple of weeks to keep them stable, and to set the time on the APC AP9606 management card daily since it doesn’t support NTP – and here to build a UPS shutdown script for ESXi; functionality that should really be built into ESXi in the first place.

 

Technorati Tags: VMware,ESX,ESXi,APC,UPS,script,how to,shut down,vSphere

Filed Under: VMware Tagged With: esxi, script, shutdown, ups

Comments

  1. chris says

    10 December 2009 at 1:48 pm

    Clever.

    Reply
  2. James Pearce says

    11 December 2009 at 12:15 pm

    Update: vmware communities member William Lam (lamw) has used this to create a new Pearl script that provides this functionality in a neater form, although without the logging – check out his post on it at http://communities.vmware.com/docs/DOC-11623

    Reply
  3. Aben says

    14 December 2009 at 11:35 pm

    Hi James,

    Many thanks for the post. Well written and clear.

    Hope to see more clever tricks!

    Reply
  4. David says

    27 December 2009 at 3:05 am

    Hi,

    Great job James, I’ve been looking for this for some time.

    I’m having a problem where the guest/s don’t get time to shut down gracefully before the host powers down. This is testing the command only, I have taken the ups out of the equation for now.

    NB: Esxi 4 running sbs 2008 and openfiler on a HP ML115 G5 w/ 8GB RAM. (There is a vcenter 4 server in the mix, but I’m directing the shutdown cmd to the host itself).

    Anyone have any ideas/suggestions as to why I cannot shutdown the sbs guest gracefully?

    Thanks in advance.

    Reply
    • James Pearce says

      27 December 2009 at 7:42 pm

      Hi David

      Two things I’d suggest – first ensure vmware tools are installed in the guests. Second configure the ESXi startup / shutdown order to automatically suspend guests, openfiler starting first (and hence closing last).

      Lamw’s interpretation of this code will work against vcentre and there are other options too for licensed ESXi, like lamw’s other scripts.

      Thanks for reading!

      Reply
  5. Fred says

    22 January 2010 at 7:54 pm

    What to change if I only want my host to reboot ?

    Reply
    • James Pearce says

      4 February 2010 at 8:19 pm

      Hello, I assume that you really just want to script a host reboot, in which case the best bet is to use Burp to capture that request from the VI Client.

      Hope that helps!

      Reply
      • Fred says

        4 February 2010 at 8:23 pm

        Hi,

        I replaced “ShutdownHost_Task” with “RebootHost_Task” in the shutdown-3.txt file and it worked.

        I never think it will be so simple.

        Thank

        Reply
        • James Pearce says

          4 February 2010 at 8:31 pm

          Excellent – thanks for sharing this!

          Reply
  6. Federico says

    4 February 2010 at 6:57 pm

    Hi James,

    I am facing a similar situation like the one you describe.
    I have an ESXi 4.0 free licensed installation running some VMs.
    I have a UPS without a network management card. This UPS is connected against the host via USB or serial port. Need to shutdown VMs and finally the host in case of power outage.

    and need to avoid the following:

    1) license the ESXi 4.0
    2) buy a network management card
    3) buy APC software (not sure if cames for free when you buy network management card)

    Any ideas about how to do this?

    Any guide will be much appreciated

    Thanks
    Federico

    Reply
    • James Pearce says

      4 February 2010 at 8:30 pm

      Hi, to get a USB UPS working you will need either a physical host with a USB port (an old laptop maybe?), a network USB hub (http://www.digi.com/products/usb/anywhereusb.jsp#overview), or an ESXi server that supports vmDirectPath and pass through a port in the host.

      To be honest, if it is an APC Smart UPS I would definitely hunt out a cheap old AP9606 web management card (seem to be about £25 on eBay at the moment, but they do come up cheaper sometimes). It doesn’t matter what it was from (i.e. PDU, UPS, whatever) – the firmware I’ve linked to above can be installed to get it working with a UPS.

      The APC software is freely downloaded from apcc.com, or there are freeware UPS utilities as linked to by the lamw scripts that are more generic.

      Hope that helps!

      Reply
  7. Roberto says

    16 February 2010 at 11:56 am

    Thanks,
    excellent solution for a “strange” limitation on the free version. It works very well. Just for info (I’m not so far expert in VmWare management), if an ESXi machine is powered off with this script, are the hosted VMs gracefully closed before?
    TIA
    Regards
    Roberto

    Reply
    • James Pearce says

      16 February 2010 at 1:22 pm

      Hi Roberto,

      Thanks for the question.

      What happens to running VMs depends on how the auto startup/shutdown policy is configured through the vSphere Client. I set all my VMs to ‘suspend’, in a particular order, except the management VM running the APC software which is set to ‘shutdown’. Since the script just asks ESXi to shutdown, these policies are adhered to in the process.

      Hope that helps.
      James.

      Reply
      • Roberto says

        16 February 2010 at 1:44 pm

        James,
        Thanks a lot!
        All the VMs are configured now for “ShutDown Guest”.
        Best regards
        Roberto

        Reply
  8. Jordi says

    22 February 2010 at 3:53 pm

    First of all, congratulations for this post is excelent. Just a point when you talk about the local rights in the power chute service I belive if you put the path of ncat in the variable PATH of the global enviorment it will works in any account.

    Reply
    • James Pearce says

      24 February 2010 at 11:53 am

      Hi Jordi, glad you find it useful, and thanks for posting the permissions tip.

      Cheers,
      James.

      Reply
  9. Aleks Nikolic says

    2 March 2010 at 3:30 pm

    Brilliant article James!
    With regards to the SBS shutdown question above, you definetly need to configure the startup/shuwdown sequence. Put your SBS shutdown timeout at 5-10 minutes. If SBS is done after 4 minutes, ESXi will continue with the next vm. If you keep everything default, ESXi will use the default timeout to continue to the next vm. After the last poweroff command, ESXi will shutdown itself, even if your SBS is still waiting to shutdown!

    Reply
    • Nicola Tiana says

      8 April 2010 at 10:21 pm

      Many thanks James for Your excellent job.

      Nicola

      A note for non-english users: some small customization are needed inside the shutdown.bat file (in the CheckHost routine) according to the response messages given by ping in Your copy of windows (those messages are used to check if Your host is alive)

      Reply
      • James Pearce says

        9 April 2010 at 10:52 am

        Hi Tiana,

        I’m glad you found this useful, and many thanks for taking the time to post the localisation note!

        Thanks
        James.

        Reply
  10. Brad says

    17 April 2010 at 3:46 pm

    Hi James,

    Thanks for taking the time to share your work. I am trying to run this with ESXi 4 and 2008 64bit VM. The script works great when I run it from the command line but when it runs via the Powerchute during the VM shutdown I get the below error. I think it is a permissions issue but not sure. The APC service is running as a Domain Admin account for testing but still get the message below when it is executed via Powerchute. The script was added with the full path C:scriptsesxipoweroff.bat and set the time for script execution for 60 seconds. The script is called as you see the message in the shuudown.txt log file but only the VM shuts down. Any ideas?

    Shutdown.txt log:
    System shutdown called: Fri 04/16/2010 3:05:53.22
    ESXi Server did not accept connection attempt:
    Return code was (no-response-from-server).

    Thanks,
    Brad

    Reply
    • Brad says

      18 April 2010 at 9:30 pm

      Hi James,

      My issue is resolved with the help of Jordi’s comments.

      Once the Nmap/Ncat path was set in the system variables and restart of the server the script was able to execute under the credentials running the PowerChute service and shutdown the ESXi host.

      Thanks again,

      Brad

      Reply
      • James Pearce says

        19 April 2010 at 8:04 am

        Hi Brad, glad it’s sorted and sorry the delay in replying. I was pondering on this, wondering whether it was a path issue or Windows firewall getting in the way of things 🙂

        Thanks

        Reply
  11. ws2000 says

    1 May 2010 at 5:10 pm

    Hello

    I just wanted to verify a few details. This process should work with any ( new or older ) APC Smart UPS and a AP9606 management card ? Are the newer APC Smart UPS still compatible with the AP9606 management card ?

    I have a HP Proliant 150 G5 and all I am really looking for is enough battery uptime to gracefully shutdown 3 VMs. Can anyone recommnned a APC Smart UPS that would fit those minimal requirements?

    Thank you in advance.

    Reply
    • James Pearce says

      5 May 2010 at 11:42 am

      Hi, sorry for the delay in picking up this query.

      The process is driven from APC’s PowerChute software. With v2.2.4 I’ve used both AP9606 and later AP9617 cards OK. The only requirement for the card is that it has the Smart-UPS firmware application (sumx.bin), as they can be flashed for several different devices.

      I’ve put a bit more detail on this at http://blog.peacon.co.uk/wiki/APC_Smart_UPS, but essentially all smart slot management cards are interchangeable as really it’s just a serial port, but the functionality is dependent on the firmware on the card, which varies by card type.

      For your server, it really depends on the wattage and your expected run time and shutdown time. My home-lab ML115G5 draws about 120W, the small 750 model in this article giving about 30 minutes with that load. I set the shutdown time at 11 minutes, to give plenty of time for all the VMs to suspend properly.

      Hope that helps!

      Reply
  12. Mehmood says

    23 June 2010 at 7:31 pm

    very nice post buddy

    cheers

    Reply
  13. odegos says

    26 June 2010 at 9:02 pm

    1. Install the Digi AnywhereUSB/2 device.

    – Configure a static IP Address

    – Connect the UPS USBcable

    2. Open SSH connection on the the ESXi host.

    – At the Host Terminal hit Alt-F1 to access the command prompt.

    – Type “unsupported” and hit enter.

    – Log in and edit /etc/inet.d.conf

    – Uncomment the line where you can see “#ssh”

    – Restart the host for changes to take effect.

    3. Install the UPS software on the Windows 2008 Guest.

    – We used the UPS Management Lite software that was shipped with the UPS unit.

    – Make all the services to run as the Administrator user. eg: it-admin.

    This is done by deleting the start-up registry entries (HLMSOFTWAREMICROSOFTWINDOWSCurrentVersionRUN) and creating a Windows Scheduler task(s) to run these processes at windows startup.

    4. Create a Windows batch file in root of C: and name it accordingly. eg: host_down.bat

    Add this line in the text file: plink.exe -ssh (ESXi-Server-IP-address) -l (username) -pw (password) “/sbin/poweroff”

    5. Download and install Putty’s Plink command utility and place it in root of C:.

    6. Run the batch file to initiate the once off Host Key acceptance prompt.

    7. Test the UPS with a power failure. You can open the VMware vSphere Client and look out for a “System auto off” event.

    Reply
  14. Mike says

    19 July 2010 at 8:48 pm

    Hi.

    Thanks for the great info!

    I have ESX servers. Will this work as well for ESX 4.o or are there edits?

    Thanks,

    Mike

    Reply
    • James Pearce says

      19 July 2010 at 9:00 pm

      Hi, yes this does work with ESX. But you also have other options – supported SSH console (see the posters comments about plink above) or William Lam’s ‘ghetto’ UPS shutdown scripts. I’ve also built this ‘original hack’ into an esxi scripting utility ‘esxi-control.pl’ which has shutdown capabilities: http://blog.peacon.co.uk/esxi-control-pl-script-vm-actions-on-free-licensed-esxi/.

      Hope one of those options helps 🙂

      Reply
  15. Kyrillos says

    14 September 2010 at 9:35 am

    Hey
    Great post..
    just a few questions that i have..
    i keep getting a message saying connection refused by server..
    can you help me on this??

    thank you

    Reply
  16. Kyrillos says

    14 September 2010 at 9:44 am

    Hey
    to add to my previous comment,
    in the poweroff bat file for the [hostname], do i put the Vm’s ip address or the server’s ip address hosting the VM?

    Thank you

    Reply
    • James Pearce says

      14 September 2010 at 11:51 am

      Hi, I’d go with IP addresses to be sure a name resolution issue won’t stop it working.

      I’ve since developed this into a more general esxi control script – imaginatively called esxi-control.pl – see http://blog.peacon.co.uk/wiki/Esxi-control.pl.

      Cheers, James.

      Reply
  17. Kyrillos says

    6 October 2010 at 8:31 am

    Hello again..
    i have managed to get everything working and the ESXI server does shut down. The only issue i have is that i found out that the VMs in the ESXI server have a delay time before shutting down. Unfortunatelly, the server itself will shut down regardless of whether the VMs have finished shutting down. Can you assist me on that matter.
    Do you know if the delay time for the host machine can be changed through the VSphere or some other place..

    THank you very much

    Reply
  18. Kyrillos says

    6 October 2010 at 8:52 am

    I was looking through that perl script you wrote and i was thinking.Maybe i can use that to shutdown all my VMs and then use the other technique described at the top to shut down my host.Can i run the script from a VM which will shutdown all the other VMs and then itself??

    Reply
    • James Pearce says

      6 October 2010 at 9:24 am

      Hi, yes you can use esxi-control.pl to do exactly that. If you’re using APC’s PowerChute, that will always shut down it’s host (itself a VM in my case) so esxi-control.pl need not shut that down. Once all VMs are stopped it is safe to let ESXi die with the power, so there is probably no need to shutdown the host in this case.

      Be sure the UPS has enough run-time to complete the shutdown process cleanly and bear in mind that the batteries will age (i.e. have a good margin of safety). Trying to suspend or shutdown lots of VMs at once will put a lot of strain on your disks, so it may be faster to run the shutdowns sequentially.

      The command for each VM would be:

      perl esxi-control.pl –server [esx-host-ip] –username root –password [password] –action shutdown –vmname [[NameOfVM]]

      List those down in the shutdown script, and remember to keep it updated!

      Re the initial problem, I’ve not seen that issue. Do you have vmware tools installed on all guests?

      Reply
      • Kyrillos says

        6 October 2010 at 10:01 am

        Thank you very much for your reply but can you assist me with the error message i get when running the script:

        Can’t locate VMware/VIRuntime.pm in @INC (@INC contains: C:/strawberry/perl/site
        /lib C:/strawberry/perl/vendor/lib C:/strawberry/perl/lib .) at Esxi-control.pl
        line 85.
        BEGIN failed–compilation aborted at Esxi-control.pl line 85.

        Thank you very much

        Reply
        • James Pearce says

          6 October 2010 at 10:09 am

          You need vmware’s vCLI installed.

          Reply
  19. Kyrillos says

    6 October 2010 at 10:17 am

    does the vmware vCLI run the script automatically?

    Reply
    • James Pearce says

      6 October 2010 at 10:46 am

      Hi, vCLI provides the perl environment with the vmware libraries and bits. It can be downloaded from vmware: http://www.vmware.com/support/developer/vcli/

      Tasks can be scheduled using the Windows task scheduler, typically to run a batch file containing the necessary commands. In the shutdown case, some other method will be needed to fire off the command, such as APC’s PowerChute.

      HTH

      Reply
      • Kyrillos says

        6 October 2010 at 11:08 am

        sorry again for bothering you but i installed vCLI and run the script but get the following:

        Server version unavailable at ‘https://localhost:443/sdk/vimService.wsdl’ at C:/
        Program Files (x86)/VMware/VMware vSphere CLI/Perl/lib/VMware/VICommon.pm line 5
        45, line 2.

        the password and username are the ones i used for the Vsphere client..is that correct?

        Reply
        • James Pearce says

          6 October 2010 at 11:20 am

          vCLI should be on the same box (or VM) as the scripts. Then the script should be run specifying the IP address of the ESX(i) host (same as used to connect through vSphere client), rather than the vCLI box.

          For example for esxi-control.pl from the command line on the box with vCLI running:

          perl esxi-control.pl –server 192.168.1.10 –username root –password letmein….

          where 192.168.1.10 is the IP address of the ESXi management interface.

          HTH!

          Reply
  20. Kyrillos says

    6 October 2010 at 10:59 am

    thank you!!!

    Reply
  21. Kyrillos says

    6 October 2010 at 11:23 am

    i got working now thank you very much!!
    Great job!!!

    Reply
  22. Kyrillos says

    8 October 2010 at 3:55 pm

    hello again..
    quick question

    I tested the perl script with the shutdown command for shutting down the VM on the ESXI server. what will you suggest in the case when there are processes running on the VM when the shutdown command is run?what happened in my case is that some VMs did not shut down because of windows messages asking “if user wants to stop/save work”..do you have anything to suggest for that so that all processes can be forced to shutdown..

    thank you

    Reply
    • James Pearce says

      12 November 2010 at 2:15 pm

      Hi Kyrillos, my script doesn’t consider the VMs, depending on the actions configured in the vSphere client for automatic host startup and shutdown. You could use ‘shutdown /f’ to force a shutdown of VMs in a set order though instead of shutting down ESXi (if nothing is running in my experience ESXi can be left to just die).

      Reply
  23. Juerg Thoeny says

    12 October 2010 at 12:59 pm

    Nice post, works well! On a non english system, you have to adapt the lines:
    REM Analyse the results and set environment variable hostconnectres:
    find “could not find host” test-connect.txt > not-found.txt
    find “Lost = 0” test-connect.txt > found.txt
    find “Lost = 1” test-connect.txt > time-out.txt

    In German it looks like:
    find “nicht finden” test-connect.txt > not-found.txt
    find “Verloren = 0” test-connect.txt > found.txt
    find “Verloren = 1” test-connect.txt > time-out.txt

    Reply
    • James Pearce says

      12 November 2010 at 2:17 pm

      Thanks for posting this which no doubt will help others. Also, the perl version won’t have this issue (search esxi-control.pl).

      Reply
  24. Josh Bickford says

    11 November 2010 at 4:16 pm

    For what it’s worth, I was having problems with the VMs not shutting down gracefully and found this fix from VMWare.

    http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1008182

    Thanks for the great help developing the script!

    Reply
  25. Simon Thomson says

    12 November 2010 at 8:28 am

    Thanks for this elegant solution to this issue, it’s a great help to us.

    I am interested in taking a look at the soap commands myself but at the moment the Burp suite will not see traffic from the vSphere client, normal browser traffic is visible. Any tips on hohw to configure Burp to see the traffic between the vSphere Client and the ESXi host?

    My concern is that in the future the soap commands may change so the scripts will need to be updated to continue to work.

    Cheers
    Simon

    Reply
    • James Pearce says

      12 November 2010 at 2:13 pm

      Hi Simon, it’s easiest to run Burp on another computer.

      Go to Proxy, Options, remove anything that’s there and enter 443 in the local listener port, check support invisible, redirect to host (ip of esxi host management interface), redirect to port 443, use self-signed, then click on add. Check running is ticked on the top left and that should start the listener. Then go to the intercept tab and set the intercept to off.

      You will also need to open 443 on the firewall of the box running burp.

      Once that’s all done you should be able to start vSphere client and enter the IP of the box running Burp as the host, and use the normal username and password.

      I’ve since wrapped up this and other functionality into a perl script which should be a bit more robust (although it needs vmware’s vCLI installed) – search for esxi-control.pl.

      Hope that helps.

      Reply
      • Simon Thomson says

        17 November 2010 at 4:41 am

        Ah, I was attempting to run Burp for the same machine as the vSphere client. I have it working now.
        Thanks so much for the tips and your work on this solution, it’s all very appreciated.

        Cheers
        Simon

        Reply
  26. lubin7 says

    24 November 2010 at 6:35 pm

    I have issue executing batch file from withing powerchute command line. Service is ran under local admin or domain admin and it will not execute any batch file. Anyone knows why? Spent 2 hours on phone with APC and they couldn’t find any answers. FYI, the low battery event triggered on the managment card shows up in powerchute, but again it doesn’t trigger the batch.

    Reply
    • James Pearce says

      24 November 2010 at 8:43 pm

      Hi, in my experience problems tend to be either that the path must be in 8.3 format or some kind of permissions or path issue. It might be worth adding “echo Started >log.txt” as the first line of the batch file, so you can be sure it is or isn’t running. HTH

      Reply
  27. Kim says

    7 January 2011 at 9:28 am

    Hi James.

    I’m looking at shutting down an ESXi box and a FreeNAS box.
    Do you see any issues or know of any that I may encounter by setting this up?

    I have a Powerware 5110 (I’ve had for a couple of years) I’d hoped to use for the FreeNAS, but haven’t been able to get FreeNAS to find the USB port the UPS is plugged into, so have kind of given up on that UPS. On top of that, it’s probably to small to use to shutdown vm guests.

    So I think the best option for me may be to find a second hand APC Smart-UPS 1000 or greater and fit it with a network management card.

    Thoughts?

    Reply
  28. saxa says

    16 March 2011 at 10:25 am

    Hi James,

    thank you for the awesome script.

    But I still have a question: doesn’t a “poweroff” command sent by ssh to the ESXi host do the same graceful shut down – assuming that the VMs are shut down gracefully by the host itself?

    Reply
    • James Pearce says

      19 March 2011 at 8:23 pm

      Hi, yes indeed SSH can be used. But having looked at the underlying SOAP communication, this opened lots of other possibilities – search online for ‘esxi-control.pl’.

      Reply
  29. abradanini says

    24 March 2011 at 9:38 am

    Great post. Someone has tested it on vSphere 4.1?

    Reply
    • James Pearce says

      24 March 2011 at 9:15 pm

      Hi, yes it will work, but this has been superceded by esxi-control. Cheers!

      Reply
  30. Kim says

    24 April 2011 at 8:05 am

    Hi James.

    You don’t seem to be issuing a maintenance mode command in amongst your (hello, authenticate, and shutdown)
    Is there a reason for this, or have I just missed it?
    It states here http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1013193

    “From vSphere Client
    Before shutting down or rebooting the ESX 4.0 or the ESXi 4.0 hosts, ensure that the hosts are put in maintenance mode.”

    Reply
    • James Pearce says

      24 April 2011 at 11:03 am

      Hi Kim

      You are right, the script doesn’t put the host in maintenance mode. I’m assuming that the controlling function (VM) will be running ON the ESXi host being controlled, hence maintenance mode is not considered (as it couldn’t be entered with the controlling VM running, and we’re relying on the auto start to wake up the VMs once power is restored).

      Also – the script is superceded by esxi-cotnrol.pl – Google on that for further info.

      Hope that helps!

      Reply
  31. Kim says

    24 April 2011 at 11:27 am

    I’ve obtained an ap9606 on your advice.
    Trouble is… I’m not sure of it’s subnet address, let alone it’s IP address.
    An arp -s [new ip] [existing mac] doesn’t help, as the ap9606 is obviously not on the same subnet.
    The lights on the card are both green, so it obviously already has an ip set.
    I don’t have a serial cable for the smart-ups, so Ethernet is my only option.
    I’m hoping nmap may be able to help me find the IP via a sweep for the MAC’s?
    Any other options anyone can think of?

    Thanks.

    Reply
    • James Pearce says

      25 April 2011 at 10:51 am

      Hi, the procedure is to find the card MAC address (see label on the card), then plug it in and let it boot. Next, reset the card (using a paper clip to press the reset button), then either use arp -s to create a static entry for it and access it via that IP, actually I think it also then has a default IP like 192.168.0.1 or 192.168.1.1 or 192.168.1.254 (sorry can’t recall exactly!).

      See page 10 on of the AP9606 manual (here) and some notes here.

      Another option is to use the configuration wizard, which should be downloadable from apc.com.

      Once you have telnet access, you’ll need to load the UPS firmware on it.

      Hope that helps!

      Reply
  32. Kim says

    26 April 2011 at 2:15 am

    Yeah, did all that.
    Problem is, the IP was already set and using a different subnet mask to my LAN.
    Can see the IP is set by the green light on the card.
    Without being on the same subnet as the AP9606 there is no way of knowing what the cards IP is.
    Not even nmap could tell me unless I knew the subnet.
    Re-assigning an IP using arp doesn’t take either.
    I plugged the AP9606 into my notebook and ran wireshark.
    This told me what the cards IP was. as the AP9606 sends a packet aprx every minute.
    I’ll be making a post of all this on my blog once I’ve completed everything.
    Once I found the IP, I just took a guess at the subnet mask it was /24.
    and assigned a temporary static IP and changed my notebooks subnet to match that of the cards.
    I could then telnet to the card and receive login prompt.
    An arp assignment still wouldn’t take though, so I’m guessing I need to log into the card to change it.
    Only problem is now… I don’t have the password, but that’s another problem I’m dealing with now.
    I contacted APC support for the backdoor credentials, and it looks like you need the special serial cable for this.
    Which is aprx $100NZ, a little to much for a single use cable.
    I requested the pin layout and was told that they make the cables, but did not have the layout information (not sure how they would make the cable without knowing the pin config).
    If I can’t find the credentials (non default (not apc/apc or User/apc)) then I still have options.
    Make up a serial cable with I believe…
    Pin#2 Female to Pin#2 Male,
    Pin#3 Female to Pin#1 Male,
    Pin#5 Female to Pin#9 Male,
    and find a computer with a com port.
    Layout info found here http://www.tek-tips.com/viewthread.cfm?qid=522564&page=1
    Hopefully it’s correct.
    That would cost next to nothing.
    Or…
    just crack the credentials with one of these http://sectools.org/crackers.html

    Reply
    • James Pearce says

      26 April 2011 at 6:11 am

      I don’t know if this applies to the AP9606 but it might be worth a go:

      “5.) Press the Reset button. Wait 5-7 seconds and the Status LED will flash rapidly while alternating between orange and green. Press the Reset button a second time immediately while the LED is rapidly flashing – which will cause the LED to then go off – in order to reset the User Name and Password to their defaults temporarily (the default User Name and password are both apc).”

      Reply
  33. Kim says

    26 April 2011 at 6:20 am

    Yeah, I saw that.
    I’m picking this only happens if the serial cable is plugged in.
    As the experience I’ve had… There is only green lights (mostly solid).
    When I reset the card, the light on the right (I think) goes out for a few seconds then it’s on solid again, that’s it.
    I did try the pressing the reset the second time while the light was out, with no apparent effect.
    If I have to resort to making up a serial cable, I’ll try this though.
    Cheers!

    Reply
  34. Kim says

    2 May 2011 at 12:38 am

    Ok, so hydra tells me the telnet user name is n/a and password is steven.
    I’m assuming somehow that means there isn’t a user name.
    Any idea how to get past the user name prompt, as it doesn’t allow a blank username?

    Reply
  35. Kim says

    3 May 2011 at 9:29 am

    Got past all this, with a db-9 equipped 3 core cable.
    Details will be in my blog post once done.

    Reply
  36. Ross Goodwin says

    14 July 2011 at 9:53 pm

    Thanks for the details James, your name has popped up a fair bit as I’m googling my way through ESXi management!!

    I find your posts great as the commands are step by step and you give syntax that I can adapt from my environment.

    The link to your esxi-control.pl script is returning nothing and a search on your site gives nothing by this name, is it still available?

    Cheers

    Reply
  37. FV says

    15 September 2011 at 10:39 am

    nothing less than brilliant.

    Reply
  38. jimwhite says

    26 September 2011 at 3:18 pm

    could you post a valid link for esxi-control.pl ??

    Reply
  39. Sloth says

    22 November 2011 at 10:23 pm

    Can’t say I’ve been able to find esxi-control.pl either, looks like it has disappeared from peacon’s site 🙁 Will give the original batch scripts a go later.

    Love your site btw, has been very helpful in getting ideas / sorting out my home esxi server so far 🙂

    Reply
  40. Oscar Suarez says

    8 February 2012 at 1:57 am

    Hi,

    Does this work with the new Vmware Vsphere ESXi 5? Has somebody tested it?

    Reply
  41. Oscar Suarez says

    8 February 2012 at 5:47 am

    Hi, has someone found new links for esxi-control.pl?

    Reply
  42. Sergey says

    1 March 2012 at 12:32 pm

    Thx a lot!
    Pleased to see this script!

    Reply
  43. John says

    9 March 2012 at 11:02 pm

    is there an easy way to call multiple boxes??

    I must be missing something.

    I’ve tried the following

    *****************************************************************
    @echo off
    c:scriptsesxishutdown ipaddress1 [username] [password]
    c:scriptsesxishutdown ipaddress2 [username] [password]
    ******************************************************************

    only the first line is completed, the script ends prior to the 2nd line being run. I’ve also tried with a timeout 30 between lines 2 and 3.

    any ideas??

    Thanks,

    John

    Reply
    • Matthias says

      14 May 2013 at 5:13 pm

      I’m having the same problem – the fist line executes, the second one does not.

      Is there any solution for this? Or do I need to set up a 2nd VM for that purpose?

      Thank you,

      Matthias

      Reply
  44. Jimbo says

    9 March 2012 at 11:10 pm

    Hi, try “call c:scripts….”, HTH!

    Reply
  45. John says

    10 March 2012 at 4:00 am

    Thanks,

    spent way too much time looking at the screen and trying to decide to go the Perl route or this one.

    This script works for me on 4.0, 4.1 and 5.0 so far.

    John

    Reply
  46. Bosco says

    30 April 2012 at 2:02 pm

    Hi!

    I try to use this script with my ESXi 4.1 and from Windows host (Windows 2008) on ESXi server scrupt works. But if a try to run this script from other Windows (XP SP3) machine, connected to same UPS (APC 1500) like ESXi hos tis, then i get this error, when i try to run poweroff.bat:

    C:scriptsesxi>poweroff.bat
    System shutdown called: pon 30.04.2012 15:57:42,92
    Unexpected call.

    What is wrong? I copy ncat.exe to %WINDIR%, also all files are located in default directory: c:scriptsesxi

    PLease, any hint would be apreciated?

    Thank you!

    Reply
  47. Fward says

    11 May 2012 at 2:31 pm

    Same problem as you Bosco, but I’m experimenting this issue in Windows Server 203

    Reply
  48. Alex says

    21 May 2012 at 2:12 pm

    Just install apcupsd on the esxi host if you are looking for a simple solution. Its a daemon for a USB/Serial/SNMP-connected APC that shuts down any Unix/Windows machine. However, you’ll still need some script to suspend the machines.

    Reply
  49. Jeff says

    24 May 2012 at 9:25 pm

    This is very handy. In my case it does shut down the ESXi host and vms but even if I enable the ““Turn off the UPS after the shutdown finishes” setting” it does not shut down the UPS. I also followed the steps here http://blog.rebelit.net/?p=118#comment-1175, same thing.

    Am I missing something, should I be able to shut the UPS down, or do I have to let the battery die?

    Reply
  50. Jeff says

    13 June 2012 at 1:23 pm

    This looks like exactly what we are after – I have some remote sites that I look after that I am rolling out new kit to. So I’m using esxi free to give me flexabilty on recovery etc – I have the USB pass thru working so can shut windows VM down, but I can’t shut the host down.

    The Host is esxi 5 free so can’t use vCLI that can shutdown the paid for licensed hosts – I’ve tried this script and after a bit of faffing to get nmap working (installing c++ redist and copying the wow 64 version of the dll into the same folder as nmap) I get a ‘cannot connect to host actively refused’ is it a non starter for esxi 5?

    Reply
  51. Running ESXi in Boston says

    24 March 2013 at 12:19 am

    It works great for me with ESXi 4.x. I don’t use the UPS signal so what I do is have a cron script that polls some devices on the network (Router, WAP and Lan NAS server). I know that my UPS battery lasts for 30-40 minutes. If ALL of these non-ups devices fail to respond to a ping for 20 minutes then I assume that power is out and shut down the ESXi server. This may sound like a strange way to do it but it works great for me.
    Also – I use cygwin so the ‘find’ in the script would invoke the cygwin find instead of the Windows ‘find’ so I modified the script and set FIND=/find.exe and then replaced find in the script with %FIND%.
    I’ve been looking for something like this for a while but most other solutions seem far too complicated. This is perfect – thank you very much.

    Reply
    • Guest says

      9 February 2015 at 11:11 am

      great idea! thanks – I’ll try that…

      Reply
  52. Artur Yarosh says

    25 March 2013 at 2:47 pm

    Looks, like using the APC AP9606 network card would be an optimal choise.

    Reply
  53. Akmetra says

    27 March 2014 at 10:01 am

    Tested on ESXi (free license) 5.5. Worked like a charm. I’m amazed 🙂
    Guest VMs are correctly shut down, everything is perfect.

    Reply
  54. juanespty says

    18 July 2014 at 2:37 am

    I haven’t tried this yet, but will try it out soon.. Very clever!..
    I was thinking about another option instead of Powerchute to detect the power outage.. and it could be done with an Arduino + network shield.. If the arduino is powered by a UPS, and also a regular dc power adapter is connected directly to the wall outlet just to detect if there’s power or not (google: arduino detect power outage), then through a simple web page arduino could report if it’s detecting power or not from the wall outlet.. From a cron process, the arduino web page could be monitored.. and if it’s reported that there’s no power for more than x minutes, then the esx shutdown process could be initiated..
    cheers!

    Reply
  55. Pham Duy says

    6 November 2016 at 12:58 am

    Thanks for your share Expirence
    Does it running as well with ESXi 5.5 free license ?

    Reply
  56. Alina @ APC Smart UPS 1500 says

    25 June 2018 at 6:54 am

    Hi,

    Great job Techhead Team, I’ve been looking for this for some time.

    I’m having a problem where the guest/s don’t get time to shut down gracefully before the host powers down. This is testing the command only, I have taken the ups out of the equation for now.

    Thanks.

    Reply

Trackbacks

  1. uberVU - social comments says:
    12 December 2009 at 3:00 pm

    Social comments and analytics for this post…

    This post was mentioned on Twitter by mars_seven: RT @PlanetV12n: How to automatically shut down VMware ESXi gracefully during power failure using an APC UPS. (TechHead) http://bit.ly/8dRJQj…

    Reply
  2. esxi-control.pl – script VM actions on free-licensed ESXi says:
    28 May 2010 at 8:51 am

    […] working on the UPS Shutdown Scriptfor free-licensed ESXi (originally published on TechHead), I made use of PortSwigger’s Burp Suiteto capture the underlying calls being made by the […]

    Reply
  3. Preparing APC Smart-UPS 1500 for Critical Servers « Binarymist says:
    16 June 2011 at 1:44 am

    […] guest listening to its events, ready to issue shutdown procedures as per James Pearce’s solution but to any number of […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Looking For Something?

Site Sponsors

Click here to become a sponsor

Awards

VMware vExpert

About Me

Simon Seagrave - TechHead - VMware EMC HP IT Tech Blogger My name is Simon Seagrave and I am a Massachusetts (ex UK) based Technical Marketing Consultant working for Dell EMC. I love my work & spend most of my time working with Virtualisation & other Enterprise IT based technologies, in particular VMware, EMC and Dell products. I am a VMware vExpert (2009 - Present).

Recent Post Comments

  • Michael on [Fix] How to access Starbucks WiFi when you aren’t redirected to the login page
  • Michal on "Cannot change the host configuration" error message when adding disk storage to a VMware vSphere ESXi Host
  • Jorge on Email Fail – virtual_aliases via virtual_aliases router force address failure
  • Javier on Email Fail – virtual_aliases via virtual_aliases router force address failure
  • Robert Baker on How to copy or move files and folders whilst maintaining their original time date stamp.

Recent Posts

  • [Fix] How to access Starbucks WiFi when you aren’t redirected to the login page
  • Fully Featured VMware vSphere Web Client (finally) On The Way
  • [Podcast] vChat Episode 45–Things Crypto-Currency and Crypto-Mining
  • [Link] Dell EMC Interactive Demos
  • "Cannot change the host configuration" error message when adding disk storage to a VMware vSphere ESXi Host

EZ Fat Footer #1

This is Dynamik Widget Area. You can add content to this area by going to Appearance > Widgets in your WordPress Dashboard and adding new widgets to this area.

EZ Fat Footer #2

This is Dynamik Widget Area. You can add content to this area by going to Appearance > Widgets in your WordPress Dashboard and adding new widgets to this area.

Copyright © 2019 · Dynamik-Gen on Genesis Framework · WordPress · Log in