Research on Physical Network Cable Connection State Detection in Linux Environment

Nov 20, 2025 · Programming · 10 views · 7.8

Keywords: Linux Network Detection | Physical Connection State | BASH Script | sys Filesystem | Ethtool Utility

Abstract: This paper provides an in-depth exploration of reliable methods for detecting the physical connection state of RJ45 network cables in Linux systems. By analyzing carrier and operstate nodes in the /sys/class/net/ filesystem and utilizing the ethtool utility, practical BASH script-based solutions are presented. The article explains the working principles of these methods, compares their advantages and disadvantages, and provides complete code examples with implementation steps.

Introduction

In modern network environments, accurately detecting the physical connection state of network cables is crucial for system monitoring and fault diagnosis. Traditional network state detection methods like ifconfig and ping have limitations in certain scenarios and cannot accurately reflect physical layer connection status. This paper explores two reliable physical connection state detection solutions based on system interfaces provided by the Linux kernel.

System File Interface Detection Method

The Linux kernel provides rich network interface information through the /sys/class/net/ directory. Among these, the carrier and operstate nodes can accurately reflect physical connection status.

The carrier node indicates physical carrier signal state:

# When cable is connected
cat /sys/class/net/eth0/carrier
1

# When cable is disconnected
cat /sys/class/net/eth0/carrier
0

The operstate node indicates operational state:

# Initial connection state
cat /sys/class/net/eth0/operstate
unknown

# After disconnection and reconnection
cat /sys/class/net/eth0/operstate
up

Complete BASH Script Implementation

Based on the above principles, we can write a complete connection state detection script:

#!/bin/bash

# Detect network interface physical connection state
check_physical_connection() {
    local interface=$1
    local carrier_file="/sys/class/net/$interface/carrier"
    local operstate_file="/sys/class/net/$interface/operstate"
    
    if [[ ! -f "$carrier_file" ]]; then
        echo "Error: Interface $interface does not exist"
        return 1
    fi
    
    local carrier=$(cat "$carrier_file" 2>/dev/null)
    local operstate=$(cat "$operstate_file" 2>/dev/null)
    
    if [[ "$carrier" == "1" ]]; then
        echo "Interface $interface: Physical connection normal"
        echo "Operational state: $operstate"
        return 0
    elif [[ "$carrier" == "0" ]]; then
        echo "Interface $interface: Physical connection disconnected"
        return 1
    else
        echo "Interface $interface: State unknown"
        return 2
    fi
}

# Batch detect all network interfaces
detect_all_interfaces() {
    for interface in /sys/class/net/*; do
        ifname=$(basename "$interface")
        # Exclude virtual interfaces
        if [[ "$ifname" != lo && ! "$ifname" =~ ^virbr && ! "$ifname" =~ ^veth ]]; then
            check_physical_connection "$ifname"
        fi
    done
}

# Main program
if [[ $# -eq 0 ]]; then
    detect_all_interfaces
else
    check_physical_connection "$1"
fi

Ethtool Utility Auxiliary Detection

In addition to system file interfaces, the ethtool utility also provides connection state detection functionality:

#!/bin/bash

# Use ethtool to detect connection state
check_link_with_ethtool() {
    local interface=$1
    
    if ! command -v ethtool >/dev/null 2>&1; then
        echo "Error: ethtool utility not installed"
        return 1
    fi
    
    local link_status=$(sudo ethtool "$interface" 2>/dev/null | grep -i "link detected")
    
    if echo "$link_status" | grep -q "yes"; then
        echo "Interface $interface: Link detection normal"
        return 0
    elif echo "$link_status" | grep -q "no"; then
        echo "Interface $interface: Link not detected"
        return 1
    else
        echo "Interface $interface: Link state unknown"
        return 2
    fi
}

# Simplified version, only get connection status
get_simple_link_status() {
    local interface=$1
    sudo ethtool "$interface" 2>/dev/null | grep -i "link detected" | awk '{print $3}'
}

Method Comparison and Analysis

Both methods have their advantages: system file interfaces offer fast response times and no external tool dependencies; ethtool provides more detailed link information but requires root privileges.

In practical applications, it's recommended to combine both methods:

#!/bin/bash

# Comprehensive detection solution
comprehensive_check() {
    local interface=$1
    
    echo "=== Interface $interface State Detection ==="
    
    # Method 1: System file detection
    echo "1. System file detection:"
    check_physical_connection "$interface"
    
    echo ""
    
    # Method 2: Ethtool detection
    echo "2. Ethtool detection:"
    check_link_with_ethtool "$interface"
    
    echo ""
    
    # Get detailed information
    echo "3. Detailed information:"
    grep "" "/sys/class/net/$interface/"* 2>/dev/null | head -10
}

Connection State in Virtualization Environments

In virtualization environments, physical connection state detection has special significance. For example, the "Replicate physical network connection state" option in VMware essentially notifies the virtual switch when the physical network interface state changes. This mechanism ensures that virtual machines can promptly update network configurations when the network environment changes.

Connection state detection in virtual environments needs to consider the following factors:

#!/bin/bash

# Specialized detection for virtual environments
check_virtual_environment() {
    # Detect if running in virtual machine
    if dmidecode -s system-manufacturer 2>/dev/null | grep -qi "vmware\|virtualbox\|kvm"; then
        echo "Running in virtualization environment"
        
        # Special handling in virtual environment
        for interface in /sys/class/net/*; do
            ifname=$(basename "$interface")
            if [[ "$ifname" =~ ^ens || "$ifname" =~ ^eth ]]; then
                echo "Detecting virtual interface: $ifname"
                check_physical_connection "$ifname"
            fi
        done
    else
        echo "Running on physical machine"
        detect_all_interfaces
    fi
}

Practical Application Scenarios

Physical connection state detection has important value in the following scenarios:

#!/bin/bash

# Network monitoring script example
network_monitor() {
    local interface="eth0"
    local last_state=""
    
    while true; do
        current_state=$(cat "/sys/class/net/$interface/carrier" 2>/dev/null)
        
        if [[ "$current_state" != "$last_state" ]]; then
            timestamp=$(date '+%Y-%m-%d %H:%M:%S')
            
            if [[ "$current_state" == "1" ]]; then
                echo "[$timestamp] Network connection restored" | tee -a /var/log/network_status.log
                # Execute actions after connection restoration
                systemctl restart network-scripts 2>/dev/null
            elif [[ "$current_state" == "0" ]]; then
                echo "[$timestamp] Network connection disconnected" | tee -a /var/log/network_status.log
                # Execute actions after connection disconnection
            fi
            
            last_state="$current_state"
        fi
        
        sleep 5
    done
}

Performance Optimization and Error Handling

In production environments, script robustness and performance need to be considered:

#!/bin/bash

# Enhanced detection script
robust_connection_check() {
    local interface=$1
    local max_retries=3
    local retry_count=0
    
    while [[ $retry_count -lt $max_retries ]]; do
        if [[ -r "/sys/class/net/$interface/carrier" ]]; then
            carrier=$(cat "/sys/class/net/$interface/carrier" 2>/dev/null)
            
            case "$carrier" in
                "1")
                    echo "Connection normal"
                    return 0
                    ;;
                "0")
                    echo "Connection disconnected"
                    return 1
                    ;;
                *)
                    echo "State read failed, retrying..."
                    retry_count=$((retry_count + 1))
                    sleep 1
                    ;;
            esac
        else
            echo "Interface not accessible"
            return 2
        fi
    done
    
    echo "Detection failed, exceeded maximum retry count"
    return 3
}

Conclusion

Through the interfaces provided by the /sys/class/net/ filesystem, we can reliably detect the physical connection state of network cables. This method does not depend on network configuration or external host reachability and can accurately reflect physical layer connection status. Combined with the use of the ethtool utility, robust network state monitoring systems can be built to meet the requirements of various application scenarios.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.