Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get only Mac address from IP (bash)

Tags:

grep

bash

awk

arp

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 ?

like image 211
MrFlyingToasterman Avatar asked Sep 17 '25 08:09

MrFlyingToasterman


1 Answers

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

like image 75
CWLiu Avatar answered Sep 19 '25 03:09

CWLiu