ESXi is popular hypervisor product from VMware. It comes with several management GUIs including Vsphere and VCenter. Command line tools are also bundled, though they are used more rarely. This article describes a short script to list all virtual machines on the system.
The script is below. It is intended to run under the Busybox shell, the default environment when you ssh directly into the system hosting ESXi.
Showvms.sh Script
A surprising number of Unix tools are available under the ESXi implementation of Busybox. For example sed, egrep and awk. So it is quite easy to write useful scripts. This one involves a long pipeline, which I have broken into lines, indented and commented separately.
#!/bin/ash # # Quick script to list VMs and specs of each. Allows for spaces in names. # # For ESXi Busybox ash # # Jim McD 22/7/14 # printf "%st%st%st%st%sn" Power Name Memory CPUs FullName vim-cmd vmsvc/getallvms | grep "^[0-9]" | while read vmid therest do # Long pipeline follows. Comment out later parts of it to debug # Run command to dump VM information vim-cmd vmsvc/get.summary $vmid | # Remove short "guest" section otherwise it would duplicate matches later sed '/ guest =/,/}/d' | # Extract the few lines of actual interest. egrep "memorySizeMB|powerState|numCpu| name |guestFullName" | # Remove the left hand side and the "=" bit, leaving only the value sed 's/.*= //' | # Remove commans and trailing spaces sed 's/,//' | sed 's/ *$//' | # Print value field with no carriage return, just a tab awk '{printf("%st", $0)}' echo done
To use the script, just run it on the ESXi system (as root) without any arguments. The output is tab seperated, to make it easier to import into Excel, if that were desired.
$ showvms.sh Connecting to host: hpblade06 Password: Power Name Memory CPUs FullName "poweredOn" "vm01" 8192 2 "Red Hat Enterprise Linux 6 (64-bit)" "poweredOn" "vm01" 8192 2 "Red Hat Enterprise Linux 6 (64-bit)" "poweredOn" "vm03" 8192 2 "Red Hat Enterprise Linux 6 (64-bit)" "poweredOn" "vm04" 8192 2 "Red Hat Enterprise Linux 6 (64-bit)" "poweredOff" "vm05" 8192 2 "Red Hat Enterprise Linux 6 (64-bit)" "poweredOff" "vm06" 8192 2 "Red Hat Enterprise Linux 6 (64-bit)" "poweredOn" "vm07" 8192 2 "Red Hat Enterprise Linux 6 (64-bit)" "poweredOn" "vm08" 8192 2 "Red Hat Enterprise Linux 6 (64-bit)" "poweredOn" "vm09" 8192 2 "Red Hat Enterprise Linux 6 (64-bit)" "poweredOn" "vm10" 8192 2 "Red Hat Enterprise Linux 6 (64-bit)" "poweredOn" "vm11" 8192 2 "Red Hat Enterprise Linux 6 (64-bit)" "poweredOn" "vm12" 8192 2 "Red Hat Enterprise Linux 6 (64-bit)" "poweredOn" "vm13" 8192 2 "Red Hat Enterprise Linux 6 (64-bit)" "poweredOn" "vm14" 8192 2 "Red Hat Enterprise Linux 6 (64-bit)"
The above output was taken from an HP BL460c G8 blade server running ESXi 5.1. I haven’t tested it on other versions. We have 14 virtual machines on there, each sized at 8 GB. The blade has 256GB of memory, of which 112GB (ie. 14 x 8) is allocated to the VMs as shown.
Run Remotely
Rather than put the script on each ESXi server, I find it easier to just put it on a linux box and run it from there, via ssh:
[fred@adminbox] $ ssh -l root esxiblade05 'ash' < ./showvms.sh Password: Power Name Memory CPUs FullName "poweredOn" "gpwinwp01" 2048 1 "Microsoft Windows 7 (64-bit)" "poweredOn" "gpwinwp02" 2048 1 "Microsoft Windows 7 (64-bit)"
To get a report on several servers, put it in a script or use a construct such as:
[fred@adminbox]$ for blade in hpblade04 hpblade0505 hpblade06; do ssh -l root $blade 'ash' < ./showvms.sh ; done
It will ask for the password of each server, unless you have apropriare keys set up.