Skip to content

Instantly share code, notes, and snippets.

@forestbaker
Forked from wozoopa/showips
Last active October 11, 2015 20:34
Show Gist options
  • Save forestbaker/82e15604ff619dd4f277 to your computer and use it in GitHub Desktop.
Save forestbaker/82e15604ff619dd4f277 to your computer and use it in GitHub Desktop.
Get ip addresses for each interface in linux with bash function.
declare -rx IFC='/sbin/ifconfig'
showips() {
NAMES=( $($IFC | egrep 'lo|eth|wlan|en' -A 1 | awk -F' ' '{print $1 }' | egrep -v 'inet|-|UP|RX|collisions' | sort -u) )
for i in "${NAMES[@]}"; do
echo "$i has ip address: $($IFC | grep $i -A 1 | grep 'addr' | awk -F' ' '{print $2}' | awk -F':' '{print $2}')"
done
}
# alternative to above
# hmm ... more testing ...
for i in {0..9}; do
ip addr list eth${i} | grep 'inet ' | tr -s ' ' | cut -d ' ' -f 3 | cut -d '/' -f 1)
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment