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

#!/bin/sh
# Shell script scripts to read ip address
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Get OS name
OS=`uname`
IO="" # store IP
case $OS in
   Linux) IP=`ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`;;
   FreeBSD|OpenBSD) IP=`ifconfig  | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'` ;;
   SunOS) IP=`ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2} '` ;;
   *) IP="Unknown";;
esac
echo "$IP"
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics via:
Category List of Unix and Linux commands
Disk space analyzers df ncdu pydf
File Management cat tree
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities NetHogs dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
35 comments… add one
  • Dheeraj Jonnalagadda Jun 16, 2015 @ 21:15

    Gourav Do i have to just writing the line in the script and execute it?

  • $37050349 Apr 11, 2015 @ 0:14

    OS X

    Private IP
    `ipconfig getifaddr $1`

    $1=en0 || en1 || en*

    Public IP
    `curl $1`

    $1=http://wgetip.com || http://icanhazip.com || http://ip-addr.es || http://wtfismyip.com/text || http://ifconfig.me || ident.me

  • Smart1 Jan 21, 2013 @ 15:56

    plz tell me how can i use it in my blog… to show visiter there’s ip address

  • James Aug 19, 2012 @ 20:07

    I added this for OSX

    OSX) LOCALIP=`ifconfig | grep ‘inet’| grep -v ‘127.0.0.1’ | cut -d: -f2 | awk ‘{ print $2}’`;;

  • none Jul 10, 2012 @ 1:26

    What about extracting IP address from a tcpdump output file?

  • David Va Jun 9, 2012 @ 5:10

    route -n | grep UG | awk ‘{print $2}’

    simple! ;)

    • jagaines Jan 30, 2013 @ 22:02

      this gives you the IP of your gateway, not you local IP.

  • Gourav Joshi May 23, 2012 @ 13:24

    ip addr | grep inet | grep -v inet6 | awk ‘{print $2}’ | cut -d “/” -f1

  • Max Apr 19, 2012 @ 14:43

    Can be useful on another alerting script in which you are sending the IP of the server is having some problem….
    This is my opinion….

  • Marissa Mar 3, 2012 @ 11:05

    bsr,
    can any1 plzzzzz tell me wat’s the utility of this script (it gives the local address i know!! but y know my local address?!!!!!).
    Thks.

  • Michael Jan 8, 2012 @ 17:39

    IO=”” # store IP

    should be IP=””

  • Mitur Binesderti Sep 10, 2010 @ 3:38

    P.S. Not to mention your example DOESN’T EVEN WORK!

    • D Sama Oct 7, 2010 @ 2:32

      most of them work for me.

  • Mitur Binesderti Sep 10, 2010 @ 3:30

    Stefan, why would you give a partial answer? Why not take .1 of a second more and post an example that actually works? It amazes me how stupid smart people can be.

    “Here’s part of an answer to something but I can’t be bothered to actually post a working example. You should see my code comments, they suck.”

  • Stefan Lasiewski Apr 5, 2010 @ 23:27

    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 ‘127.0.0.1’ | awk ‘{ print $2}’` ;;

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

  • livingston.JPS Mar 12, 2010 @ 10:50

    IP=`getip $Hostname` (you can use this syntax in HPUX)

  • livingston.JPS Mar 12, 2010 @ 10:47

    IP=`/usr/sbin/ifconfig -a | grep ‘broadcast’| awk ‘{ print $2}’`

  • Iurie Feb 22, 2010 @ 11:29

    After end of your script I added:

    if [ $IP == “192.168.20.101” ]
    then
    echo “HELLO!”
    fi

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

    • katsu Feb 18, 2015 @ 5:41

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

  • Sean Jan 22, 2010 @ 3:36

    Here is my go at it:

    #!/bin/sh
    # 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 ;
    fi

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

    exit 0 ;

  • Leon King Nov 24, 2009 @ 3:02

    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=192.168.0.1, hostname=jo.blogg.com. http://jo.blog.com“?

  • Tim Silvey Oct 27, 2009 @ 22:07

    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
    ./filename.sh > 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.

  • Alexandre Aguiar Sep 1, 2009 @ 18:02

    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. :-)

  • Tom Jun 25, 2009 @ 15:02

    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 :(

    Tom…

  • Paul Aviles Jun 4, 2009 @ 12:17

    A very simple way
    who | cut -d”(” -f2 | cut -d”)” -f1

    Enjoy

    Paul

  • Peteris Krumins Sep 25, 2008 @ 16:02

    I wrote a post how to extract IP Address with 4 different tools – sed, awk, perl 5.8 and perl 5.10:

    http://www.catonmat.net/blog/golfing-the-extraction-of-ip-addresses-from-ifconfig/

    Pete

  • Bubuntu Aug 15, 2008 @ 12:41

    for ubuntu:

    ifconfig | grep 'inet adr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'

  • vivek Jul 31, 2008 @ 21:42

    Try
    ifconfig eth0 | grep HWaddr | awk -F'HWaddr' '{ print $2}'

  • pooppp Jul 31, 2008 @ 12:43

    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:17.1.1.150 Bcast:17.255.255.255 Mask:255.0.0.0

  • Andrey Jul 17, 2008 @ 9:36

    /sbin/ifconfig eth0 | awk ‘/inet/ {print $2}’ | sed ‘s/addr://’

  • lied Jun 23, 2008 @ 14:14

    You should replace “ifconfig” with “/sbin/ifconfig” normaly /sbin isn’t in $PATH.

  • Kyle Brandt Jun 18, 2008 @ 2:45

    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 ‘127.0.0.1’ the periods are not literal since they are not escaped, but rather they match any character.

    -Kyle

  • EstebanWeb.cl May 8, 2008 @ 1:46

    And also it doesn’t work in Ubuntu 8.04 in another languages, like spanish

  • EstebanWeb.cl May 8, 2008 @ 0:16

    Another way, but I think that is better, because in your way, it will give you your local IP, like 192.168.0.101…

    you need to have “links2” already intalled

    #!/bin/sh
    IP_ACTUAL=`links2 -dump http://www.whatismyip.com | grep “Your IP ” | awk ‘{ print $5 }’`
    echo $IP_ACTUAL

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.