How to Use Shell Scripts to Retrieve IP, MAC, and Speed of Linux Network Interfaces
This guide shows how to use shell commands and simple Bash scripts on CentOS 7 to list active network interfaces, extract their IPv4/IPv6 addresses, display MAC addresses, and check link speed using ip, awk, ethtool, and other standard utilities.
1) List Available Network Interfaces
Use ip combined with awk to filter interfaces whose state is UP :
[root@localhost ~]# ip ad | awk '/state UP/ {print $2}'
ens33:
ens38:2) View IP Addresses
Show each interface’s IPv4 and IPv6 addresses:
[root@localhost ~]# ip -o addr | awk '/inet/ {print $2,$4}'
lo 127.0.0.1/8
lo ::1/128
ens33 192.168.43.138/24
ens33 fe80::214:53b4:43f6:5495/64
ens38 172.25.254.130/24
ens38 fe80::c2ff:9dbc:76be:6dd9/64Or display only IPv4 addresses:
[root@localhost ~]# ip addr | grep inet | grep -v 'inet6' | awk '{print $NF, $2}'
lo 127.0.0.1/8
ens33 192.168.43.138/24
ens38 172.25.254.130/243) View MAC Addresses
Show the MAC address of a specific interface (e.g., ens33):
[root@localhost ~]# ip link show ens33 | awk '/link/ {print $2}'
00:0c:29:ee:d9To list MAC addresses for all active interfaces, you can use a small script:
# cat mac-address.sh
#!/bin/bash
ip addr | awk '/state UP/ {print $2}' | sed 's/://' | while read iface; do
echo "$iface:"
ethtool -P $iface
done4) Check Interface Speed
Use ethtool to query the speed of a particular interface:
[root@localhost ~]# ethtool ens33 | grep "Speed:"
Speed: 1000Mb/sTo obtain speeds for all active interfaces, another script can be used:
# cat port-speed.sh
#!/bin/bash
ip addr | awk '/state UP/ {print $2}' | sed 's/://' | while read iface; do
echo "$iface:"
ethtool $iface | grep "Speed:"
done5) Comprehensive NIC Information Script
The following Bash script prints the hostname, then for each active interface displays its name, IPv4/IPv6 addresses, MAC address, and link speed:
# cat nic-info.sh
#!/bin/bash
hostname
echo "-------------"
for iface in $(ip addr | awk '/state UP/ {print $2}'); do
echo "$iface"
ip addr show $iface | grep inet | awk '{printf "%s:\t%s
", $1, $2}'
ip link show $iface | grep link | awk '{printf "MAC:\t%s
", $2}'
ethtool $iface | awk '/Speed/ {printf "%s\t%s
", $1, $2}'
doneRunning the script produces a concise summary of each network interface’s IP configuration, MAC address, and negotiated speed.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
