≡ Menu

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

#!/bin/sh
# 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
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" 
{ 35 comments… add one }
  • Dheeraj Jonnalagadda June 16, 2015, 9:15 pm

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

  • $37050349 April 11, 2015, 12:14 am

    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 January 21, 2013, 3:56 pm

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

  • James August 19, 2012, 8:07 pm

    I added this for OSX

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

  • none July 10, 2012, 1:26 am

    What about extracting IP address from a tcpdump output file?

  • David Va June 9, 2012, 5:10 am

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

    simple! ;)

    • jagaines January 30, 2013, 10:02 pm

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

  • Gourav Joshi May 23, 2012, 1:24 pm

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

  • Max April 19, 2012, 2:43 pm

    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 March 3, 2012, 11:05 am

    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 January 8, 2012, 5:39 pm

    IO=”” # store IP

    should be IP=””

  • Mitur Binesderti September 10, 2010, 3:38 am

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

    • D Sama October 7, 2010, 2:32 am

      most of them work for me.

  • Mitur Binesderti September 10, 2010, 3:30 am

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

Security: Are you a robot or human?

Leave a Comment