Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Getting net addy on localhost?

by kwoff (Friar)
on Nov 27, 2001 at 09:49 UTC ( #127733=note: print w/replies, xml ) Need Help??

in reply to Getting net addy on localhost?

You can use Net::hostent gethost() to get $h->name, which would be the outside name (not localhost), then use gethost() again on that outside name and the address is in inet_ntoa($h->addr). Look at `perldoc Net::hostent`.

Replies are listed 'Best First'.
Re: Re: Getting net addy on localhost?
by rob_au (Abbot) on Nov 27, 2001 at 09:57 UTC
    Nifty idea, but the problem with this approach is that you still need to know the world-resolvable name of the machine. You cannot call Net::hostent::gethost() without a host name to resolve. eg.

    perl -MNet::hostent -e '$host = gethost(); print $host->name, "\n"' Not enough arguments for Net::hostent::gethost at -e line 1, near "()" Execution of -e aborted due to compilation errors.

    Ooohhh, Rob no beer function well without!
      And let's not get into the fact that you might have multiple IP addresses.

      You never said what platform you are on either. If UN*Xy the easiest pure-perl is to parse /etc/sysconfig/network-scripts/ifcfg-*. You might steal the code from

      perl -p -e "s/(?:\w);([st])/'\$1/mg"

        The method that I pointed out in the addendum to my first post in this thread *should* suffice if you are wanting to resolve the default external IP address of the machine - But I do agree, there is a great amount of 'grey area' on this question given the number of available configurations and methods of interface.

        I don't however agree with the concept of parsing /etc/sysconfig/network-scripts/ifcfg-* - For starters, this is very much a Red Hat (and variant) locale for network configurations, *not* a general *NIX location pragma. Further to this, the idea of relying on text files generated outside of the real scope of the program makes me very nervous - I have rambled on this concept previously and as such won't repeat myself here.

        Suffice to say, if you are going to rely on *any* external source, consider it tainted and evil (-T, -T and -T) until proven otherwise - And even then treat it with care. If I had to rely on an external program source, then I would be looking directly at /sbin/ifconfig (the path of which should be hard-coded into the program, not drawn from an alternate configuration file) and parsing the output of that.

        Just my random thoughts on the matter


        Ooohhh, Rob no beer function well without!

        That is incorrect, '/etc/sysconfig/network-scripts/ifcfg-*' is a RedHat-ism and isn't even portable between all linux distros. As far as I know there is no standard file between unix and unix-like systems for this information. You can try /etc/hosts but that will only give you resolvable interfaces. Parsing the output of '/sbin/ifconfig' is more likely to be portable. (not that portability is the goal here, but we don't know what system your running on) SNMP (if you have it running) would be another possible means of getting interfaces addresses.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://127733]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2022-12-01 07:00 GMT
Find Nodes?
    Voting Booth?