Sys::HostAddr - Get IP address information about this host
use Sys::HostAddr;
my $sysaddr = Sys::HostAddr->new();
my $string = $sysaddr->public();
my $aref = $sysaddr->interfaces();
my $aref = $sysaddr->addresses();
my $href = $sysaddr->ip();
my $ip = $sysaddr->first_ip();
my $main = $sysaddr->main_ip();
Sys::HostAddr provides methods for determining IP address information about a local host.
Sys::HostAddr
my $sysaddr = Sys::HostAddr->new( debug => [0|1], ipv => [4|6], interface => 'ethX', );
debug will control ancillary/informational messages being printed.
debug
ipv will limit response data to either IPv4 or IPv6 addresses. Default: IPv4
ipv
interface limits response data to a particular interface, where applicable. This value is overriden if a method is given an interface argument directly.
interface
public will attempt to find the public ip address of your machine. usefull if you're behind some NAT. Sends an automation request to the www.dnsbyweb.com service. Works on IPv4 only.
public
main_ip will attempt to find the "main" or "primary" IP address of the machine. method can be: auto (default), preferred (MSWin32/cygwin only), route, or dns.
main_ip
first_ip will return the first ip address on a given interface (if provided), or the first ip address returned by ifconfig (that is not localhost).
first_ip
ip will return a hash reference containing ipaddress/netmask information keyed by interface. if $interface is provded, will be limited to that interface, otherwise will include all interfaces
ip
addresses will return an array reference of all ip addresses found. if $interface is provided, will be limited to that interface.
addresses
interfaces will return an array reference of all interfaces found.
interfaces
use Sys::HostAddr; my $sysaddr = Sys::HostAddr->new(); my $int_aref = $sysaddr->interfaces(); foreach my $interface ( @{$int_aref} ){ print "found $interface\n"; } my $addr_aref = $sysaddr->addresses(); foreach my $address ( @{$addr_aref} ){ print "found $address\n"; } my $href = $sysaddr->ip(); foreach my $interface ( keys %{$href} ){ print "$interface has: "; foreach my $aref ( @{$href->{$interface}} ){ print " $aref->{addr} $aref->{netmask}\n"; } } my $ip = $sysaddr->first_ip(); print "$ip found as the first ip address\n"; my $main = $sysaddr->main_ip(); print "$main appears to be the main ip address of this machine\n"; my $pub = $sysaddr->public(); print "public addr appears to be $pub\n";
Jeremy Kister
1 POD Error
The following errors were encountered while parsing the POD:
You forgot a '=back' before '=head1'
To install Sys::HostAddr, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Sys::HostAddr
CPAN shell
perl -MCPAN -e shell install Sys::HostAddr
For more information on module installation, please visit the detailed CPAN module installation guide.