How to Query Switch Port MAC Address from Linux Using ARP and DHCP Techniques


1 views

When troubleshooting network connectivity or documenting infrastructure, identifying the exact switch port your Linux server connects to is crucial. The MAC address of the upstream switch port serves as a unique identifier for this physical connection.

Before attempting to retrieve the switch port MAC, ensure you understand:

  • ARP (Address Resolution Protocol) table operations
  • DHCP lease mechanics
  • Basic Layer 2 network topology

The most direct approach examines your server's ARP table. The default gateway (typically a switch in small networks) will appear here:

arp -an | grep -i "gateway_ip"

Example output showing the switch port MAC:

? (192.168.1.1) at 00:1a:2b:3c:4d:5e [ether] on eth0

For more detailed topology information, install the lldpd package:

sudo apt install lldpd
sudo systemctl start lldpd
lldpcli show neighbors

As noted in the original scenario, DHCP plays a critical role in MAC address visibility. When static IPs prevent proper MAC discovery:

  1. Temporarily enable DHCP on the host
  2. Force a DHCP request: dhclient -r eth0 && dhclient eth0
  3. Check the switch's MAC address table

Cross-verify using the switch's administrative interface:

show mac address-table interface gigabitethernet1/0/24

Create a bash script for regular monitoring:

#!/bin/bash
INTERFACE="eth0"
GATEWAY=$(ip route | grep default | awk '{print $3}')
MAC=$(arp -n $GATEWAY | awk '{print $3}')

echo "Connected to switch port with MAC: $MAC"
  • If MAC addresses aren't appearing, check spanning-tree port states
  • Verify VLAN configurations match on both endpoints
  • Test with tcpdump when needed: tcpdump -i eth0 -n -e

When troubleshooting network connectivity or setting up security policies, knowing the MAC address of your upstream switch port becomes crucial. Many administrators face this scenario when working with statically configured Linux servers where traditional DHCP-based MAC discovery fails.

The simplest approach checks your server's ARP table for the gateway's MAC (which typically belongs to your switch):

arp -n | grep $(ip route show default | awk '/default/ {print $3}')

Example output:

192.168.1.1              ether   aa:bb:cc:dd:ee:ff   C                     eth0

For managed switches supporting LLDP (Layer 2 Discovery Protocol), install the lldpd package:

sudo apt install lldpd
sudo systemctl start lldpd
lldpcli show neighbors

Sample output showing switch port details:

-----------------------------------------------------------------------
LLDP neighbors:
-----------------------------------------------------------------------
Interface:    eth0, via: LLDP, RID: 1, Time: 0 day, 00:03:45
  Chassis:     
    ChassisID:    mac 00:11:22:33:44:55
    SysName:      switch1.example.com
  Port:        
    PortID:       ifname Gi1/0/24
    PortDescr:    Server uplink
-----------------------------------------------------------------------

When you have console access to the switch, check its MAC address table:

show mac address-table interface gigabitethernet 1/0/24

As mentioned in your update, static IP configurations may prevent switches from learning MAC addresses properly. The workaround involves:

  1. Temporarily enabling DHCP
  2. Letting the switch populate its MAC table
  3. Reverting to static IP while preserving the binding

Here's a script combining multiple discovery methods:

#!/usr/bin/env python3
import subprocess
import re

def get_switch_mac():
    try:
        # Try LLDP first
        lldp = subprocess.check_output(['lldpcli', 'show', 'neighbors'])
        if b'ChassisID' in lldp:
            return re.search(b'ChassisID:.*mac (([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2})', lldp).group(1).decode()
    except:
        pass
    
    # Fallback to ARP
    gw = subprocess.check_output(['ip', 'route', 'show', 'default']).split()[2]
    arp = subprocess.check_output(['arp', '-n']).decode()
    for line in arp.splitlines():
        if gw in line:
            return line.split()[2]
    
    return None

print(f"Upstream switch MAC: {get_switch_mac()}")

Remember that MAC addresses can be spoofed. For critical operations, combine MAC verification with other authentication methods like certificate-based network access control (802.1X).