Script Categories

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

Posted in 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"

Download - Email this to a friend - Printable version

Discussion on This Shell Script:

  1. EstebanWeb.cl Says:

    Another way, but I think that is better, because in your way, it will give you your local IP, like 192.168.0.101…

    you need to have “links2″ already intalled

    #!/bin/sh
    IP_ACTUAL=`links2 -dump http://www.whatismyip.com | grep “Your IP ” | awk ‘{ print $5 }’`
    echo $IP_ACTUAL

  2. EstebanWeb.cl Says:

    And also it doesn’t work in Ubuntu 8.04 in another languages, like spanish

  3. Kyle Brandt Says:

    If one must parse ifconfig what about:

    ifconfig | egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}’ | egrep -v ‘255|(127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})’

    I think I would like that more then cutting with awk or cut. Also I don’t think it matters what *nix system you are running ifconfig on.

    Also in the above example grep -v ‘127.0.0.1′ the periods are not literal since they are not escaped, but rather they match any character.

    -Kyle

  4. lied Says:

    You should replace “ifconfig” with “/sbin/ifconfig” normaly /sbin isn’t in $PATH.

  5. Andrey Says:

    /sbin/ifconfig eth0 | awk ‘/inet/ {print $2}’ | sed ’s/addr://’

  6. pooppp Says:

    Hai,
    I tried to print the HWaddress in interface as like in the above script..i m unable to print..can u plz help me to print the HWaddress as like inet addr..

    eth1 Link encap:Ethernet HWaddr 00:0C:FC:00:3A:7E
    inet addr:17.1.1.150 Bcast:17.255.255.255 Mask:255.0.0.0

  7. vivek Says:

    Try
    ifconfig eth0 | grep HWaddr | awk -F'HWaddr' '{ print $2}'

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tags: , , , , , , , , , , , , , , ~ Last updated on: April 10, 2008