1

does anyone know how in python in linux to get the mac address of something that's plugged directoy into the ethernet port where either it does not yet have an ip address or we do not yet know its ip address?

I know it's possible as dhcp servers do it.

thank you

i used socket programming in python and the returned information contained only the mac address of the interface but not the mac address of anything plugged into it.

i used this code which i found on the web

#!/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]))
    print (info)
    return ':'.join('%02x' % b for b in info[18:24])


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


if __name__ == "__main__":
    main()

fjb59
  • 19
  • 2

0 Answers0