Getting IP and MAC Address from BASH/Shell Script

Ever wondered how to get your IP address and MAC address and then use it in a Linux shell script? Here’s how:

#!/bin/sh
# showIPandMAC.sh

IP=$(ip addr show wlan0 | awk '/inet / {print $2}' | cut -d/ -f 1)
MAC=$(ip link show wlan0 | awk '/ether/ {print $2}')

echo "IP Address: $IP"
echo "MAC Address: $MAC"

# EndOfFile

You can change the wlan0 into whatever device you have, e.g. eth0.

Advertisements