怎么用脚本获取网卡高级属性

wen 实用脚本 2

本文目录导读:

怎么用脚本获取网卡高级属性

  1. Windows 系统
  2. Linux 系统
  3. 跨平台 Python 脚本
  4. 获取特定高级属性
  5. 注意事项

要使用脚本获取网卡高级属性,主要有以下几种方法,具体取决于你的操作系统和需求:

Windows 系统

使用 PowerShell(推荐)

# 获取所有网卡的高级属性
Get-NetAdapterAdvancedProperty
# 获取特定网卡的高级属性
Get-NetAdapterAdvancedProperty -Name "以太网"
# 获取指定注册表路径下的属性
Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\*" | 
    Where-Object {$_.DriverDesc -eq "Realtek PCIe GBE Family Controller"}

使用 WMI 查询

# 通过 Win32_NetworkAdapter 获取网卡信息
Get-WmiObject -Class Win32_NetworkAdapter | 
    Where-Object {$_.NetEnabled -eq $true} | 
    Select-Object Name, Description, MACAddress, Speed, AdapterType
# 获取详细的高级属性
Get-WmiObject -Class Win32_NetworkAdapterConfiguration | 
    Where-Object {$_.IPEnabled -eq $true}

Linux 系统

使用 ethtool(查看/修改网卡高级属性)

# 查看所有网卡
ip link show
# 查看网卡高级属性
ethtool eth0
# 查看网卡驱动信息
ethtool -i eth0
# 查看网卡统计信息
ethtool -S eth0
# 查看网卡私有标记
ethtool --show-priv-flags eth0

使用 sysfs 文件系统

# 查看网卡支持的特性
cat /sys/class/net/eth0/device/vendor
cat /sys/class/net/eth0/device/device
# 查看网卡驱动
cat /sys/class/net/eth0/device/driver/module
# 查看网卡队列数量
cat /sys/class/net/eth0/queues/rx-0/rps_cpus

跨平台 Python 脚本

import psutil
import subprocess
import platform
def get_network_interfaces():
    """获取所有网卡信息"""
    interfaces = psutil.net_if_stats()
    addrs = psutil.net_if_addrs()
    for interface, stats in interfaces.items():
        print(f"接口名称: {interface}")
        print(f"状态: {'已启用' if stats.isup else '已禁用'}")
        print(f"速度: {stats.speed} Mbps")
        print(f"双工: {stats.duplex}")
        if interface in addrs:
            for addr in addrs[interface]:
                print(f"  {addr.family.name}: {addr.address}")
        print("-" * 40)
def get_advanced_properties():
    """获取网卡高级属性(仅Windows)"""
    if platform.system() == "Windows":
        import wmi
        c = wmi.WMI()
        for nic in c.Win32_NetworkAdapter():
            print(f"名称: {nic.Name}")
            print(f"描述: {nic.Description}")
            print(f"MAC: {nic.MACAddress}")
            print(f"速度: {nic.Speed}")
            print(f"状态: {nic.NetEnabled}")
            print("-" * 40)
# 执行
get_network_interfaces()
# get_advanced_properties()  # 取消注释以获取高级属性

获取特定高级属性

Windows 注册表方式

$adapterGuid = (Get-NetAdapter -Name "以太网").InterfaceGuid
$registryPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\$adapterGuid"
# 查看具体的高级属性
Get-ItemProperty -Path $registryPath | 
    Select-Object DriverDesc, *FlowControl*, *SpeedDuplex*, *WakeOnLan*

Linux 特定属性查询

# 查看流控设置
ethtool -a eth0
# 查看卸载设置
ethtool -k eth0
# 查看特定网卡参数
ethtool -c eth0
ethtool -g eth0

注意事项

  1. 权限要求:大多数操作需要管理员/root权限
  2. 网卡名称:Windows通常为"以太网"、"WLAN"等,Linux为eth0、ens33等
  3. 驱动支持:ethtool的功能取决于网卡驱动支持程度

选择哪种方法取决于你的具体需求:

  • Windows:推荐PowerShell的Get-NetAdapterAdvancedProperty
  • Linux:推荐ethtool
  • 跨平台:推荐Python的psutil库

抱歉,评论功能暂时关闭!