If I have two nodes/computers connected to each other on the same range of IPs. Say:
Node A: 192.168.0.1/30 Node B: 192.168.0.2/30
How to know (from Node A) if B is connected directly to it (via the same switch) or not (via multiple hops)?
Network Engineering Stack Exchange is a question and answer site for network engineers. It only takes a minute to sign up.Sign up to join this community
Not unless the switch is identifying itself via CDP or LLDP. Even then, there can be technology along the path that won't be detectable. (i.e. in a metro-e setup you may have two ports on the same layer-2 device that follow different layer-1 paths.)
Timing measurements will ultimately be inconclusive. Different switches process at different rates, and the load on the switch can lead to variability in switching rates. Plus, do you have the gear to time things down to the micro- or nano-second?
To piggy back on Ricky's answer if you can access PC A and B but not any of the switches inbetween you could use Wireshark to capture any CDP or LLDP packets that come your way. Using the device name and other information in these packets you could at least confirm whether they are 1 switch hop apart, or 2 (or more) switch hops away from each other.
EDIT: Expanded the original answer below
You would be able to capture CDP or LLDP packets which, if enabled on the switches are sent out every 60 and 30 seconds respectively. These types of packets both have switch information inside them. For example: You enable Wireshark on PC A to capture packets. You receive an LLDP (or CDP) packet there, you expand the details in Wireshark and you will see System Name: Cisco-3750-London-1 or you could look at the System ID: de:ad:01:01:be:ef. On PC B you could do the same and see if they are the same or different System Names and IDs. Does PC B receive CDP/LLDP packets that contain System Name: Cisco-3750-London1 and de:ad:01:01:be:ef, or will it be Cisco-3550-London-Building2 with a System ID of 0c:07:08:de:ab:45?
It will only tell you for sure if it is one switch as if the Names or IDs differ then it could be 2 or 3 or 50 hops in between. It also depends on the network to have CDP or LLDP compatible switches and for it to be enabled on the host ports.