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

by on April 10, 2008 · 32 comments

  1. #!/bin/sh
  2. # Shell script scripts to read ip address
  3. # -------------------------------------------------------------------------
  4. # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
  5. # This script is licensed under GNU GPL version 2.0 or above
  6. # -------------------------------------------------------------------------
  7. # This script is part of nixCraft shell script collection (NSSC)
  8. # Visit http://bash.cyberciti.biz/ for more information.
  9. # -------------------------------------------------------------------------
  10. # Get OS name
  11. OS=`uname`
  12. IO="" # store IP
  13. case $OS in
  14. Linux) IP=`ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`;;
  15. FreeBSD|OpenBSD) IP=`ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'` ;;
  16. SunOS) IP=`ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2} '` ;;
  17. *) IP="Unknown";;
  18. esac
  19. echo "$IP"


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • Mitur Binesderti

    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.”

  • Mitur Binesderti

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

  • D Sama

    most of them work for me.

  • Michael

    IO=”” # store IP

    should be IP=””

  • Marissa

    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.

  • Max

    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….

  • Gourav Joshi

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

  • David Va

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

    simple! ;)

  • none

    What about extracting IP address from a tcpdump output file?

  • James

    I added this for OSX

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

  • Smart1

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

  • jagaines

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

Previous Script:

Next Script: