Subject: | if_list returning garbage on macOS Sierra |
I'm trying to find a cross-platform mechanism to get a list of local interfaces. I was using IO::Interface::Simple but this doesn't work on Windows so I'm trying to use the "old" API. I'm testing with the sample code from the docs:
use IO::Socket;
use IO::Interface qw(:flags);
my $s = IO::Socket::INET->new(Proto => 'udp');
my @interfaces = $s->if_list;
for my $if (@interfaces) {
print "interface = $if\n";
my $flags = $s->if_flags($if);
print "addr = ",$s->if_addr($if),"\n",
"broadcast = ",$s->if_broadcast($if),"\n",
"netmask = ",$s->if_netmask($if),"\n",
"dstaddr = ",$s->if_dstaddr($if),"\n",
"hwaddr = ",$s->if_hwaddr($if),"\n";
print "is running\n" if $flags & IFF_RUNNING;
print "is broadcast\n" if $flags & IFF_BROADCAST;
print "is p-to-p\n" if $flags & IFF_POINTOPOINT;
print "is loopback\n" if $flags & IFF_LOOPBACK;
print "is promiscuous\n" if $flags & IFF_PROMISC;
print "is multicast\n" if $flags & IFF_MULTICA
print "is notrailers\n" if $flags & IFF_NOTRAILERS;
print "is noarp\n" if $flags & IFF_NOARP;
}
This works fine on Linux (CentOS 7.3.1611, perl 5.16.3) but on macOS Sierra with perl 5.26.1 I get garbage in @interfaces.
Dumping @interfaces with Data::Dumper gives this:
$VAR1 = '';
$VAR2 = '';
$VAR3 = '';
$VAR4 = '';
$VAR5 = '
';
$VAR6 = '';
$VAR7 = 'en3';
$VAR8 = 'ge0�';
$VAR9 = 'ipsec0';
$VAR10 = 'lo0';
$VAR11 = 'stf0';
$VAR12 = 'utun0';
$VAR13 = '��n&�';
$VAR14 = '��';
The actual list of interfaces on the Mac is as follows:
ifconfig -a | awk '/^[a-z0-9]+:/ {print $1}'
lo0:
gif0:
stf0:
en0:
en8:
en4:
en7:
en3:
bridge0:
p2p0:
awdl0:
utun0:
en5:
ipsec0: