python - 从 Python 中的已知 MAC 地址获取本地 IP 地址?
问题描述
我在 Raspberry Pi 上运行 Python 脚本,以便从智能插头中获取测量数据。在我的脚本中,我需要编写智能插头的 IP 地址,以便检索它测量的数据。问题是我需要能够将智能插头带到不同的地方,而不必每次都对其新的本地 IP 地址进行硬编码。
我有 MAC 地址,所以我希望有一种“简单”的方法来添加几行代码并从 Python 脚本中的 MAC(?)检索本地 IP 地址。谢谢!
解决方案
这可以在子进程模块中使用 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])
推荐阅读
- javascript - 如何获取类的属性名称
- session - How in code to detect if session is enabled rather than just get an error
- python - WARN SparkContext:在同一个 JVM 中检测到多个正在运行的 SparkContext
- javascript - 将 javascript 对象列表从 QML 发送到 c++
- swift - 如何计算 Swift3 中两次之间的百分比?
- c - fgets 正在影响输出
- css - 如何从 scss 文件重建 bootstrap-grid.css
- sql - 用 case 语句更新语句 SQL
- spring-boot - common-data maven项目需要包含在两个spring boot application中
- jquery - 在网站上安装 Ckeditor 和 ResponsiveFileMenager