Dans Lists wrote:

> The following snipit of code returns a MAC address this is true but when
> I try and get the MAC addresses for systems that have more than one NIC
> it only returns one of the MAC addresses.
> I can not figure out what all is in the hash returned in $ref as well. I
> would like to be able to link the IP address to the MAC address.
> 
> 
> 
> Can anybody tell me what is going wrong?
> 
> --
> Dan
> 
> 
> use Win32::NetAdmin;
> $ref = {};
> $host = $ARGV[0];
> Win32::NetAdmin::GetTransports( $host, $ref ) || die "Could not get the
> MAC Address!";
> 
> foreach $trans(keys %{$ref}){
>       $mac = $ref->{$trans}->{'transport_address'};
>       print "The hardware address is $mac.\n";
> }
> 


I can't run it on 9x, but you should be able to run this on NT
(need to modify it since it's designed to just get one MAC addr,
but gets them all before isolating it - change first sub):


use strict;
use Data::Dumper; $Data::Dumper::Indent=1;

my $d = 0;                      # debug
my $adapters = undef;           # store ipconfig data

# get MAC address on PPP interface

printf "ipconfig MAC method:\n";
my $addr = get_interface_address ('Description', qr{^PPP},
   'Physical Address');
print "ipconfig: MAC=$addr\n";
print "\n";

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub get_interface_address {             # return the requested interface field
        my $key = shift || 'Description';
        my $val = shift || qr{^PPP};
        my $ret = shift || 'IP Address';

$adapters = &get_active_interfaces() if not defined $adapters;
for (my $ii = 0; $ii < @{$adapters->{adapter}}; $ii++) {
        
        if (exists $adapters->{adapter}[$ii]{$key} and
          $adapters->{adapter}[$ii]{$key} =~ /$val/i) {
                return $adapters->{adapter}[$ii]{$ret};
        }
}
return 'Not found';

}

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# get_active_interfaces

sub get_active_interfaces {
        my %adapters;
        my $adapter;

my $ipconfig = 'c:\windows\ipconfig /all';
open IPC, "$ipconfig |" or die "Error piping $ipconfig: $!\n";
binmode IPC;
while (<IPC>) {

        next if /^\s*$/;
        s/[\r\n]//g;

        if (/^(\d+)\s+eth/i) {  # found adapter line

                $adapter = $1;                          # get number
                next;

        } else {

                next if not /^\s/;      # drop header line
                s/^\s+//;               # drop leading WS
                s/\.\s+//g;             # drop . . .

                my ($key, $val) = split /\s*:\s*/, $_;  # split on :
                if (not defined $adapter) {
                        $adapters{$key} = $val;
                        next;
                }
                $adapters{adapter}[$adapter]{$key} = $val;
        }
}
close IPC;
print Data::Dumper->Dump([\%adapters], [qw(%adapters)]) if $d;

return \%adapters;

}

__END__


-- 
   ,-/-  __      _  _         $Bill Luebkert   ICQ=14439852
  (_/   /  )    // //       DBE Collectibles   Mailto:[EMAIL PROTECTED]
   / ) /--<  o // //      http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/

_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to