Python: 获取Linux中的Wi-Fi接口列表

huangapple go评论55阅读模式
英文:

Python: getting the list of wifi interfaces in linux

问题

获取当前wifi接口列表最简单的方法是运行一个shell命令并从中提取,但我想知道是否有更简单的方法。

英文:

Whats the most simple way to get the list of current wifi interfaces?
i guess i can just run a shell command and extract if from there, but i wonder if theres a simpler way.

答案1

得分: 1

另一个选项是使用psutil。您可以使用net_if_stats函数来获取字典键中的所有网络接口卡名称。值包含其他有用的信息,例如网卡是否正在运行(isup)。

将键筛选以匹配wlan,这应该涵盖大多数Linux情况。

英文:

Another option is using psutil. You can use e.g. net_if_stats function to get all the network interface card names in dictionary keys. Values contain other useful info, e.g. whether the card is running (isup).

Filter the keys to match wlan which should cover the majority of cases in Linux.

答案2

得分: 0

import os
os.system("ip link show")

英文:

Anyway you will have to interact with the OS and Python can do it either with os or subprocess modules.

import os
os.system("ip link show")

答案3

得分: 0

这里是翻译的内容:

"Yet another approach to the ones mentioned is to read the files that describe the network interfaces. Depending on your distro/version it could be something like: /etc/netplan or /etc/network/interfaces. There are other possibilities as well, again, it depends on your specific system. They should all contain the same type of information though. I believe tools like ip addr read from these files."

英文:

Yet another approach to the ones mentioned is to read the files that describe the network interfaces. Depending on your distro/version it could be something like: /etc/netplan or /etc/network/interfaces. There are other possibilities as well, again, it depends on your specific system. They should all contain the same type of information though. I believe tools like ip addr read from these files.

huangapple
  • 本文由 发表于 2023年3月7日 21:54:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定