WMI can do this nicely.  See attached script.

 - Pat

use warnings;
use strict;
use Getopt::Long;
use Win32::OLE;

sub die_usage () {
    die "Usage: $0 [ --all ] <hostname>\n";
}

my %opts;
GetOptions (\%opts, "all")
    or die_usage ();

scalar @ARGV == 1
    or die_usage ();

my ($hostname) = @ARGV;

# Bomb out completely if COM engine encounters any trouble.
Win32::OLE->Option ('Warn' => 3);

# Get a handle to the SWbemServices object of the local machine.
my $computer = Win32::OLE->GetObject ("WinMgmts://$hostname/");

# Get the SWbemObjectSet of all network adapters.  See:
# 
<http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_networkadapterconfiguration.asp>
my $adapters_set =
    $computer->InstancesOf ('Win32_NetworkAdapterConfiguration');

# Convert set to a Perl array.
my @adapters = Win32::OLE::Enum->All ($adapters_set);

# Loop through them, printing various items of interest.
foreach my $adapter (@adapters) {
    # Skip adapters for which TCP/IP is not bound and enabled, unless
    # --all switch was given.
    $opts{'all'} || ($adapter->{'IPEnabled'})
        or next;

    print "Index: $adapter->{'Index'}\n";
    print "Caption: $adapter->{'Caption'}\n";
    print "Service Name: $adapter->{'ServiceName'}\n";
    my $mac_addr = $adapter->{'MACAddress'};
    defined $mac_addr
        and print "MAC Address: $mac_addr\n";
    my $ipaddrs = $adapter->{'IPAddress'};
    if (defined $ipaddrs) {
        foreach my $ip (@$ipaddrs) {
            print "IP Address: $ip\n";
        }
    }

    print "\n";
}

Reply via email to