Python 获取启用的物理网卡名称

    import psutil
    
    def get_physical_netcard():
        """获取启用的物理网卡名称"""
        stats_info = psutil.net_if_stats()
        netcard = os.listdir("/sys/class/net")
        virtual_netcard = os.listdir("/sys/devices/virtual/net/")
        physical_netcard_all = list(set(netcard) ^ set(virtual_netcard))
        physical_netcard_down = []
        for key in physical_netcard_all:
            isup, _, _, _ = stats_info[key]
            if not isup:
                physical_netcard_down.append(key)
        physical_netcard_up = list(
            set(physical_netcard_all) ^ set(physical_netcard_down)
        )
        # 启用的物理网卡 physical_netcard_up
        # 关闭的物理网卡 physical_netcard_down
        # 所有的物理网卡 physical_netcard_all
        return physical_netcard_up, physical_netcard_down, physical_netcard_all

你可能感兴趣的:(Python 获取启用的物理网卡名称)