macosterminalip

Get mac address from gateway


I want to be able to get the mac address from the gateway with a bash script.

My idea was to get the gateway IP:

netstat -nr | grep default

However I get this:

default 192.168.1.1 UGSc 77 0 en0

I would somehow need to get rid of everything on the line and make it just read the IP so then I could then do the following command:

arp -n -i en0 $ip

If anyone could help me or think of a better way of doing it that would be great!


Solution

  • Please try

    netstat -nr | grep default | awk '{print $1}'
    

    If you do lots of bash scripting, you should probably get familiar with awk, which does this type of things (and is pretty mighty by the way).