the most reliable and correct method to get network interface address in Linux using Perl
This method doesn't rely on parsing 'ifconfig' tool output, and should work in any Linux distribution, but also could work in other POSIX environments.
The function returns a string with IPv4 address or 'undef' if it's not possible.
my ($iface) = @_;
my $buf = pack('a256', $iface)
SIOCGIFADDR(), $buf) && (my @address = unpack('x20 C4', $buf)))
Report this snippet