I have my web page in python, i am able to get the ip address of the user, who will be accessing our web page, we want to get the mac address of the user's PC, is it possible in python, we are using linux PC, we want to get it on linux.
I have a small, signed Java Applet, which requires Java 6 runtime on the remote computer to do this. It uses the getHardwareAddress() method on NetworkInterface to obtain the MAC address. I use javascript to access a method in the applet that calls this and returns a JSON object containing the address. This gets stuffed into a hidden field in the form and posted with the rest of the fields.
#!/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')
-
3This will only work on the local network since the ARP won't traverse a network boundary. – tvanfosson Jul 7 '09 at 13:42
-
Yes I agree with that .. Might be it will helpful for him or i need delete this – joe Jul 7 '09 at 13:59
All you can access is what the user sends to you.
MAC address is not part of that data.
-
1I should've been more precise, all your website can see is what the web server reveals to you. From what I've seen, the MAC address isn't part of that data. – databyss Jul 7 '09 at 13:45
-
@Roberto: AFAIK the MAC is part of the ethernet protocol, not the IP. – unbeknown Jul 7 '09 at 13:47
-
8MAC address is always sent with the Ethernet packet, but it stops at the router boundary when the packet is forwarded to a different wire. If the client is on the same physical network you can do an ARP to get the address corresponding to the IP address, but that's of only limited utility. – tvanfosson Jul 7 '09 at 13:51
The dpkt package was already mentioned on SO. It allows for parsing TCP/IP packets. I have not yet used it for your case, though.