Search code examples
bashippingarpnmap

Get live hosts MAC in local network


Hi I'm working on a small bash script that will scan lan every 5 minutes and get live host and then get theirs MAC addresses.

So far I have this:

nmap -sP -n -oG - 10.0.0.1-20 | grep "Up" | awk '{print $2}'

Which gives me ip addresses. Now I have to do something like

arp -an | grep 'ip'

but I'm new to bash and I don't know how :)


Solution

  • Here is a script that does exactly what you want:

    #!/bin/bash
    
    HOSTS=$(nmap -sP -n -oG - 192.168.1.1-10 | grep "Up" | awk '{print $2}')
    
    for host in ${HOSTS}; do
      arp -an | grep ${host} | awk '{print $2 $4}'
    done