Obtain MAC Address from Devices using Python

前端 未结 8 956
余生分开走
余生分开走 2020-11-30 03:34

I\'m looking for a way (with python) to obtain the layer II address from a device on my local network. Layer III addresses are known.

The

8条回答
  •  情深已故
    2020-11-30 04:04

    General update for Python 3.7. Remark: the option -n for arp does not provide the arp list on windows systems as provided with certain answers for linux based systems. Use the option -a as stated in the answer here.

    from subprocess import Popen, PIPE
    
    pid = Popen(['arp', '-a', ip], stdout=PIPE, stderr=PIPE)
    
    IP, MAC, var = ((pid.communicate()[0].decode('utf-8').split('Type\r\n'))[1]).split('     ')
    IP  =  IP.strip(' ')
    MAC =  MAC.strip(' ')
    
    if ip == IP:
        print ('Remote Host : %s\n        MAC : %s' % (IP, MAC))
    

提交回复
热议问题