2009-05-12

A friend asked me how to retrieve your IP-number in emacs; he needs it to
figure out in which of various networks he lives, to update proxy settings and the like. He
had found a decade-old function get-ip-addresson the internet, and
thought (correctly!) that it can't be that hard.

So, fortunately, it wasn't too hard to do it a bit better, esp. with all the
improvements in emacs in the last ten years.

The somewhat-ugly-but-it-works solution is to use the output of the ifconfig
tool:

Now, calling (get-ip-address "eth0") or (get-ip-address "lo") in your scripts
will get you the IP-number. Obviously, this only works on systems that have
this ifconfig, and is also vulnerable to small changes in the output. Don't even ask about IPv6.

The solution does give us a nice example of using shell-command-to-string,
which is really useful function to integrate with all kinds of external tools;
the difference with decade-old get=ip-address is striking.

However, we can do even better in these modern times. More recent versions of
emacs provide nice networking functionality:

All fine - but unfortunately, this does not work on Windows; there is no such
thing as network-interface-info there, not even in the latest Emacs
incarnations. Is there nothing else we can do on Windows? Well… we can go
back to the first solution (using shell-command-to-string, and see if we can
use it with Windows' ipconfig tool. Something like:

The Windows version does not support choosing the interface; I'll leave that
as an excercise to a reader with some more Win-fu; that same reader might also
have a solution that does not involve ipconfig, but uses some Win32-API. And
of course, that user is invited to help the emacs development team to add a
Win32 version of network-interface-info.

Nice Article.M-x ifconfig" is a part of emacs already.it does not solve the 'get me my ip address' problem though; running either ifconfig or ipconfig is the easy part.And Get my internet ipaddress in the www.ip-details.com.