http://qs1969.pair.com?node_id=219002

c has asked for the wisdom of the Perl Monks concerning the following question:

I am in need of finding my local machine's IP address assigned to ETH0. I need to get the information independent of resolving a hostname. Instead, I need to pull information from something similar to ifconfig. I could certainly backtick the command into the code and parse out the information, but I would think there would be a cleaner, easier way. Aside from all of the nodes referencing how to find an ip address using Sys::Hostname, I've only found one that address my needs, and I don't think its the best way of doing things.

Is there a better manner, a module or built-in function that may do the trick?

-c

Replies are listed 'Best First'.
Re: finding ip address without hostname
by pg (Canon) on Dec 11, 2002 at 04:21 UTC
    Go CPAN, check out Net::Interface module.
Re: Finding an IP Address without the hostname
by cjf-II (Monk) on Dec 11, 2002 at 05:26 UTC

    Looking at the source of Sys::HostIP it appears to parse ifconfig output to obtain the IP address.

Re: finding ip address without hostname
by Abigail-II (Bishop) on Dec 11, 2002 at 10:44 UTC
    You seem to be thinking that calling an external program is "unclean". Why? It might actually be the most portable way of doing so, and it's certainly not hard.

    Abigail

Re: finding ip address without hostname
by Aristotle (Chancellor) on Dec 11, 2002 at 14:24 UTC
    A gotcha to keep in mind: a machine can have multiple IP addresses (even if it only has a single interface!). If you donīt go via the hostname, be sure to allow the user or admin to override your IP detection.

    Makeshifts last the longest.