首页 > 解决方案 > 从 Python 中的已知 MAC 地址获取本地 IP 地址?

问题描述

我在 Raspberry Pi 上运行 Python 脚本,以便从智能插头中获取测量数据。在我的脚本中,我需要编写智能插头的 IP 地址,以便检索它测量的数据。问题是我需要能够将智能插头带到不同的地方,而不必每次都对其新的本地 IP 地址进行硬编码。

我有 MAC 地址,所以我希望有一种“简单”的方法来添加几行代码并从 Python 脚本中的 MAC(?)检索本地 IP 地址。谢谢!

标签: pythonip-addresshostnamemac-address

解决方案


这可以在子进程模块中使用 arp 命令来实现。这是代码。签入窗口。

import subprocess
cmd = 'arp -a | findstr "ff-ff-ff-ff-ff-ff" '
returned_output = subprocess.check_output((cmd),shell=True,stderr=subprocess.STDOUT)
print(returned_output)
parse=str(returned_output).split(' ',1)
ip=parse[1].split(' ')
print(ip[1])

推荐阅读