开发者

How to Grab MAC Address of Active Ethernet Connection in Bash Script?

开发者 https://www.devze.com 2022-12-28 05:32 出处:网络
Simple Question: How do I grab the MAC address of the active Ethernet connection in a bash script? I currently have:

Simple Question:

How do I grab the MAC address of the active Ethernet connection in a bash script?

I currently have:

set - `/sbin/ifconfig eth0 | head -1`
MAC=$5

Which outputs the MAC address of the eth0, but if it's eth1 that's active, I want that instead.

Could I beforehand execute ifconfig | grep inet but that wouldn't tell me which interface is active, just that one is active. I need to grab the line above it to tell m开发者_StackOverflow社区e which one is the active connection.

Any help would be much appreciated.

Thank you!


Found the answer:

set - `ifconfig | grep -B 1 inet | head -1`
MAC=$5

I grep'd the inet string and returned the line before. Then use head to grab the first line.


you could do something like this

ifconfig | awk '/eth/ { print $5 }'

also an option... depending on user may need to specify /sbin/ifconfig in the xargs

route | awk '/default/ { print $NF }' | xargs -I {} ifconfig {} | awk '/HWaddr/ { print $5 }'
0

精彩评论

暂无评论...
验证码 换一张
取 消