Network Interface MAC Address

Below is my somewhat brute force method of getting an interface MAC address, but in the interests of learning something, I’m sure there’s a more efficient way. Any clues?

set ipData to do shell script "ifconfig en0" as string
set macLoc to (offset of "ether" in ipData) + 6
set macAdd to ""
repeat with k from macLoc to (macLoc + 16)
	set macAdd to macAdd & character k of ipData
end repeat
macAdd

This would also work:

do shell script "ifconfig en0 | grep ether | cut -c 8-24"
set MACaddress to result

Thanks. I was ok up to grep ether, but forgot cut -c as the way to get the characters.

FWIW, you could also use cut like this:

cut -d " " -f 2

That would split the line at every space and return the second item (similar to AS’s text item delimiters). [If you use that inside AppleScript, you’ll have to escape the quotation marks.]

Works as advertised although I don’t see the advantage. The interesting part is “similar to AS’s text item delimiters” because the ability to combine the power of a simple grep (for those of us with limited RegEx capability - takes me forever to figure out the RegEx for a sed) with ASTID’s could be rather handy.

A caveat to this is that different interfaces have different MAC addresses. “en0” is usually the primary ethernet interface but you could have multiple ethernet interfaces, AirPort, etc.:

set ethernet_MAC_add to do shell script "ifconfig en0 | grep ether | cut -c 8-24"
set airport_MAC_add to do shell script "ifconfig en1 | grep ether | cut -c 8-24"
return {ethernet_MAC_add:ethernet_MAC_add, airport_MAC_add:airport_MAC_add}

Jon

You can also use IP over FireWire, which would probably be device fw0. When I check that on my machine I get this:

Of course, this won’t work with my script in its current state. If you’re looking for MAC addresses, be sure to put in the correct device name.

You should be able to do something like this:

on getMAC(device)
	if device starts with "en" then
		get "ether"
	else if device starts with "fw" then
		get "lladdr"
	end if
	
	try
		do shell script "ifconfig " & device & " | grep " & result & " | cut -d \" \" -f 2"
		return result
	on error
		return missing value
	end try
end getMAC

getMAC("en0")