我撰寫了這段代碼,它顯示網路上的 arp 表,并顯示網路上每個設備的多行資訊。我想知道是否有辦法提取每個設備的IP 地址和物理地址,因為它們是我唯一需要的部分。以下是我用來獲取 ARP 表的代碼:
for device in os.popen('arp -a'):
print(device)
我對 Python 還很陌生,所以我需要關于如何從運行這些代碼時獲得的表中獲取該資訊的幫助。
uj5u.com熱心網友回復:
您可以在空白處拆分線條并保留您感興趣的部分。
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/369585.html
