What Physical NICs Are Your VMs Using?

What Physical NICs Are Your VMs Using?

By Carlo Costanzo
Posted in Infrastructure, Support
On June 02, 2023

The Scene

Recently, we were working with a client who was experiencing packet loss on some network adapters on their systems. The ESXi host was set up in a pretty standard configuration of a vSwitch with two physical NICs connected to it. We’d done a lot of troubleshooting up to this point and wanted to start correlating Virtual Machines to pNICs.

The NIC teaming on the vSwitch was set up as a default active/active scenario and was using the route by origin method. This means that when a VM powers up, VMware associates it with a physical NIC bound to the vSwitch and keeps it on that NIC for the VM’s entire lifecycle. The VM will not be moved to another NIC regardless of load until it is either powered off and on or vMotioned off the host. With many Virtual Machines on the host, this type of round robin placement normally works fine.

"

The issue is that there is no real way to see which NIC is in use by what VM via the UI or even PowerShell.

Our solution came as a shell script that uses ESXCLI to query and report this information. The script is run on the ESXi host and lists out all the VMs on the host with their associated WorldIDs. With the WorldIDs, we then can find out what VMNIC, the VM is tied to.

The Code

#!/bin/sh
# Get the list of VMs
esxcli network vm list | awk 'NR>2' > /tmp/vmlist

while read -r line; do
  WorldID=$(echo $line | awk '{print $1}')
  VMName=$(echo $line | awk ' {print $2}')
  PortList=$(esxcli network vm port list -w $WorldID)
  NICID=$(echo "$PortList" | grep "Team Uplink:" | awk '{print $3}')
  MACID=$(echo "$PortList" | grep "MAC Address:" | awk '{print $3}')
  echo "VM: $VMName          NIC: $NICID  MAC: $MACID"
  echo "For more Details: esxcli network vm port list -w $WorldID"
done < /tmp/vmlist

# Clean up the temporary file
rm /tmp/vmlist

I’ve uploaded this script to Github as well.

In order to get this into our shell, we SSH’d into our ESXi host and typed the following:

cd /tmp
vi show_nic.sh
>press i to goto insert mode
>paste code
>press esc, wq [enter]

With the file created you just need to make it executable:
chmod +x show_nic.sh

The output of the script will look like this:

"

With this data, we can easily see if the VMs are stacked on a particular NIC or in our case, we could look at our troubled NIC and begin to correlate VMs that were tied to it to further troubleshoot.

Hope this helps!

Carlo Costanzo

Carlo Costanzo

I am a seasoned Senior Consultant with over 25 years of experience in designing and implementing complex VMware, Microsoft, and Citrix solutions. Through my writing and contributions, I provide valuable insights into the latest technical advancements.