Skip to content

Instantly share code, notes, and snippets.

@v1k0d3n
Last active December 6, 2024 22:26
Show Gist options
  • Save v1k0d3n/981d90e75da4d1c34835328a77928fe1 to your computer and use it in GitHub Desktop.
Save v1k0d3n/981d90e75da4d1c34835328a77928fe1 to your computer and use it in GitHub Desktop.
Get Interface Information

TLDR

Running the following script to get a nice summary of all network interface information which includes the Interface Name, IP Address, MAC Address, Link State, Link Speed, MTU, Vendor, Device ID, Driver, Total VFs, Configured VFs, and PCIe Address.

curl -sL https://tinyurl.com/net-print | bash

Details

Using oc debug commands, this AI-generated gobbledygook script will collect all of the relevant useful information that can be gathered for an SriovNetworkNodePolicy. It's helpful for general NIC information as well.

While you're here, I would suggest checking out my other dirty hack for getting Disk information!

NOTE: *By default, the variable is used for an SNO. If you want to use another node, just change it to the standard node/<node-name> syntax and this script will work just fine.

NODE_NAME=$(oc get no -o name)

cat <<EOF | oc debug $NODE_NAME
chroot /host /bin/bash << 'CHROOT_EOF'
interfaces=\$(find /sys/class/net -mindepth 1 -maxdepth 1 -lname '*virtual*' -prune -o -print | xargs -n1 basename)

tmpfile=\$(mktemp)

# Adjusted column widths with the new column order
printf "%-15s %-15s %-17s %-10s %-10s %-10s %-10s %-10s %-15s %-10s %-15s %-30s\n" "Interface" "IP Address" "MAC Address" "Link State" "Link Speed" "MTU" "Vendor" "Device ID" "Driver" "Total VFs" "Configured VFs" "PCIe Address" > \$tmpfile

for iface in \$interfaces; do
    if [ "\$iface" != "bonding_masters" ] && [ ! -d "/sys/class/net/\$iface/device/virtfn0" ]; then
        ip_address=\$(ip addr show \$iface 2>/dev/null | grep 'inet ' | awk '{print \$2}' | cut -d/ -f1)
        if [ -z "\$ip_address" ]; then
            ip_address="-"
        fi
        
        mac_address=\$(cat /sys/class/net/\$iface/address 2>/dev/null)
        if [ -z "\$mac_address" ]; then
            mac_address="-"
        fi
        
        link_state=\$(ip link show \$iface 2>/dev/null | grep -oP '(?<=state )[^ ]+')
        if [ -z "\$link_state" ]; then
            link_state="-"
        fi

        link_speed=\$(ethtool \$iface 2>/dev/null | grep 'Speed:' | awk '{print \$2}')
        if [ "\$link_speed" = "Unknown!" ] || [ -z "\$link_speed" ]; then
            link_speed=\$(ip link show \$iface 2>/dev/null | grep 'state UP' | grep -oP '(?<=state UP )[^ ]+')
            if [ -z "\$link_speed" ]; then
                link_speed="-"
            fi
        fi
        
        mtu=\$(ip link show \$iface 2>/dev/null | grep -oP '(?<=mtu )[^ ]+')
        if [ -z "\$mtu" ]; then
            mtu="-"
        fi
        
        vendor=\$(cat /sys/class/net/\$iface/device/vendor 2>/dev/null | sed 's/^0x//')
        if [ -z "\$vendor" ]; then
            vendor="-"
        fi
        
        device_id=\$(cat /sys/class/net/\$iface/device/device 2>/dev/null | sed 's/^0x//')
        if [ -z "\$device_id" ]; then
            device_id="-"
        fi
        
        driver=\$(ethtool -i \$iface 2>/dev/null | grep driver | awk '{print \$2}')
        if [ -z "\$driver" ]; then
            driver="-"
        fi
        
        total_vfs=\$(cat /sys/class/net/\$iface/device/sriov_totalvfs 2>/dev/null)
        if [ -z "\$total_vfs" ]; then
            total_vfs="-"
        fi
        
        configured_vfs=\$(cat /sys/class/net/\$iface/device/sriov_numvfs 2>/dev/null)
        if [ -z "\$configured_vfs" ]; then
            configured_vfs="-"
        fi
        
        pci_address=\$(ethtool -i \$iface 2>/dev/null | grep bus-info | awk '{print \$2}')
        if [ -z "\$pci_address" ]; then
            pci_address="-"
        fi
        
        printf "%-15s %-15s %-17s %-10s %-10s %-10s %-10s %-10s %-15s %-10s %-15s %-30s\n" "\$iface" "\$ip_address" "\$mac_address" "\$link_state" "\$link_speed" "\$mtu" "\$vendor" "\$device_id" "\$driver" "\$total_vfs" "\$configured_vfs" "\$pci_address" >> \$tmpfile
    fi
done

sort \$tmpfile

rm -f \$tmpfile
CHROOT_EOF
EOF

Example Output:

Starting pod/roderika-debug-sldzx ...
To use host binaries, run `chroot /host`
Pod IP: 192.168.3.99
If you don't see a command prompt, try pressing enter.
Interface       IP Address      MAC Address       Link State Link Speed MTU        Vendor     Device ID  Driver          Total VFs  Configured VFs  PCIe Address
eno1np0         -               b0:7b:25:d5:98:fa UP         10000Mb/s  9000       14e4       16d8       bnxt_en         8          0               0000:18:00.0
eno2np1         -               b0:7b:25:d5:98:fa UP         10000Mb/s  9000       14e4       16d8       bnxt_en         8          0               0000:18:00.1
enp0s20f0u14u3  169.254.1.2     b0:7b:25:d5:99:0d UNKNOWN    425Mb/s    1500       -          -          cdc_ether       -          -               usb-0000:00:14.0-14.3
ens1f0          -               6c:fe:54:5a:32:78 DOWN       -          1500       8086       1592       ice             128        0               0000:5e:00.0
ens1f1          -               6c:fe:54:5a:32:79 DOWN       -          1500       8086       1592       ice             128        0               0000:5e:00.1
ens8f0v32       -               12:02:d6:94:77:04 UP         10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:06.0
ens8f0v33       -               be:ab:28:49:a8:11 UP         10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:06.1
ens8f0v34       -               4e:56:2d:60:2c:24 UP         10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:06.2
ens8f0v35       -               32:11:0d:af:05:74 UP         10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:06.3
ens8f0v36       -               2a:fc:77:3b:d8:b7 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:06.4
ens8f0v37       -               1e:c2:ea:00:1a:82 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:06.5
ens8f0v38       -               22:a8:df:cb:9a:d8 UP         10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:06.6
ens8f0v39       -               f2:be:fd:e1:52:7e DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:06.7
ens8f0v40       -               56:29:a8:81:42:b6 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:07.0
ens8f0v41       -               be:91:1f:d6:a5:ad UP         10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:07.1
ens8f0v42       -               62:42:68:d0:b3:4f UP         10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:07.2
ens8f0v43       -               aa:53:19:b2:38:d3 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:07.3
ens8f0v44       -               6a:8e:db:6d:0a:71 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:07.4
ens8f0v45       -               72:c7:7c:57:58:32 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:07.5
ens8f0v46       -               06:e1:14:72:d8:43 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:07.6
ens8f0v47       -               ca:73:3d:ac:23:ab DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:07.7
ens8f0v48       -               a2:5e:87:ac:26:92 UP         10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:08.0
ens8f0v49       -               0a:d3:3d:25:1d:d9 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:08.1
ens8f0v50       -               1e:52:9c:bd:56:a2 UP         10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:08.2
ens8f0v51       -               26:f4:aa:98:07:08 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:08.3
ens8f0v52       -               c6:9d:ea:7c:af:45 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:08.4
ens8f0v53       -               26:f6:43:54:a5:85 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:08.5
ens8f0v54       -               d2:9f:db:b7:71:28 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:08.6
ens8f0v55       -               32:d6:79:2d:06:b7 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:08.7
ens8f0v56       -               b6:b0:da:51:12:87 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:09.0
ens8f0v57       -               76:d8:56:0b:d1:c4 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:09.1
ens8f0v58       -               1e:7b:53:e5:e0:29 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:09.2
ens8f0v59       -               62:88:d6:aa:5c:c1 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:09.3
ens8f0v60       -               6e:09:c6:b0:2b:32 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:09.4
ens8f0v61       -               e6:eb:bf:f5:02:ac DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:09.5
ens8f0v62       -               e6:19:16:e7:23:f1 DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:09.6
ens8f0v63       -               5a:24:bd:c8:1b:fd DOWN       10000Mb/s  1500       8086       154c       iavf            -          -               0000:b1:09.7
ens8f1          -               40:a6:b7:46:87:41 UP         10000Mb/s  9000       8086       158b       i40e            64         0               0000:b1:00.1

Removing debug pod ...

For most of the examples, I am going to use an interface named ens8f0 (which examplains the PF enumerated NICs above) and sometimes ens8f0, which are both ports from an Intel XXV710 (25G x2) NIC card. You may see be use the following cards (which I have for testing in my lab).

Considerations

One thing that you may notice is that when SR-IOV is enabled, you won't see the base interface but you will see the PF interfaces that base interface created. Example, if you hvae an interface like ens8f0, and create a bunch of PFs named ens8f0v1-31 you will not see the originating ens8f01. I was ok with this, but if you need to see this and you have suggestions for improvements, please feel free to leave them down below (and I will update it accordingly).

#!/bin/bash
# Gather interfaces excluding virtual ones
interfaces=$(find /sys/class/net -mindepth 1 -maxdepth 1 -lname '*virtual*' -prune -o -print | xargs -n1 basename)
# Temporary file to store the output
tmpfile=$(mktemp)
# Adjusted column widths with the new column order
printf "%-15s %-15s %-17s %-10s %-10s %-10s %-10s %-10s %-15s %-10s %-15s %-30s\n" \
"Interface" "IP Address" "MAC Address" "Link State" "Link Speed" "MTU" \
"Vendor" "Device ID" "Driver" "Total VFs" "Configured VFs" "PCIe Address" > "$tmpfile"
# Process each interface
for iface in $interfaces; do
if [ "$iface" != "bonding_masters" ] && [ ! -d "/sys/class/net/$iface/device/virtfn0" ]; then
ip_address=$(ip addr show "$iface" 2>/dev/null | grep 'inet ' | awk '{print $2}' | cut -d/ -f1)
ip_address=${ip_address:-"-"}
mac_address=$(cat /sys/class/net/"$iface"/address 2>/dev/null)
mac_address=${mac_address:-"-"}
link_state=$(ip link show "$iface" 2>/dev/null | grep -oP '(?<=state )[^ ]+')
link_state=${link_state:-"-"}
link_speed=$(sudo ethtool "$iface" 2>/dev/null | grep 'Speed:' | awk '{print $2}')
if [ "$link_speed" = "Unknown!" ] || [ -z "$link_speed" ]; then
link_speed=$(ip link show "$iface" 2>/dev/null | grep 'state UP' | grep -oP '(?<=state UP )[^ ]+')
link_speed=${link_speed:-"-"}
fi
mtu=$(ip link show "$iface" 2>/dev/null | grep -oP '(?<=mtu )[^ ]+')
mtu=${mtu:-"-"}
vendor=$(cat /sys/class/net/"$iface"/device/vendor 2>/dev/null | sed 's/^0x//')
vendor=${vendor:-"-"}
device_id=$(cat /sys/class/net/"$iface"/device/device 2>/dev/null | sed 's/^0x//')
device_id=${device_id:-"-"}
driver=$(sudo ethtool -i "$iface" 2>/dev/null | grep driver | awk '{print $2}')
driver=${driver:-"-"}
total_vfs=$(cat /sys/class/net/"$iface"/device/sriov_totalvfs 2>/dev/null)
total_vfs=${total_vfs:-"-"}
configured_vfs=$(cat /sys/class/net/"$iface"/device/sriov_numvfs 2>/dev/null)
configured_vfs=${configured_vfs:-"-"}
pci_address=$(sudo ethtool -i "$iface" 2>/dev/null | grep bus-info | awk '{print $2}')
pci_address=${pci_address:-"-"}
printf "%-15s %-15s %-17s %-10s %-10s %-10s %-10s %-10s %-15s %-10s %-15s %-30s\n" \
"$iface" "$ip_address" "$mac_address" "$link_state" "$link_speed" "$mtu" \
"$vendor" "$device_id" "$driver" "$total_vfs" "$configured_vfs" "$pci_address" >> "$tmpfile"
fi
done
# Sort and display the results
sort "$tmpfile"
rm -f "$tmpfile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment