简体   繁体   English

从网关获取MAC地址

[英]Get mac address from gateway

I want to be able to get the mac address from the gateway with a bash script. 我希望能够使用bash脚本从网关获取mac地址。

My idea was to get the gateway IP: 我的想法是获取网关IP:

netstat -nr | netstat -nr | grep default grep默认

However I get this: 但是我得到这个:

default 192.168.1.1 UGSc 77 0 en0 默认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: 我将以某种方式需要摆脱线路上的所有内容,并使其仅读取IP,因此我可以执行以下命令:

arp -n -i en0 $ip arp -n -i en0 $ ip

If anyone could help me or think of a better way of doing it that would be great! 如果有人可以帮助我或想办法做的更好,那就太好了!

在Mac OS X上有效的另一个答案是:

route get default | grep '^    gateway:' | cut -f 6 -d " " | xargs arp | cut -f 4 -d " "

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). 如果您进行了大量的bash脚本编写,您可能应该熟悉awk ,它可以执行这种类型的事情(顺便说一下,它非常强大)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM