Thursday, October 20, 2011

getting an ip address

I sometimes need to know my local IP address for sharing data on my local network.
here is a oneliner that does the trick:

grep ":" /proc/net/arp |awk '{print $1}'
And a faster way as a function using only shell builtins:
while read A || [ "${A}" ]; do
    case "${A}" in
            echo "${A%% *}"
done </proc/net/arp
The second one seems like it would be slower, because it is more code.  Right?  Not really, when you account for grep and awk as "code".  They take a not-insignificant time to be found, load and then run, but lets be honest, you won't notice this if you are running it by itself from a prompt.  It could however come in handy in speeding up a boot process on a connected device.

But what if we have a local dynamic ip address and want to know what our "real" ip address is.
Kieth Hatfield has posted a server side php script and shell script to do just that here:

the server side php is pretty simple:
<?php echo $_SERVER['REMOTE_ADDR']; ?>
 and the shell script is:
wget -q -O - 2>/dev/null
 (Note: Keith's demo is at
 There are several other places to get this information such as:    (NOTE: once every 300 seconds) (NOTE: has html formatting)

No comments:

Post a Comment