Useful bash oneliner: List Server IP Addresses

Gravatar
Posted by apokalyptik

for i in $(/sbin/ifconfig | grep addr: | cut -d’:’ -f2 | cut -d’ ‘ -f1 | grep -Ev ‘^$’); do echo -n $i”, “; done | sed -r s/’, $’/”/g; echo

5 Responses

  1. James Byers says:

    Looks like “done” turned into “one”. I needed the same one-liner a while back, based it on ip:

    /sbin/ip addr list | sed -n ’s/inet \(.*\)\/.*/\1,/p’ | xargs | sed ’s/,$//’

    Amazing it takes stuff like this to get a list of IPs.

  2. Joseph Scott says:

    Here’s a FreeBSD version:

    /sbin/ifconfig -a | /usr/bin/grep netmask | /usr/bin/awk ‘{print $2}’

  3. apokalyptik says:

    This is what I love about the *nix way of life. there’s always so many ways of doing things. What differentiates the men from the boys (as it were) is not how something gets done but that something gets done. :D

  4. Gowtham says:

    Here’s one more (worked on CentOS/Fedora/RedHat-like distros):

    /sbin/ifconfig [OPTIONAL_DEVICE_NAME] | grep “Bcast” | awk -F ‘ ‘ ‘{print $2}’ | awk -F ‘:’ ‘{print $2}’

    -g

Trackback URL for this entry