Need support creating a perl script selecting MAC-Adresses

Dirk Kotte
Dirk Kotte used Ask the Experts™
i need ideas (or a script) how to search all MAC Addresses of a windows system and pass these as parameter to an external program...with PERL.

script should do the following:
1. get all MAC-Adresses (LAN1, LAN2, WLAN,...)

2. start external programm and pass every MAC (one after another) ... possible within a loop
extProg.exe AA-AA-AA-AA-AA-AA
extProg.exe BB-BB-BB-BB-BB-BB
extProg.exe CC-CC-CC-CC-CC-CC-CC
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fractional CTO
Distinguished Expert 2018
1) Parse the output of ifconfig...

imac> ifconfig | grep ether
	ether 38:c9:86:01:0c:09 
	media: autoselect (1000baseT <full-duplex,flow-control,energy-efficient-ethernet>)
	ether ac:29:3a:9f:98:99 
	ether 0a:00:00:b7:48:00 
	ether 0a:00:00:b7:48:01 
	ether 0e:29:3a:9f:98:99 
	ether 9a:a5:a5:7b:10:e5 
	ether 3a:c9:86:10:c9:00

Open in new window

2) To use these, your PERL snippet will be something like...

my @macs = `ifconfig | grep ether`;
chomp @macs;

foreach my $mac (@macs) {
    system("extProg.exe $mac");

Open in new window

3) Code above will work on Linux, OSX, any other UNIX-esque derivative OS.

Since various versions of Windoze implement ifconfig differently, refer to your specific version for how to get your mac addrs.

I'd likely use the getmac, if all I required was mac addrs... so Windoze code will be something like...

my @macs = `getmac`;
chomp @macs;

# first time through flag
my $ftt = 1;

foreach my $entry (@macs) {

    my ($mac,$tail) = split /\s+/, $entry;

    # Skip header cruft, till mac addrs start
    if ($ftt) {
       $ftt = 0 if ($mac =~ /^===/o);

    system("extProg.exe $mac");


Open in new window

Perl is my preferred language but it's not always the best choice.  In this case, I'd use a cleaner/shorter powershell script.

$interfaces = Get-WmiObject -Class 'Win32_NetworkAdapterConfiguration'
Foreach ($interface in $interfaces) { Write-Output $interface.MACaddress }

Open in new window

PS C:\test> .\get_mac.ps1

That example uses Write-Output to print the mac addresses, but you can just as easily execute your external command.  Just like in Perl, powershell has several ways to execute external connamds.

Another advantage in using the powershell script is that it can find more mac addresses than the getmac command.  On my system the getmac command found 2 mac addresses and the powershell script found 3.

Here's what I get from David's script.

Use of uninitialized value $mac in pattern match (m//) at C:\test\ line 19.
Try this from yesterday to now ... works great and independent from windows version.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial