3

How can I get the MAC addresses of machines using python-nmap?

I'm using python-nmap to scan my local network, I can get the ip of several systems but not their MAC addresses.

How can I get the MAC addresses in the scan results?

nm = nmap.PortScanner()    
a=nm.scan(hosts=cidr2, arguments='-sP') 

for k,v in a['scan'].iteritems(): 
        if str(v['status']['state']) == 'up':
                     number_thread += 1
                     print str(v)
             try:    print str(v['addresses']['ipv4']) + ' => ' + str(v['addresses']['mac'])
             except: print str(v['addresses']['ipv4'])
kaio
  • 677

1 Answers1

1

I managed to get the MAC addresses of systems on the same network (my own local network) using the following code.

Moreover you have to run this code as root (using sudo)

#!/usr/bin/env python

import nmap

nm = nmap.PortScanner() 
cidr2='192.168.1.99/24'

a=nm.scan(hosts=cidr2, arguments='-sP') 

for k,v in a['scan'].iteritems(): 
    if str(v['status']['state']) == 'up':
        print str(v)
        try:    print str(v['addresses']['ipv4']) + ' => ' + str(v['addresses']['mac'])
        except: print str(v['addresses']['ipv4'])

Source: is it possible to get the MAC address for machine using nmap