从IP地址获取MAC地址
Obtain MAC address from IP address
我正在学习 python 3.6.4,我正在尝试让这段代码工作...
我有一个 IP 地址列表(在我的 LAN 上),我需要为列表中的每个 IP 获取关联的 mac 地址。
当我 运行 代码时,我得到这个错误:
ip_addresses_as_bytes = str.encode(ip_addresses) TypeError: descriptor
'encode' requires a 'str' object but received a 'list'
我不知道如何将单个列表项转换为字节。
import subprocess
import re
def get_mac_addresses(ip_addresses):
for ip_address in ip_addresses:
subprocess.Popen(["ping", "-c 1", ip_address], stdout=subprocess.PIPE)
proc = subprocess.Popen("arp -a", stdout=subprocess.PIPE)
data = proc.communicate()[0]
results = []
ip_address_as_bytes = str.encode(ip_address)
type(ip_address_as_bytes) # ensure it is byte representation
ip_addresses_as_bytes = str.encode(ip_addresses)
type(ip_addresses_as_bytes)
for ip_address_as_bytes in ip_addresses_as_bytes[:]:
if ip_address_as_bytes in data:
ip_data = data[data.find(ip_address_as_bytes):]
mac = re.search(r"(([a-f\d]{1,2}:){5}[a-f\d]{1,2})", ip_data)
if mac is None:
mac = re.search(r"(([a-f\d]{1,2}-){5}[a-f\d]{1,2})", ip_data)
if mac is not None:
mac = mac.groups()[0].replace('-', ':').upper()
else:
mac = '00:00:00:00:00:00'
ip_addresses = ip_addresses_as_bytes.decode()
type(ip_addresses) # ensure it is string representation
ip_address = ip_address_as_bytes.decode()
type(ip_address) # ensure it is string representation
ip_addresses.remove(ip_address)
results += [[ip_address, mac]]
return results
感谢任何帮助。
谢谢!
您的问题在这里:
ip_addresses_as_bytes = str.encode(ip_addresses)
ip_addresses
是一个列表,encode 需要一个字符串。
使用循环:
ip_addresses_as_bytes = []
for ip_addresse in ip_addresses:
ip_addresses_as_bytes.append(str.encode(ip_addresses))
我正在学习 python 3.6.4,我正在尝试让这段代码工作...
我有一个 IP 地址列表(在我的 LAN 上),我需要为列表中的每个 IP 获取关联的 mac 地址。
当我 运行 代码时,我得到这个错误:
ip_addresses_as_bytes = str.encode(ip_addresses) TypeError: descriptor 'encode' requires a 'str' object but received a 'list'
我不知道如何将单个列表项转换为字节。
import subprocess
import re
def get_mac_addresses(ip_addresses):
for ip_address in ip_addresses:
subprocess.Popen(["ping", "-c 1", ip_address], stdout=subprocess.PIPE)
proc = subprocess.Popen("arp -a", stdout=subprocess.PIPE)
data = proc.communicate()[0]
results = []
ip_address_as_bytes = str.encode(ip_address)
type(ip_address_as_bytes) # ensure it is byte representation
ip_addresses_as_bytes = str.encode(ip_addresses)
type(ip_addresses_as_bytes)
for ip_address_as_bytes in ip_addresses_as_bytes[:]:
if ip_address_as_bytes in data:
ip_data = data[data.find(ip_address_as_bytes):]
mac = re.search(r"(([a-f\d]{1,2}:){5}[a-f\d]{1,2})", ip_data)
if mac is None:
mac = re.search(r"(([a-f\d]{1,2}-){5}[a-f\d]{1,2})", ip_data)
if mac is not None:
mac = mac.groups()[0].replace('-', ':').upper()
else:
mac = '00:00:00:00:00:00'
ip_addresses = ip_addresses_as_bytes.decode()
type(ip_addresses) # ensure it is string representation
ip_address = ip_address_as_bytes.decode()
type(ip_address) # ensure it is string representation
ip_addresses.remove(ip_address)
results += [[ip_address, mac]]
return results
感谢任何帮助。
谢谢!
您的问题在这里:
ip_addresses_as_bytes = str.encode(ip_addresses)
ip_addresses
是一个列表,encode 需要一个字符串。
使用循环:
ip_addresses_as_bytes = []
for ip_addresse in ip_addresses:
ip_addresses_as_bytes.append(str.encode(ip_addresses))