Sunday, January 31, 2010

The eloquence of simplicity

Some things in Linux/UNIX are so simple that they become eloquent.

I wanted to know be able to get the IP address of the local machine. Sure everyone know about ifconfig, but that returns too much; I just wanted the IP address. After a couple of quick Googles, I ended found this one from commandlinefu.com:
echo -e "local:\n$(ifconfig $1 | grep -oP 'inet (add?r:)?\K(\d{1,3}\.){3}\d{1,3}')\n\npublic:\n$(curl -s sputnick-area.net/ip)";

Returns:
local:
192.168.1.15
192.168.1.16
127.0.0.1

public:
67.76.26.2

Very cool, but still a little more than I wanted.
A few tweaks and I get to:
echo -e "$(ifconfig $1 | grep -oP 'inet (add?r:)?\K(\d{1,3}\.){3}\d{1,3}')";
192.168.1.15
192.168.1.16
127.0.0.1

Then to use the other half on its own:
 echo -e "$(curl -s sputnick-area.net/ip)";
67.76.26.2

Simple, eloquent.