In a previous article, we talked about how to get SSH command line access in ESX 4.0. Well, now that you’ve got the access, what are you going to do with it? I use VMware Sphere for most of my administration, but every now and again I like to use the command line for quick and easy shutdown and startups. In addition, if I have a mass shutdown/restarts, I like to use ssh-agents and authorized keys to easily script them.
So first things first, SSH into your ESX server at the command line:
# ssh root@xxx.xxx.xxx.xxx
where xxx.xxx.xxx.xxx is the IP address of the VMware host.
In order to run most VMware commands, you need an identifier (VMID) of the VM upon which you want to effect change. You can get the VMID identifiers for all the VMs on the box by running:
# vim-cmd vmsvc/getallvms
This will list all the VMs on the machines whether or not they are running. You’ll get a response which looks something like this:
Vmid Name File Guest OS Version Annotation
112 mach1 [local_vmhost_a] mach1/mach1.vmx winNetStandardGuest vmx-07 256 mach2 [local_vmhost_a] mach2/mach2.vmx winXPProGuest vmx-07 320 mach3 [local_vmhost_a] mach3/mach3.vmx ubuntuGuest vmx-07
While this listing gives you a good deal of information regarding the VMs on the system, it unfortunately leaves out whether or not the VMs are powered on. Now that you have the VMID, however, VM state is easily determined:
# vim-cmd vmsvc/power.getstate 112 Retrieved runtime info Powered on
Other handy commands include being able to power a VM on and off… Use the correct VMID to identify the VM:
vim-cmd vmsvc/power.on 112
vim-cmd vmsvc/power.off 112
In fact, there’s a slew of commands you can run in this method. To see them, type:
vim-cmd help vmsvc
This will output a list of all the vmsvc commands. Many of the commands are self explanatory, but if you need a little extra verbiage to figure out what a command does, type vim-cmd help followed by the option to get a little blurb:
# vim-cmd help vmsvc/snapshot.dumpoption Usage: snapshot.dumpoption showconfig Set the snapshot dump option.
Hopefully this will help you when you’re automating!
