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
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}
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")