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
  • 617
  • 2
  • 8
  • 13

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

Sylvain Pineau
  • 61,564
  • 18
  • 149
  • 183
  • thnks Sylvain Pineau for your response .I take your code and I try to but I have executed the same result no mac address: here is the print screen . rimeh@rimeh-PC:~$ sudo python mac.py [sudo] password for rimeh: {'status': {'state': u'up', 'reason': u'localhost-response'}, 'hostname': u'rimeh-PC', 'vendor': {}, 'addresses': {u'ipv4': u'192.168.1.117'}} 192.168.1.117 – kaio Mar 09 '15 at 16:12
  • 1. change the value of cidr2 to your own ip 2. use an ip in the same network 3. run your script with sudo – Sylvain Pineau Mar 09 '15 at 16:14
  • of course i make my own ip but no adress mac – kaio Mar 09 '15 at 16:18
  • @kaio: Try to use the ip address of your router (e.g 192.168.1.1/24). I also don't get any MAC add for my localhost – Sylvain Pineau Mar 09 '15 at 16:18
  • OK thank you so much i have the result, thanks a lot :) Sylvain Pineau – kaio Mar 09 '15 at 16:22
  • Sylvain Pineau if my ip network is x.x.x.x then ip of my routeur is x.x.x.1 – kaio Mar 09 '15 at 16:40
  • @kaio. It was just an example, it can be anything. – Sylvain Pineau Mar 09 '15 at 16:42
  • but I need to write a method that gives ip my router from my network ip local.To have display mac address if the router ip does not end with one example which other cases.@ Sylvain Pineau – kaio Mar 09 '15 at 16:47
  • @kaio the command `route -n` will tell you the ip of your router/gateway – Sylvain Pineau Mar 09 '15 at 16:51
  • But I will not use the terminal I must write a code on python to find routeur ip when I have network ip local @Sylvain Pineau – kaio Mar 09 '15 at 16:54
  • @kaio: You can use `subprocess.check_output()` from python or use a dedicated lib (See http://stackoverflow.com/questions/2761829/python-get-default-gateway-for-a-local-interface-ip-address-in-linux) – Sylvain Pineau Mar 09 '15 at 16:57
  • ,Can you help me ,when I use subprocess.check_output() the result is gws=subprocess.check_output() File "/usr/lib/python2.7/subprocess.py", line 537, in check_output process = Popen(stdout=PIPE, *popenargs, **kwargs) TypeError: __init__() takes at least 2 arguments (2 given) – kaio Mar 09 '15 at 21:52
  • @kaio: Could you please create a new question? comments are not the right place for answers. thanks – Sylvain Pineau Mar 09 '15 at 22:03
  • I did a new question @Sylvain Pineau http://askubuntu.com/questions/589205/how-can-i-determine-the-ip-address-of-my-router – kaio Mar 09 '15 at 22:08