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:
CategoryList of Unix and Linux commands
File Management cat
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities 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 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.”

Leave a Reply

Your email address will not be published. Required fields are marked *

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