Shell Script To Read IP Address ( Find Ip Address Script )

in Categories Script last updated April 10, 2008
# Shell script scripts to read ip address
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit for more information.
# -------------------------------------------------------------------------
# Get OS name
IO="" # store IP
case $OS in
   Linux) IP=`ifconfig  | grep 'inet addr:'| grep -v '' | cut -d: -f2 | awk '{ print $1}'`;;
   FreeBSD|OpenBSD) IP=`ifconfig  | grep -E 'inet.[0-9]' | grep -v '' | awk '{ print $2}'` ;;
   SunOS) IP=`ifconfig -a | grep inet | grep -v '' | awk '{ print $2} '` ;;
   *) IP="Unknown";;
echo "$IP"

Share this on:

35 comment

  1. I made a minor change so that script to supports Apple Darwin (MacOSX, Leopard):

    FreeBSD|OpenBSD|Darwin) IP=`ifconfig | grep -E ‘inet.[0-9]’ | grep -v ‘’ | awk ‘{ print $2}’` ;;

    Dirt simple. Hope it works for everyone, and for Snow Leopard ;)

  2. After end of your script I added:

    if [ $IP == “” ]
    echo “HELLO!”

    but this it not working. Have I made a mistake?

    1. if [ $X == “string” ] only works in Bash (and maybe others).
      either change #!/bin/sh to #!/bin/bash or use [ “$IP” = “” ] instead.

  3. Here is my go at it:

    # Copyright (c) 2010 Sean A.O. Harney
    # This script is licensed under GNU GPL version 2.0 or above

    if [ $# -ne 1 ] ; then
    echo -e “Usage:\t$0 ” ;
    exit 1 ;

    ifconfig $1 | \
    grep ‘inet addr:’ | \
    awk ‘{ split($2, ar, “:”) ; print ar[2] }’ ;

    exit 0 ;

  4. 1. Can you write a short shell to find any IPV4 IP addresses in any files under /var/lib/*. Perform a dns reverse. Lookup for each IP found, and format the output neatly, like “IP=,“?

  5. here’s how I got the IP ADDRESS
    ADDRESS=`ifconfig eth0 | grep “intet addr” | tr ‘ ‘ ‘\n’ | grep “addr” | tr -d “addr:”`
    echo $ADDRESS
    #printf “%s\r\n” “$ADDRESS”

    then I had another shell script that called that I could have included it all in one but I did different things at different times when the customer wanted to switch between DHCP and staic
    ifconfig eth0 down
    ifconfig eth0 up
    ./ > ip.txt
    chmod 0666 ip.txt

    This way I could read ip.txt into my c application and always display the address in the customers software.

  6. The problem is a bit wider because of internationalization issues.
    I use
    ifconfig $IF | grep -i inet | head -n 1 | sed ‘s/[:a-zA-Z]//g;s/\.//’ | awk ‘{ print $1 }’
    that will handle ifconfig output for all (or almost) occidental languages under any nix I know of.
    Suggestions are welcome. :-)

  7. To Kyle Brandt:
    This will no work on Solaris as there is no -o option for egrep on said platform. Much as I love the gnu toolset, the unix toolset often has less advanced features so easy commands become harder.

    To Paul Avilles:
    This does not get you the IP address of the machine. It gets you the IP address of the machine that you are logged in from – not really helpful.

    Sorry to be so negative :(


  8. Hai,
    I tried to print the HWaddress in interface as like in the above script..i m unable to print..can u plz help me to print the HWaddress as like inet addr..

    eth1 Link encap:Ethernet HWaddr 00:0C:FC:00:3A:7E
    inet addr: Bcast: Mask:

  9. If one must parse ifconfig what about:

    ifconfig | egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}' | egrep -v '255|(127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})'

    I think I would like that more then cutting with awk or cut. Also I don’t think it matters what *nix system you are running ifconfig on.

    Also in the above example grep -v ‘’ the periods are not literal since they are not escaped, but rather they match any character.


Leave a Comment