英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论