获取MAC地址

问题描述 投票:96回答:13

我需要一种在运行时确定计算机MAC地址的跨平台方法。对于Windows,可以使用'wmi'模块,我可以找到Linux下唯一的方法是运行ifconfig并在其输出中运行正则表达式。我不喜欢使用只能在一个操作系统上运行的软件包,而解析另一个程序的输出似乎并不优雅,更不用说容易出错了。

有谁知道跨平台方法(windows和linux)方法来获取MAC地址?如果没有,有没有人知道比我上面列出的更优雅的方法?

python windows linux networking
13个回答
142
投票

Python 2.5包含一个uuid实现(至少在一个版本中)需要mac地址。您可以轻松地将mac查找功能导入到您自己的代码中:

from uuid import getnode as get_mac
mac = get_mac()

返回值是mac地址为48位整数。


0
投票

我不知道一种统一的方式,但是你会发现一些有用的东西:

http://www.codeguru.com/Cpp/I-N/network/networkinformation/article.php/c5451

在这种情况下我要做的是将它们包装到一个函数中,并根据操作系统运行正确的命令,根据需要解析并只返回格式化的MAC地址。它的所有相同,除了你只需要做一次,它从主代码看起来更干净。


0
投票

对于Linux,让我介绍一个shell脚本,它将显示mac地址并允许更改它(MAC嗅探)。

 ifconfig eth0 | grep HWaddr |cut -dH -f2|cut -d\  -f2
 00:26:6c:df:c3:95

削减参数可能不同(我不是专家)尝试:

ifconfig etho | grep HWaddr
eth0      Link encap:Ethernet  HWaddr 00:26:6c:df:c3:95  

要更改MAC我们可能会这样做:

ifconfig eth0 down
ifconfig eth0 hw ether 00:80:48:BA:d1:30
ifconfig eth0 up

将mac地址更改为00:80:48:BA:d1:30(暂时,重启后将恢复为实际值)。


0
投票

或者,

import uuid
mac_id=(':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff)

-8
投票

对于Linux,您可以使用SIOCGIFHWADDR ioctl检索MAC地址。

struct ifreq    ifr;
uint8_t         macaddr[6];

if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0)
    return -1;

strcpy(ifr.ifr_name, "eth0");

if (ioctl(s, SIOCGIFHWADDR, (void *)&ifr) == 0) {
    if (ifr.ifr_hwaddr.sa_family == ARPHRD_ETHER) {
        memcpy(macaddr, ifr.ifr_hwaddr.sa_data, 6);
        return 0;
... etc ...

你已经标记了问题“python”。我不知道现有的Python模块来获取这些信息。您可以使用ctypes直接调用ioctl。


77
投票

Linux下用于此问题的纯python解决方案,用于获取特定本地接口的MAC,最初由vishnubob发布为评论,并在this activestate recipe上由Ben Mackey改进

#!/usr/bin/python

import fcntl, socket, struct

def getHwAddr(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]])

print getHwAddr('eth0')

这是Python 3兼容代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import fcntl
import socket
import struct


def getHwAddr(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    info = fcntl.ioctl(s.fileno(), 0x8927,  struct.pack('256s', bytes(ifname, 'utf-8')[:15]))
    return ':'.join('%02x' % b for b in info[18:24])


def main():
    print(getHwAddr('enp0s8'))


if __name__ == "__main__":
    main()

26
投票

netifaces是一个很好的模块,用于获取mac地址(和其他地址)。它是跨平台的,比使用socket或uuid更有意义。

>>> import netifaces
>>> netifaces.interfaces()
['lo', 'eth0', 'tun2']

>>> netifaces.ifaddresses('eth0')[netifaces.AF_LINK]
[{'addr': '08:00:27:50:f2:51', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]


20
投票

另外一件事你应该注意的是,uuid.getnode()可以通过返回一个随机的48位数字伪造MAC地址,这可能不是你所期望的。此外,没有明确指示MAC地址已伪造,但您可以通过调用getnode()两次并查看结果是否变化来检测它。如果两个呼叫都返回相同的值,则您拥有MAC地址,否则您将获得伪造的地址。

>>> print uuid.getnode.__doc__
Get the hardware address as a 48-bit positive integer.

    The first time this runs, it may launch a separate program, which could
    be quite slow.  If all attempts to obtain the hardware address fail, we
    choose a random 48-bit number with its eighth bit set to 1 as recommended
    in RFC 4122.

16
投票

有时我们有多个网络接口。

查找特定接口的mac地址的简单方法是:

def getmac(interface):

  try:
    mac = open('/sys/class/net/'+interface+'/address').readline()
  except:
    mac = "00:00:00:00:00:00"

  return mac[0:17]

调用方法很简单

myMAC = getmac("wlan0")

8
投票

使用我在这里的答案:https://stackoverflow.com/a/18031868/2362361

重要的是要知道你想要MAC的iface,因为许多可以存在(蓝牙,几个nics等)。

当您使用netifaces(在PyPI中提供)知道您需要MAC的iface的IP时,这可以完成这项工作:

import netifaces as nif
def mac_for_ip(ip):
    'Returns a list of MACs for interfaces that have given IP, returns None if not found'
    for i in nif.interfaces():
        addrs = nif.ifaddresses(i)
        try:
            if_mac = addrs[nif.AF_LINK][0]['addr']
            if_ip = addrs[nif.AF_INET][0]['addr']
        except IndexError, KeyError: #ignore ifaces that dont have MAC or IP
            if_mac = if_ip = None
        if if_ip == ip:
            return if_mac
    return None

测试:

>>> mac_for_ip('169.254.90.191')
'2c:41:38:0a:94:8b'

3
投票

请注意,您可以使用条件导入在python中构建自己的跨平台库。例如

import platform
if platform.system() == 'Linux':
  import LinuxMac
  mac_address = LinuxMac.get_mac_address()
elif platform.system() == 'Windows':
  # etc

这将允许您使用os.system调用或特定于平台的库。


2
投票

你可以使用跨平台的psutil来做到这一点:

import psutil

mac_addresses = []

nics = psutil.net_if_addrs()
nics.pop('lo') # remove loopback since it doesnt have a real mac address

for i in nics:
    for j in nics[i]:
        if j.family == 17:  # AF_LINK
            mac_addresses.append(j.address)

1
投票

如果您不介意依赖,那么跨平台的getmac包将适用于此。它适用于Python 2.7+和3.4+。它将尝试许多不同的方法,直到获取地址或返回None。

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)

免责声明:我是该软件包的作者。

更新(2019年1月14日):该软件包现在仅支持Python 2.7+和3.4+。如果需要使用较旧的Python(2.5,2.6,3.2,3.3),仍然可以使用旧版本的软件包。

© www.soinside.com 2019 - 2024. All rights reserved.