Skip to content

Instantly share code, notes, and snippets.

@scue
Created October 16, 2019 02:25
Show Gist options
  • Save scue/c12610d1318168fca96565c91802d35e to your computer and use it in GitHub Desktop.
Save scue/c12610d1318168fca96565c91802d35e to your computer and use it in GitHub Desktop.
Linux 获取物理网卡名字(Linux list physical network interfaces by shell script)
# list physical network interfaces
ls -l /sys/class/net/ | egrep -v 'virtual|total' | awk '{print $NF}' | awk -F/ '{print $NF}'
# list physical network interfaces mac addresses
for i in $(ls -l /sys/class/net/ | egrep -v 'virtual|total' | awk '{print $NF}' | awk -F/ '{print $NF}'); do ip addr show dev $i | awk '/ether/{print $2}'; done
@scue
Copy link
Author

scue commented Oct 16, 2019

Output:

$ ls -l /sys/class/net/ | egrep -v 'virtual|total' | awk '{print $NF}' | awk -F/ '{print $NF}'
em1
em2
em3
em4
p5p1
p5p2

$ for i in $(ls -l /sys/class/net/ | egrep -v 'virtual|total' | awk '{print $NF}' | awk -F/ '{print $NF}'); do ip addr show dev $i | awk '/ether/{print $2}'; done
b8:ca:3a:xx:c7:a0
b8:ca:3a:xx:c7:a1
b8:ca:3a:xx:c7:a2
b8:ca:3a:xx:c7:a3
00:1b:21:xx:2f:db
00:1b:21:xx:2f:da

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment