Hi. Sometimes it can be helpful to find out the own ip-addresses. This is a way to do it with the help of an undocumented feature of gethostbyname. HTH
#!/usr/bin/perl use Socket; use strict; sub ShowIPLocalInfo { my @hostent = gethostbyname(undef) or return 0; print "name: ",shift @hostent,"\n"; # the name print "alias: ",shift @hostent,"\n"; # not always available print "addrtype: ",shift @hostent,"\n"; print "length: ",shift @hostent,"\n"; print "\nOwn Ip-Addresses:\r\n"; print "\t",inet_ntoa($_), "\n" foreach (@hostent); } ShowIPLocalInfo();

Replies are listed 'Best First'.
•Re: Getting own ip-addresses!
by merlyn (Sage) on Mar 01, 2004 at 00:35 UTC
      perldoc -q find.+?my.+?ip Found in /perl58/lib/pod/perlfaq9.pod How do I find out my hostname/domainname/IP address? The normal way to find your own hostname is to call the `hostname` program. While sometimes expedient, this has some problems, such a +s not knowing whether you've got the canonical name or not. It's one of +those tradeoffs of convenience versus portability. The Sys::Hostname module (part of the standard perl distribution) +will give you the hostname after which you can find out the IP address (assuming you have working DNS) with a gethostbyname() call. use Socket; use Sys::Hostname; my $host = hostname(); my $addr = inet_ntoa(scalar gethostbyname($host || 'localhost' +)); Probably the simplest way to learn your DNS domain name is to grok + it out of /etc/resolv.conf, at least under Unix. Of course, this assu +mes several things about your resolv.conf configuration, including tha +t it exists. (We still need a good DNS domain name-learning method for non-Unix systems.) $$ perl use Socket; use Sys::Hostname; my $host = hostname(); my $addr = inet_ntoa(scalar gethostbyname($host || 'localhost')); die "$host ][ $addr $/"; __END__ medina ][ 169.190.150.133
      well, it just seems to work on a Win32 machine;
      it's probably because of the way gethostbyname is implemented in the socket library;
      well, i think it is still a good and simple way for win32 users
Re: Getting own ip-addresses!
by Anonymous Monk on Mar 01, 2004 at 05:18 UTC
Re: Getting own ip-addresses on Win32 machines
by meetraz (Hermit) on Mar 01, 2004 at 22:33 UTC
    Of course, your Win32 box may have multiple network cards and IP addresses, so most/all of these answers are only partial solutions.

    I would like to recommend the Win32::IPHelper module:

    use strict; use Win32::IPHelper; my @Adapters; Win32::IPHelper::GetAdaptersInfo(\@Adapters); foreach my $Adapter (@Adapters) { foreach my $IP (@{$Adapter->{IpAddressList}}) { print "IP = $IP->{IpAddress}\n"; } }