我正在使用Windows 7和Python 2.6。我想获得我的网络接口的MAC地址。
我尝试过使用wmi
模块:
def get_mac_address():
c = wmi.WMI ()
for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):
return interface.MACAddress
但是,在没有互联网连接的情况下执行时会遇到问题
我尝试过使用uuid
模块:
from uuid import getnode
print getnode()
但是,返回值是MAC地址的48字节表示
66610803803052
1)我应该如何将给定的数字转换为ff:ff:ff:ff:ff:ff
格式?
2)有更好的方法来获取MAC地址吗?
答案 0 :(得分:6)
尝试使用Python 2:
import uuid
def get_mac():
mac_num = hex(uuid.getnode()).replace('0x', '').upper()
mac = '-'.join(mac_num[i : i + 2] for i in range(0, 11, 2))
return mac
print get_mac()
如果您使用的是Python 3,请尝试以下操作:
import uuid
def get_mac():
mac_num = hex(uuid.getnode()).replace('0x', '').upper()
mac = '-'.join(mac_num[i: i + 2] for i in range(0, 11, 2))
return mac
print (get_mac())
答案 1 :(得分:4)
这有效:
>>> address = 1234567890
>>> h = iter(hex(address)[2:].zfill(12))
>>> ":".join(i + next(h) for i in h)
'00:00:49:96:02:d2'
或者:
>>> "".join(c + ":" if i % 2 else c for i, c in enumerate(hex(address)[2:].zfill(12)))[:-1]
'00:00:49:96:02:d2'
或者:
>>> h = hex(address)[2:].zfill(12)
>>> ":".join(i + j for i, j in zip(h[::2], h[1::2]))
'00:00:49:96:02:d2'
首先将数字转换为十六进制,将其转换为12个字符,然后将其转换为一系列两个字符串,然后将它们与冒号连接。但是,这取决于MAC查找方法的准确性。
答案 2 :(得分:2)
使用Python 3规范:
>>> import uuid
>>> mac_addr = hex(uuid.getnode()).replace('0x', '')
>>> print(mac_addr)
>>> 94de801e0e87
>>> ':'.join(mac_addr[i : i + 2] for i in range(0, 11, 2))
>>> '94:de:80:1e:0e:87
或者
print(':'.join(['{:02x}'.format((uuid.getnode() >> i) & 0xff) for i in range(0,8*6,8)][::-1]))
答案 3 :(得分:1)
来自给定接口名称的Mac地址:
https://gist.github.com/JayZar21/6f3fd031430d865d208c29ba55ce7ccb
# Python 2:
import socket
import fcntl
import struct
def get_hw_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15]))
return ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1]
# Python 3:
import fcntl
import socket
import struct
import binascii
def get_hw_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytes(ifname[:15], 'utf-8')))
return ''.join(l + ':' * (n % 2 == 1) for n, l in enumerate(binascii.hexlify(info[18:24]).decode('utf-8')))[:-1]
用法示例:
get_hw_address("eth0")
答案 4 :(得分:0)
代码:
import re
from uuid import getnode
# to get physical address:
original_mac_address = getnode()
print("MAC Address: " + str(original_mac_address)) # this output is in raw format
#convert raw format into hex format
hex_mac_address = str(":".join(re.findall('..', '%012x' % original_mac_address)))
print("HEX MAC Address: " + hex_mac_address)
输出:
答案 5 :(得分:0)
如果您有多个网络接口,则不能依赖uuid
模块。
有一个跨平台的第三方库,名为getmac
安装:
pip install getmac
用法:
from getmac import get_mac_address
eth_mac = get_mac_address(interface="eth0")
win_mac = get_mac_address(interface="Ethernet 3")
ip_mac = get_mac_address(ip="192.168.0.1")
ip6_mac = get_mac_address(ip6="::1")
host_mac = get_mac_address(hostname="localhost")
updated_mac = get_mac_address(ip="10.0.0.1", network_request=True)
答案 6 :(得分:0)
没有第三方,聚会晚了! python 3,仅Windows解决方案:
import subprocess, re
ipconfig_all = subprocess.check_output('ipconfig /all').decode()
mac_addr_pattern = re.compile(r'(?:[0-9a-fA-F]-?){12}')
mac_addr_list = re.findall(mac_addr_pattern, ipconfig_all)
print(mac_addr_list)