http://www.perlmonks.org?node_id=53675


in reply to How do I get the local internet IP address?

Non portable, return the ip address of all hosts found in /etc/hosts...

while (@adrs=(gethostent())[4]) { for my $value (@adrs) { print join '.',unpack('C4',$value); print "\n"; } }


Replies are listed 'Best First'.
Re: Answer: How do I get the local internet IP address?
by arhuman (Vicar) on Jan 23, 2001 at 22:44 UTC
    Or the more perlish (read : normal, not perverted brain have difficulties to understand it)
    *grin*
    while (@adrs=(gethostent())[4]) { map { $\="\n";print join '.',unpack'C4',$_} @adrs }
    BTW I'm sure a Perl Guru, could tell me how to make this even smaller !
    Please show me the light !

    UPDATE
    Ooops ! I suddenly realized that even if MY /etc/hosts contains only entries about my local machine, it might not always be the case (in fact it should often contains entries about other machines...).
    So thousand apologizes, but all my posts in this thread do not resolve the problem...
    But my code could be use to get the ip adresses of the machines in /etc/hosts...