How to identify NICs that are connected to the same switch from a Linux box?

The switches may already be sending you the information you want. If they are Cisco switches, by default they will be using a process called CDP (Cisco Discovery Protocol) which will provide you information about the switch where it is connected.

You can use tcpdump to view this information with the following (substituting the appropriate interface):

tcpdump -nn -v -i eth0 -s 1500 -c 1 'ether[20:2] == 0x2000'

The standard based version of CDP is LLDP (link layer discovery protocol). Some vendors will have this on by default and others off, so your mileage will vary. There are some LLDP implementations for Linux, but if you want something similar to the above you can use this (set up LLDP on a Cisco switch and tested the below, which is more consistent with above):

tcpdump -nn -v -i eth0 -s 1500 -c 1 'ether proto 0x88cc'

Barring that, I would say that a modification of option 1 you provide might work, however, instead of sending out a broadcast ICMP, you can try a normal ICMP (to a host not in the ARP table) and capture the ARP packets. If ARP request is sent out eth0 and you receive it on eth1 and eth3, then you know those are on the same VLAN. Simplest command for that is as follows:

tcpdump -i eth0 arp

Leave a Comment