想获取远程PC的MAC地址

2 投票
4 回答
7532 浏览
提问于 2025-04-15 12:44

我有一个用Python做的网页,我可以获取访问我们网页的用户的IP地址。我们想知道用户电脑的MAC地址,这在Python中可以实现吗?我们使用的是Linux电脑,想在Linux上获取这个信息。

4 个回答

1

你能访问的所有信息,都是用户发给你的。

而MAC地址并不在这些信息中。

3

来自Active code

#!/usr/bin/env python

import ctypes
import socket
import struct

def get_macaddress(host):
    """ Returns the MAC address of a network host, requires >= WIN2K. """

    # Check for api availability
    try:
        SendARP = ctypes.windll.Iphlpapi.SendARP
    except:
        raise NotImplementedError('Usage only on Windows 2000 and above')

    # Doesn't work with loopbacks, but let's try and help.
    if host == '127.0.0.1' or host.lower() == 'localhost':
        host = socket.gethostname()

    # gethostbyname blocks, so use it wisely.
    try:
        inetaddr = ctypes.windll.wsock32.inet_addr(host)
        if inetaddr in (0, -1):
            raise Exception
    except:
        hostip = socket.gethostbyname(host)
        inetaddr = ctypes.windll.wsock32.inet_addr(hostip)

    buffer = ctypes.c_buffer(6)
    addlen = ctypes.c_ulong(ctypes.sizeof(buffer))
    if SendARP(inetaddr, 0, ctypes.byref(buffer), ctypes.byref(addlen)) != 0:
        raise WindowsError('Retreival of mac address(%s) - failed' % host)

    # Convert binary data into a string.
    macaddr = ''
    for intval in struct.unpack('BBBBBB', buffer):
        if intval > 15:
            replacestr = '0x'
        else:
            replacestr = 'x'
        macaddr = ''.join([macaddr, hex(intval).replace(replacestr, '')])

    return macaddr.upper()

if __name__ == '__main__':
    print 'Your mac address is %s' % get_macaddress('localhost')
5

我有一个小的、经过签名的Java小程序,它需要在远程计算机上运行Java 6环境才能工作。这个小程序使用了一个叫做 getHardwareAddress() 的方法,这个方法来自 NetworkInterface 类,用来获取MAC地址。我用JavaScript来调用这个小程序里的一个方法,这个方法会获取MAC地址并返回一个包含地址的JSON对象。然后,这个地址会被放进一个隐藏的表单字段里,和其他字段一起提交。

撰写回答