Make sure that you check for definedness.#!/usr/bin/perl use strict; use warnings; use Socket; my $packed_ip = gethostbyname('localhost.localdomain'); if (defined $packed_ip) { my $ip_address = inet_ntoa($packed_ip); print $ip_address, "\n"; }
Using Sys::Hostname::FQDN, you can do an ascii-version of Anonymous Monk's post like this:
#!/usr/bin/perl BEGIN { $| = 1; $^W = 1; } use strict; use warnings; use Socket; use Sys::Hostname::FQDN qw( asciihostinfo gethostinfo fqdn short ); my($name,$aliases,$addrtype,$length,@addrs) = asciihostinfo(); print qq|host info short name : |, short(), qq| long name ; |, fqdn(), qq| aliases : |, $aliases, qq| address type : |, $addrtype, qq| address length: |, $length, qq| IP address(es): |; foreach(@addrs) { print "\t$_\n"; }
In reply to Re: gethostbyname returns reverse order of ipaddress expected
by Khen1950fx
in thread gethostbyname returns reverse order of ipaddress expected
by anubhavd
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |