bashawkgreparp

Get only Mac address from IP (bash)


I want to grep the MAC address in arp -n.

I tried this: $ arp | grep 192.168.15.1 | awk '{print $3}'

But i end up like this:

00:00:00:00:00:00
00:00:00:00:00:00
00:00:00:00:00:00
00:00:00:00:00:00
00:00:00:00:00:00
00:00:00:00:00:00

I censored the macs

I want only a single MAC address, how can i get it ?


Solution

  • arp | awk '/192.168.15.1/{print $3;exit}'
    

    By using this command, you will get only 1 mac.

    If you want to adopt an input of bash script to be the addr, use the command below,

    arp -n $1 | awk -v a=$1 '$0 ~ a{print $3;exit}'
    

    use -v a=$1 to assign $1 of bash to the variable a in awk