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
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: