LLDP: Identify switch port to which the server is connected

Show LLDP neighbors

networkctl lldp

lldpctl
http://www.panticz.de/lldpd

for NIC in $(find /sys/class/net -type l -not -lname "*virtual*" -printf "%f\n" | sort); do
    echo "NIC: ${NIC}"
    echo "NIC MAC: $(ethtool -P ${NIC})"
    timeout 300 tcpdump -nn -v -i ${NIC} -s 1500 -c 1 "ether[20:2] == 0x2000"
done

Get host port, switch, switch port assignment

LLDP_JSON=$(lldpctl -f json)
 
NICS=$(echo ${LLDP_JSON} | jq -r '.lldp.interface[] | select(.[].port.id.type != "mac") | keys[]')
# echo ${NICS}
 
for NIC in ${NICS}; do
    # echo ${HOSTNAME}
    # echo ${NIC}
 
    SWITCH_NAME=$(echo ${LLDP_JSON} | jq -r ".lldp.interface[].${NIC} | select(.chassis != null) .chassis | keys[]")
    # echo ${SWITCH_NAME}
 
    SWITCH_PORT=$(echo ${LLDP_JSON} | jq -r ".lldp.interface[].${NIC} | select(.chassis != null) .port.id.value")
    # echo ${SWITCH_PORT}
 
    SWITCH_VLAN=$(echo ${LLDP_JSON} | jq -r ".lldp.interface[].${NIC} | select(.chassis != null) .vlan.\"vlan-id\"")
    # echo ${SWITCH_VLAN}
 
    echo "${HOSTNAME},${NIC},${SWITCH_NAME},${SWITCH_PORT},${SWITCH_VLAN}"
done