This is a little script I wrote to get rid of all the non-ip-address stuff from the output of the ip addr command on Linux. It tells you the current IP and which network interface is using it.
#!/bin/bash
wifiip=$(ip addr \
| grep inet | grep wlan0 \
| awk -F" " '{print $2}' \
| sed -e 's/\/.*$//')
checketh0=$(ip addr |grep eth0 |grep DOWN &> /dev/null ; echo $?)
if [ "$checketh0" == "0" ] ; then
eth0ip="not connected"
else
eth0ip=$(ip addr \
| grep inet | grep eth0 \
| awk -F" " '{print $2}' \
| sed -e 's/\/.*$//')
fi
checketh1=$(ip addr |grep eth1 |grep DOWN &> /dev/null ; echo $?)
if [ "$checketh1" == "0" ] ; then
eth1ip="not connected"
else
eth1ip=$(ip addr \
| grep inet | grep eth1 \
| awk -F" " '{print $2}' \
| sed -e 's/\/.*$//')
fi
# report findings, only returning devices with IPs
echo "wlan0: $wifiip" | grep [0-9]$
echo "eth0: $eth0ip" | grep [0-9]$
echo "eth1: $eth1ip" | grep [0-9]$
And here’s a sample of the output:
jon@hostname:~$ myip
wlan0: 192.168.1.199
jon@hostname:~$