Programming and Scripting :: How to find machine's IP address (localhost)?



Within a bash script, I'm trying to obtain the machine's local IP address.  My couple stabs at it hasn't work, and I haven't been able to find any working examples on the web.  I found one example that used "host" and another that uses "ipaddr", neither of which seems to work on DSL.

Does anyone know how to do this, or can otherwise point me in the right direction?

TIA,

Richard

/sbin/ifconfig
Thank for the reply, but how do I get the output of ifconfig into a variable so I can proceed to extract the ip address?

Richard

I'd suggest for you to browse some scripting guide, or the like.

ie you can typically use
MY_VAR=`ifconfig`
or
MY_VAR=$(ifconfig)

In bash, just pipe together some commands.
If you only have one net interface active something like:

#!/bin/bash
IP=`ifconfig | grep addr: | grep -v 127.0.0.1 | cut -f2 -d: | awk '{print $1}'`
echo "$IP"

Next Page...
original here.