lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


steve donovan <steve.j.donovan@gmail.com> [2010-03-18 14:46:40]:

> f = io.popen('ipconfig /all')
> line = f:read()
> while line do
>   if line:find 'Ethernet adapter Local Area Connection' then
>     line = f:read()
>     while not line:find 'Physical Address' do line = f:read() end
>     print(line:match(': (.+)'))
>     return
>   end
>   line = f:read()
> end

Don't know if it's portable, but should be faster:

function mac(iface)
        if not iface then
		return nil, 'no interface'
	end

        local f = io.open('/proc/net/arp', 'r')
        if not f then
		return nil, 'open error'
	end

	local match = string.format('%%x*:.*%s', iface)
        local mac = string.match(tostring(f:read('*a')), match)
        if mac then return mac:sub(1, 17), nil end
end

print(mac("eth0"))