How to find machine's IP address (localhost)?


Forum: Programming and Scripting
Topic: How to find machine's IP address (localhost)?
started by: rlr0304

Posted by rlr0304 on Jan. 16 2007,02:09
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

Posted by mikshaw on Jan. 16 2007,04:03
/sbin/ifconfig
Posted by rlr0304 on Jan. 16 2007,05:56
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

Posted by ^thehatsrule^ on Jan. 16 2007,06:23
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)

Posted by roberts on Jan. 16 2007,13:21
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"

Posted by rlr0304 on Jan. 16 2007,15:24
Thanks guys!  :D

I see my mistake -- I was trying to redirect the results of ifconfig to the variable (something like ifconfig >myVar) rather than assigning the results to the variable.

Richard

Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.