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 4,265 other subscribers

ESXi: listing virtual machines with their IP addresses

Posted by jpluimers on 2021/10/26

This is sort of a follow-up on VMware ESXi console: viewing all VMs, suspending and waking them up: part 4 which already gave part of the configuration details of all the configured VMs.

Back then, we ended with this:

List the vmid values, power status and name of all VMs

Back to the listing script vim-cmd-list-all-VMs.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=`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'`
    echo "VM with id ${vmid} has power state ${powerState} (name = ${name}; vmPathName = ${vmPathName})."
done

It uses vim-cmd vmsvc/getallvms, vim-cmd vmsvc/power.getstate and vim-cmd vmsvc/get.config with some sed and a for loop from dash to generate a nice list of information.

A long time ago, I already figured out that vim-cmd vmsvc/get.guest # gives all guest information including network information for a running VM that has either VMware Tools or open-vm-tools running (see VMware ESXi console: viewing all VMs, suspending and waking them up: part 3 for the difference between these two tools).

A full output of a sample VM is below the signature.

There are a few places that have the LAN ipAddress. 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) [.

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

I went replaced echo with printf output because that allows for both left (%-#s) and right (%#s) alignment of strings. I had to put double quotes around the paramaters in case they contain spaces (and would count as multiple parameters, messing up the printf output).

It seems the dash implementation of printf supports most of not all of the bash features.

Note that printf needs a newline (\n) at the end of the string, otherwise it continues on the same line.

See these posts for more on the printf alignment:

For a single Vmid, you can do this ugly output in a simple way using grep, as the BusyBox grep allows the -m # parameter allows to limit the output to # matches:

# vim-cmd vmsvc/get.guest 6 | grep -m 1 -i 'ipaddress = "'
   ipAddress = "192.168.124.48", 

[Wayback] Busybox grep:

grep [-HhrilLnqvsoweFEABCz] PATTERN [FILE]...

Search for PATTERN in each FILE or standard input

Options:

        -H      Prefix output lines with filename where match was found
        -h      Suppress the prefixing filename on output
        -r      Recurse
        -i      Ignore case distinctions
        -l      List names of files that match
        -L      List names of files that do not match
        -n      Print line number with output lines
        -q      Quiet. Return 0 if PATTERN is found, 1 otherwise
        -v      Select non-matching lines
        -s      Suppress file open/read error messages
        -c      Only print count of matching lines
        -o      Show only the part of a line that matches PATTERN
        -m MAX  Match up to MAX times per file
        -w      Match whole words only
        -F      PATTERN is a set of newline-separated strings
        -E      PATTERN is an extended regular expression
        -e PTRN Pattern to match
        -f FILE Read pattern from file
        -A      Print NUM lines of trailing context
        -B      Print NUM lines of leading context
        -C      Print NUM lines of output context
        -z      Input is NUL terminated

–jeroen


All guest information for one VM that supports both IPv4 and IPv6

# vim-cmd vmsvc/get.guest 6
Guest information:

(vim.vm.GuestInfo) {
   toolsStatus = "toolsOk", 
   toolsVersionStatus = "guestToolsUnmanaged", 
   toolsVersionStatus2 = "guestToolsUnmanaged", 
   toolsRunningStatus = "guestToolsRunning", 
   toolsVersion = "11264", 
   toolsInstallType = "guestToolsTypeOpenVMTools", 
   toolsUpdateStatus = (vim.vm.GuestInfo.ToolsUpdateStatus) null, 
   guestId = "opensuse64Guest", 
   guestFamily = "linuxGuest", 
   guestFullName = "SUSE openSUSE (64-bit)", 
   hostName = "lampje", 
   ipAddress = "192.168.124.48", 
   net = (vim.vm.GuestInfo.NicInfo) [
      (vim.vm.GuestInfo.NicInfo) {
         network = "VM Network", 
         ipAddress = (string) [
            "192.168.124.48", 
            "2001:985:5a49:1:61bf:89b0:472b:2992", 
            "2001:985:5a49:1:41c4:26e8:406:4095", 
            "2001:985:5a49:1:451f:6ea5:7a2f:4e65", 
            "2001:985:5a49:1:39b3:78f5:d16b:3b3f", 
            "2001:985:5a49:1:cdb7:9d8c:c765:7916", 
            "2001:985:5a49:1:d82:53e8:8341:4d10", 
            "2001:985:5a49:1:e1b1:3646:1219:ad33", 
            "2001:985:5a49:1:20c:29ff:fe79:a2b0", 
            "fe80::20c:29ff:fe79:a2b0"
         ], 
         macAddress = "00:0c:29:79:a2:b0", 
         connected = true, 
         deviceConfigId = 4000, 
         dnsConfig = (vim.net.DnsConfigInfo) null, 
         ipConfig = (vim.net.IpConfigInfo) {
            ipAddress = (vim.net.IpConfigInfo.IpAddress) [
               (vim.net.IpConfigInfo.IpAddress) {
                  ipAddress = "192.168.124.48", 
                  prefixLength = 24, 
                  origin = , 
                  state = "preferred", 
                  lifetime = 
               }, 
               (vim.net.IpConfigInfo.IpAddress) {
                  ipAddress = "2001:985:5a49:1:61bf:89b0:472b:2992", 
                  prefixLength = 64, 
                  origin = , 
                  state = "unknown", 
                  lifetime = 
               }, 
               (vim.net.IpConfigInfo.IpAddress) {
                  ipAddress = "2001:985:5a49:1:41c4:26e8:406:4095", 
                  prefixLength = 64, 
                  origin = , 
                  state = "unknown", 
                  lifetime = 
               }, 
               (vim.net.IpConfigInfo.IpAddress) {
                  ipAddress = "2001:985:5a49:1:451f:6ea5:7a2f:4e65", 
                  prefixLength = 64, 
                  origin = , 
                  state = "unknown", 
                  lifetime = 
               }, 
               (vim.net.IpConfigInfo.IpAddress) {
                  ipAddress = "2001:985:5a49:1:39b3:78f5:d16b:3b3f", 
                  prefixLength = 64, 
                  origin = , 
                  state = "unknown", 
                  lifetime = 
               }, 
               (vim.net.IpConfigInfo.IpAddress) {
                  ipAddress = "2001:985:5a49:1:cdb7:9d8c:c765:7916", 
                  prefixLength = 64, 
                  origin = , 
                  state = "unknown", 
                  lifetime = 
               }, 
               (vim.net.IpConfigInfo.IpAddress) {
                  ipAddress = "2001:985:5a49:1:d82:53e8:8341:4d10", 
                  prefixLength = 64, 
                  origin = , 
                  state = "unknown", 
                  lifetime = 
               }, 
               (vim.net.IpConfigInfo.IpAddress) {
                  ipAddress = "2001:985:5a49:1:e1b1:3646:1219:ad33", 
                  prefixLength = 64, 
                  origin = , 
                  state = "unknown", 
                  lifetime = 
               }, 
               (vim.net.IpConfigInfo.IpAddress) {
                  ipAddress = "2001:985:5a49:1:20c:29ff:fe79:a2b0", 
                  prefixLength = 64, 
                  origin = , 
                  state = "unknown", 
                  lifetime = 
               }, 
               (vim.net.IpConfigInfo.IpAddress) {
                  ipAddress = "fe80::20c:29ff:fe79:a2b0", 
                  prefixLength = 64, 
                  origin = , 
                  state = "unknown", 
                  lifetime = 
               }
            ], 
            dhcp = (vim.net.DhcpConfigInfo) null, 
            autoConfigurationEnabled = 
         }, 
         netBIOSConfig = (vim.net.NetBIOSConfigInfo) null
      }
   ], 
   ipStack = (vim.vm.GuestInfo.StackInfo) [
      (vim.vm.GuestInfo.StackInfo) {
         dnsConfig = (vim.net.DnsConfigInfo) {
            dhcp = false, 
            hostName = "lampje", 
            domainName = "fritz.box", 
            ipAddress = (string) [
               "192.168.124.1", 
               "fd00::3a10:d5ff:fe17:56f6"
            ], 
            searchDomain = (string) [
               "fritz.box"
            ]
         }, 
         ipRouteConfig = (vim.net.IpRouteConfigInfo) {
            ipRoute = (vim.net.IpRouteConfigInfo.IpRoute) [
               (vim.net.IpRouteConfigInfo.IpRoute) {
                  network = "0.0.0.0", 
                  prefixLength = 0, 
                  gateway = (vim.net.IpRouteConfigInfo.Gateway) {
                     ipAddress = "192.168.124.1", 
                     device = "0"
                  }
               }, 
               (vim.net.IpRouteConfigInfo.IpRoute) {
                  network = "192.168.124.0", 
                  prefixLength = 24, 
                  gateway = (vim.net.IpRouteConfigInfo.Gateway) {
                     ipAddress = , 
                     device = "0"
                  }
               }, 
               (vim.net.IpRouteConfigInfo.IpRoute) {
                  network = "2001:985:5a49:1::", 
                  prefixLength = 64, 
                  gateway = (vim.net.IpRouteConfigInfo.Gateway) {
                     ipAddress = , 
                     device = "0"
                  }
               }, 
               (vim.net.IpRouteConfigInfo.IpRoute) {
                  network = "fe80::", 
                  prefixLength = 64, 
                  gateway = (vim.net.IpRouteConfigInfo.Gateway) {
                     ipAddress = , 
                     device = "0"
                  }
               }, 
               (vim.net.IpRouteConfigInfo.IpRoute) {
                  network = "::", 
                  prefixLength = 0, 
                  gateway = (vim.net.IpRouteConfigInfo.Gateway) {
                     ipAddress = "fe80::3a10:d5ff:fe17:56f6", 
                     device = "0"
                  }
               }, 
               (vim.net.IpRouteConfigInfo.IpRoute) {
                  network = "2001:985:5a49:1:20c:29ff:fe79:a2b0", 
                  prefixLength = 128, 
                  gateway = (vim.net.IpRouteConfigInfo.Gateway) {
                     ipAddress = , 
                     device = "0"
                  }
               }, 
               (vim.net.IpRouteConfigInfo.IpRoute) {
                  network = "2001:985:5a49:1:d82:53e8:8341:4d10", 
                  prefixLength = 128, 
                  gateway = (vim.net.IpRouteConfigInfo.Gateway) {
                     ipAddress = , 
                     device = "0"
                  }
               }, 
               (vim.net.IpRouteConfigInfo.IpRoute) {
                  network = "2001:985:5a49:1:39b3:78f5:d16b:3b3f", 
                  prefixLength = 128, 
                  gateway = (vim.net.IpRouteConfigInfo.Gateway) {
                     ipAddress = , 
                     device = "0"
                  }
               }, 
               (vim.net.IpRouteConfigInfo.IpRoute) {
                  network = "2001:985:5a49:1:41c4:26e8:406:4095", 
                  prefixLength = 128, 
                  gateway = (vim.net.IpRouteConfigInfo.Gateway) {
                     ipAddress = , 
                     device = "0"
                  }
               }, 
               (vim.net.IpRouteConfigInfo.IpRoute) {
                  network = "2001:985:5a49:1:451f:6ea5:7a2f:4e65", 
                  prefixLength = 128, 
                  gateway = (vim.net.IpRouteConfigInfo.Gateway) {
                     ipAddress = , 
                     device = "0"
                  }
               }, 
               (vim.net.IpRouteConfigInfo.IpRoute) {
                  network = "2001:985:5a49:1:61bf:89b0:472b:2992", 
                  prefixLength = 128, 
                  gateway = (vim.net.IpRouteConfigInfo.Gateway) {
                     ipAddress = , 
                     device = "0"
                  }
               }, 
               (vim.net.IpRouteConfigInfo.IpRoute) {
                  network = "2001:985:5a49:1:cdb7:9d8c:c765:7916", 
                  prefixLength = 128, 
                  gateway = (vim.net.IpRouteConfigInfo.Gateway) {
                     ipAddress = , 
                     device = "0"
                  }
               }, 
               (vim.net.IpRouteConfigInfo.IpRoute) {
                  network = "2001:985:5a49:1:e1b1:3646:1219:ad33", 
                  prefixLength = 128, 
                  gateway = (vim.net.IpRouteConfigInfo.Gateway) {
                     ipAddress = , 
                     device = "0"
                  }
               }, 
               (vim.net.IpRouteConfigInfo.IpRoute) {
                  network = "fe80::20c:29ff:fe79:a2b0", 
                  prefixLength = 128, 
                  gateway = (vim.net.IpRouteConfigInfo.Gateway) {
                     ipAddress = , 
                     device = "0"
                  }
               }, 
               (vim.net.IpRouteConfigInfo.IpRoute) {
                  network = "ff00::", 
                  prefixLength = 8, 
                  gateway = (vim.net.IpRouteConfigInfo.Gateway) {
                     ipAddress = , 
                     device = "0"
                  }
               }
            ]
         }, 
         ipStackConfig = , 
         dhcpConfig = (vim.net.DhcpConfigInfo) null
      }
   ], 
   disk = (vim.vm.GuestInfo.DiskInfo) [
      (vim.vm.GuestInfo.DiskInfo) {
         diskPath = "/", 
         capacity = 32201768960, 
         freeSpace = 27806666752
      }, 
      (vim.vm.GuestInfo.DiskInfo) {
         diskPath = "/boot/grub2/x86_64-efi", 
         capacity = 32201768960, 
         freeSpace = 27806666752
      }, 
      (vim.vm.GuestInfo.DiskInfo) {
         diskPath = "/.snapshots", 
         capacity = 32201768960, 
         freeSpace = 27806666752
      }, 
      (vim.vm.GuestInfo.DiskInfo) {
         diskPath = "/boot/grub2/i386-pc", 
         capacity = 32201768960, 
         freeSpace = 27806666752
      }, 
      (vim.vm.GuestInfo.DiskInfo) {
         diskPath = "/srv", 
         capacity = 32201768960, 
         freeSpace = 27806666752
      }, 
      (vim.vm.GuestInfo.DiskInfo) {
         diskPath = "/home", 
         capacity = 32201768960, 
         freeSpace = 27806666752
      }, 
      (vim.vm.GuestInfo.DiskInfo) {
         diskPath = "/root", 
         capacity = 32201768960, 
         freeSpace = 27806666752
      }, 
      (vim.vm.GuestInfo.DiskInfo) {
         diskPath = "/opt", 
         capacity = 32201768960, 
         freeSpace = 27806666752
      }, 
      (vim.vm.GuestInfo.DiskInfo) {
         diskPath = "/usr/local", 
         capacity = 32201768960, 
         freeSpace = 27806666752
      }, 
      (vim.vm.GuestInfo.DiskInfo) {
         diskPath = "/tmp", 
         capacity = 32201768960, 
         freeSpace = 27806666752
      }, 
      (vim.vm.GuestInfo.DiskInfo) {
         diskPath = "/var", 
         capacity = 32201768960, 
         freeSpace = 27806666752
      }
   ], 
   screen = (vim.vm.GuestInfo.ScreenInfo) {
      width = 800, 
      height = 600
   }, 
   guestState = "running", 
   powerPolicy = (vim.vm.PowerPolicy) null, 
   appHeartbeatStatus = "appStatusGray", 
   guestKernelCrashed = false, 
   appState = "none", 
   guestOperationsReady = true, 
   interactiveGuestOperationsReady = false, 
   guestStateChangeSupported = true, 
   generationInfo = , 
   customizationInfo = (vim.vm.GuestInfo.CustomizationInfo) null
}

Leave a comment

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