Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Answer: How do I get the local internet IP address?

by Perlbotics (Chancellor)
on Aug 13, 2008 at 22:31 UTC ( #704242=categorized answer: print w/replies, xml ) Need Help??

Q&A > network programming > How do I get the local internet IP address? - Answer contributed by Perlbotics

Sometimes, firewalls block a connection setup to a well known IP address. When using the resolve mechanisms it is assumed that the information — e.g. in /etc/hosts, name servers, etc. — is maintained, accessible, trusted, and valid.

If both methods fail, a look at the actual IP configuration might help. As a (okay, maybe not the very) last resort, esp. when multiple interfaces and IPMP is concerned, examining the output of /sbin/ifconfig -a (*NIX), or ipconfig /all (WIN*) might work to reveal the interfaces currently plumbed and configured. At least the OS's tool which is responsible to configure the IPs should be able to give a reliable answer. The fragile part is to rely on the outputs layout.
You might need to change the locale language (e.g. LANG, LC_ALL) beforehand.

Illustrative code that was tested on Solaris 10, openSUSE 10.2, and OS X:
#!/usr/bin/perl use strict; use Data::Dumper; my $interface; my %IPs; foreach ( qx{ (LC_ALL=C /sbin/ifconfig -a 2>&1) } ) { $interface = $1 if /^(\S+?):?\s/; next unless defined $interface; $IPs{$interface}->{STATE}=uc($1) if /\b(up|down)\b/i; $IPs{$interface}->{IP}=$1 if /inet\D+(\d+\.\d+\.\d+\.\d+)/i; } print Dumper(\%IPs); __END__ $VAR1 = { 'qfe0' => { 'IP' => '', 'STATE' => 'UP' }, 'qfe1' => { 'IP' => '', 'STATE' => 'UP' }, 'hme0' => { 'IP' => '', 'STATE' => 'DOWN' }, 'lo0' => { 'IP' => '', 'STATE' => 'UP' } };

Replies are listed 'Best First'.
Re: Answer: How do I get the local internet IP address?
by jasonk (Parson) on Aug 14, 2008 at 01:06 UTC

      The OP code did work for me though on a closed box and Net::Address::IP::Local only seems to work if there is, as the method name suggests, a public address open otherwise it's Unable to create UDP socket: No route to host.... The OP code found the right address on both my running OS X boxes, one closed, one open.

Re: Answer: How do I get the local internet IP address?
by betterworld (Curate) on Aug 14, 2008 at 14:31 UTC

    Update: I've ripped my code contribution out of this node and submitted it as a separate answer.

    I agree with the other posters that the best (as in portable and maintainable) solution would be to use a CPAN module.

    qx{ (LANG=C ; /sbin/ifconfig -a 2>&1) }

    I think it would be better to omit the ";" to make sure that the variable gets into the environment. Better yet would be to use "LC_ALL=C" because afaik that overrides all locales (including LANG).

Re: Answer: How do I get the local internet IP address?
by Perlbotics (Chancellor) on Aug 14, 2008 at 21:15 UTC

    Hi all, thanks for your feedback. I also thought that LC_ALL is the 'right' way to do it because it has precedence over LANG, but when I tested it yesterday, only LANG did work. That's because LANG is automatically exported, LC_ALL not (on the machines available to me). So leaving the semicolon away made LC_ALL=C work. I'll update that, after testing it with the Solaris boxes.
    Actually, the regular expressions are so fuzzy, they match any language that is available on my installation (cs, de, et_EE, fr, pt_BR - the other 129 languages have no localisation for ifconfig (net-tools here)). Right this approach is not perfect, but I thought the phrases 'Sample' and 'last resort' made clear, that this is not production code and one might need to fiddle a bit around with it? Would you suggest to make that more explicit?

    The reason why I presented this sample was because it works for me and it shows another approach (TIMTOWTDI) that amend to the already given answers. If it works it works without the need to establish a connection and it lists all IPs, not only the one that leaves the (usually "default") route as when contacting a root name-server. This is handy if you're behind several layers of firewalls and in situations where you are interested in the local IP configuration.
    If the intention of the question is more like How do I get the local internet IP address that currently connects my host to the IP a.b.c.d? (which is often not identical with the IP address other hosts would contact the local machine in return), then I would prefer this suggestion from tirwhan. Also because it doesn't require to install a non standard module beforehand.

    Any other advice is appreciated. Thanks again.

Log In?

What's my password?
Create A New User
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2018-06-24 21:42 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.