#!/usr/bin/perl use strict; use warnings; use Socket; my $ifconfig='/usr/sbin/ifconfig'; # run ifconfig -a open my $if, '-|', "$ifconfig -a" or die "Couldn't run $ifconfig: $!\n"; while (<$if>) { # find a line with "broadcast" in it # on Solaris this contains all the information I need next unless /inet\s+(.*?)\s+netmask\s+(.*?)\s+broadcast\s+(.*?)\s*$/; # keep ip address as it is (a dotted string) my $iaddr= $1; # convert the netmask to a "long" my $netmsk= hex "0x$2"; # dito for the broadcast my $brdcst= unpack 'N', inet_aton $3; # How many bits does the netmask have? my $bits= 31-int(log(0xffffffff ^ $netmsk)/log(2)); # Convert the netmask to a "slashed" IP my $network= inet_ntoa(pack "N", ($brdcst & $netmsk)) . "/" . $bits; # show all info found print "$iaddr\t$network\n"; } close $if;