≡ Menu

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

# 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 http://bash.cyberciti.biz/ 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" 

Comments on this entry are closed.

  • 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

    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?!!!!!).

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

  • 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 ‘’ | 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.

  • katsu

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

  • $37050349

    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

  • Dheeraj Jonnalagadda

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