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

by on April 10, 2008 · 26 comments

#!/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"
4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

{ 25 comments… read them below or add one }

21 Mitur Binesderti September 10, 2010

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

22 D Sama October 7, 2010

most of them work for me.

23 Michael January 8, 2012

IO=”" # store IP

should be IP=”"

24 Marissa March 3, 2012

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.

25 Max April 19, 2012

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

Leave a Comment

You can use these HTML tags and attributes for UNIX commands or shell scripts: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 13 + 8 ?
Please leave these two fields as-is:
Are you a human being? Solve the simple math so we know that you are a human and not a script.



Tagged as: awk command, case ... esac command, case control, find freebsd ip address, find IP address, find linux ip address, find sunos ip address, find unix ip address, freebsd, grep command, ifconfig command, linux, sun solaris, sunos, unix

Previous Script:

Next Script: