2

我在python中有我的网页,我能够获取将访问我们网页的用户的IP地址,我们想获取用户PC的mac地址,在python中是否可以,我们使用的是Linux PC,我们想在 Linux 上安装它。

4

4 回答 4

5

我有一个小型的已签名 Java Applet,它需要远程计算机上的 Java 6 运行时才能执行此操作。它使用NetworkInterface上的getHardwareAddress()方法来获取 MAC 地址。我使用 javascript 访问小程序中的一个方法,该方法调用它并返回一个包含地址的 JSON 对象。这被填充到表单中的隐藏字段中,并与其余字段一起发布。

于 2009-07-07T13:41:33.280 回答
3

来自活动代码

#!/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')
于 2009-07-07T13:39:04.700 回答
1

您可以访问的只是用户发送给您的内容。

MAC 地址不是该数据的一部分。

于 2009-07-07T13:38:25.513 回答
0

SO 上已经提到dpkt包。它允许解析 TCP/IP 数据包。不过,我还没有将它用于您的情况。

于 2009-07-07T13:48:37.217 回答