The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

VMware ESXi: shell script to get uuid.bios and all Ethernet generatedAddress MAC address values for all VMs

Posted by jpluimers on 2023/12/28

This is a sort of follow-up on ESXi: listing virtual machines with their IP addresses where we ended with this:

I modified the above script to become this:

#!/bin/sh
vmids=`vim-cmd vmsvc/getallvms | sed -n -E -e "s/^([[:digit:]]+)s+((S.+S)?)s+([S+])s+(.+.vmx)s+(S+)s+(vmx-[[:digit:]]+)s*?((S.+)?)$/1/p"`
for vmid in ${vmids} ; do
    # powerState values:
    #   Powered off
    #   Powered on
    #   Suspended
    powerState=`vim-cmd vmsvc/power.getstate ${vmid} | sed '1d'`
    name=`vim-cmd vmsvc/get.config ${vmid} | sed -n -E -e '/(vim.vm.ConfigInfo) {/,/files = (vim.vm.FileInfo) {/ s/^ +name = "(.*)",.*?/1/p'`
    vmPathName=`vim-cmd vmsvc/get.config ${vmid} | sed -n -E -e '/files = (vim.vm.FileInfo) {/,/tools = (vim.vm.ToolsConfigInfo) {/ s/^ +vmPathName = "(.*)",.*?/1/p'`
    # For now, I choose to use only the IPv4 main address from ipAddress, which is in between (vim.vm.GuestInfo) { and net = (vim.vm.GuestInfo.NicInfo) [.
    ipAddress=`vim-cmd vmsvc/get.guest ${vmid} | sed -n -E -e '/(vim.vm.GuestInfo) {/,/net = (vim.vm.GuestInfo.NicInfo) [/ s/^ +ipAddress = "(.*)",.*?/1/p'`
    printf "VM with id %3s has power state %-11s and IPv4=%-15s (name = ${name}; vmPathName = ${vmPathName}).n" "${vmid}" "${powerState}" "${ipAddress}"
done

Now the script grew even larger in to vim-cmd-list-all-VMs-with-IPv4-MAC-uuid.sh:

#!/bin/sh

# https://wiert.me/2021/04/29/vmware-esxi-console-viewing-all-vms-suspending-and-waking-them-up-part-4/
vmids=`vim-cmd vmsvc/getallvms | sed -n -E -e "s/^([[:digit:]]+)\s+((\S.+\S)?)\s+(\[\S+\])\s+(.+\.vmx)\s+(\S+)\s+(vmx-[[:digit:]]+)\s*?((\S.+)?)$/\1/p"`

for vmid in ${vmids} ; do
    # powerState values:
    #   Powered off
    #   Powered on
    #   Suspended
    powerState=`vim-cmd vmsvc/power.getstate ${vmid} | sed '1d'`

    name=`vim-cmd vmsvc/get.config ${vmid} | sed -n -E -e '/\(vim.vm.ConfigInfo\) \{/,/files = \(vim.vm.FileInfo\) \{/ s/^ +name = "(.*)",.*?/\1/p'`

    vmPathName=`vim-cmd vmsvc/get.config ${vmid} | sed -n -E -e '/files = \(vim.vm.FileInfo\) \{/,/tools = \(vim.vm.ToolsConfigInfo\) \{/ s/^ +vmPathName = "(.*)",.*?/\1/p'`

    # https://wiert.me/2021/10/26/esxi-listing-virtual-machines-with-their-ip-addresses/
    # vim-cmd vmsvc/get.guest 6
    # For now, I choose to use only the IPv4 main address from ipAddress, which is in between (vim.vm.GuestInfo) { and net = (vim.vm.GuestInfo.NicInfo) [.
    ipAddress=`vim-cmd vmsvc/get.guest ${vmid} | sed -n -E -e '/\(vim.vm.GuestInfo\) \{/,/net = \(vim.vm.GuestInfo.NicInfo\) \[/ s/^ +ipAddress = "(.*)",.*?/\1/p'`

    # vim-cmd vmsvc/get.datastores via https://github.com/mjbeverley/xsibackup/blob/master/xsibackup
    # since I dislike IFS: use awk to get the second space delimited value on the line starting with "^url "
    datastorePathName=`vim-cmd vmsvc/get.datastores ${vmid} | grep "^url " | awk '{print $2}'`

    # base logic from https://github.com/openSUSE/suseviclient/blob/master/suseviclient
    vmxRelativePathName=`echo "${vmPathName}" | sed -n -E -e 's/^\[.*\] (.*)$/\1/p'`
    vmxPathName="${datastorePathName}/${vmxRelativePathName}"
    vmxRelativePathName=`echo "${vmPathName}" | sed -n -E -e 's/^\[.*\] (.*)$/\1/p'`

    uuidBios=`sed -n -E -e 's/^uuid.bios = "(.*)"$/\1/p' "${vmxPathName}"`

    printf "VM: id=%3s; power state=%-11s; IPv4=%-15s; uuid.bios=%47s; (name = ${name}; vmx = ${vmxPathName}).\n" "${vmid}" "${powerState}" "${ipAddress}" "${uuidBios}"

    grep -e "\.generatedAddress = " "${vmxPathName}"

done
esxcli vm process list
exit 0

Note I added esxcli vm process list to check if the uuid.bios would be the same as the UUID reported by esxcli: indeed they are.

Python?

The above script might have been easier in Python, but I still have not found time (given all the rectum cancer procedures and their aftermath) to follow-up on On my list of things to try: Python with ESXi.

References

The above adoption borrows heavily on ideas in these two scripts:

For the latter, I filed this issue: [Wayback/Archive] vmid2datastore() is buggy as datastores can have names outside [A-Za-z0-9-] · Issue #7 · openSUSE/suseviclient. In Datastore Naming Rules – VMware Technology Network VMTN, I referenced a post trying to document the valid characters in datastore names. My intuition in the bug report seems to be correct.

In my script, the combination of using vim-cmd vmsvc/get.datastores and  awk works around that problem.

–jeroen

Leave a comment

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