【问题标题】:How to extract the ip address and physical address a arp table for each device on a network in Python如何在Python中为网络上的每个设备提取ip地址和物理地址的arp表
【发布时间】:2022-01-05 01:42:06
【问题描述】:

我编写了这段代码来显示网络上的 arp 表,并在多行上显示网络上每个设备的信息。我想知道是否有一种方法可以提取每个设备的 IP 地址物理地址,因为它们是我唯一需要的部分。以下是我用来获取 ARP 表的代码:

for device in os.popen('arp -a'):
    print(device)

我对 Python 还很陌生,因此我需要有关如何从运行这些代码时获得的表中获取该信息的帮助。

【问题讨论】:

  • 你会得到string,所以你可以在上面使用字符串函数——即。 split()、切片[start:end]、正则表达式等。我将从device.split(" ")开始

标签: python string substring arp


【解决方案1】:

您可以在空白处分割线并保留您感兴趣的部分。

for device in os.popen('arp -a'):
    # example output: xxxx (192.168.1.254) at xx:xx:xx:xx:xx:xx [ether] on wlp..
    _, ip, _, phy, _ = device.split(maxsplit=4)
    # remove the paranthesis around the ip address
    ip = ip.strip('()')
    print(ip, phy)

【讨论】:

  • 谢谢,windows上的arp表有点不同,但我设法做到了
猜你喜欢
  • 2014-05-25
  • 1970-01-01
  • 2014-09-14
  • 2011-09-06
  • 1970-01-01
  • 1970-01-01
  • 2019-02-03
  • 2015-02-26
  • 1970-01-01
相关资源
最近更新 更多