使用命令inet:getifaddrs().
详情见:http://erlang.org/doc/man/inet.html ,官方定义如下:
getifaddrs() -> {ok, Iflist} | {error, posix()}
Types
Iflist = [{Ifname, [Ifopt]}]
Ifname = string()
Ifopt =
{flags, [Flag]} |
{addr, Addr} |
{netmask, Netmask} |
{broadaddr, Broadaddr} |
{dstaddr, Dstaddr} |
{hwaddr, Hwaddr}
Flag =
up | broadcast | loopback | pointtopoint | running | multicast
Addr = Netmask = Broadaddr = Dstaddr = ip_address()
Hwaddr = [byte()]
Returns a list of 2-tuples containing interface names and the interface addresses. Ifname is a Unicode string. Hwaddr is hardware dependent, for example, on Ethernet interfaces it is the 6-byte Ethernet address (MAC address (EUI-48 address)).
咱自己实践了一下,如下所示,hwaddr就是MAC地址:
[root@localhost ~]# erl Erlang/OTP 19 [erts-8.3] [source] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false] Eshell V8.3 (abort with ^G) 1> inet:getifaddrs(). {ok,[{"lo", [{flags,[up,loopback,running]}, {hwaddr,[0,0,0,0,0,0]}, {addr,{127,0,0,1}}, {netmask,{255,0,0,0}}, {addr,{0,0,0,0,0,0,0,1}}, {netmask,{65535,65535,65535,65535,65535,65535,65535, 65535}}]}, {"ens33", [{flags,[up,broadcast,running,multicast]}, {hwaddr,[0,12,41,185,123,49]}, {addr,{192,168,83,128}}, {netmask,{255,255,255,0}}, {broadaddr,{192,168,83,255}}, {addr,{65152,0,0,0,8566,4963,38750,27671}}, {netmask,{65535,65535,65535,65535,0,0,0,0}}]}, {"virbr0", [{flags,[up,broadcast,running,multicast]}, {hwaddr,[82,84,0,236,32,118]}, {addr,{192,168,122,1}}, {netmask,{255,255,255,0}}, {broadaddr,{192,168,122,255}}]}, {"virbr0-nic", [{flags,[broadcast,multicast]}, {hwaddr,[82,84,0,236,32,118]}]}]}