"""
Network
=======
"""

from fabric.api import hide, run, settings, sudo
from fabtools.files import is_file


def interfaces():
    """
    Get the list of network interfaces. Will return all datalinks on SmartOS.
    """
    with settings(hide('running', 'stdout')):
        if is_file('/usr/sbin/dladm'):
            res = run('/usr/sbin/dladm show-link')
        else:
            res = sudo('/sbin/ifconfig -s')
    return map(lambda line: line.split(' ')[0], res.splitlines()[1:])


def address(interface):
    """
    Get the IPv4 address assigned to an interface.

    Example::

        import fabtools

        # Print all configured IP addresses
        for interface in fabtools.network.interfaces():
            print(fabtools.network.address(interface))

    """
    with settings(hide('running', 'stdout')):
        res = sudo("/sbin/ifconfig %(interface)s | grep 'inet '" % locals())
    if 'addr' in res:
        return res.split()[1].split(':')[1]
    else:
        return res.split()[1]


def nameservers():
    """
    Get the list of nameserver addresses.

    Example::

        import fabtools

        # Check that all name servers are reachable
        for ip in fabtools.network.nameservers():
            run('ping -c1 %s' % ip)

    """
    with settings(hide('running', 'stdout')):
        res = run(r"cat /etc/resolv.conf | grep 'nameserver' | cut -d\  -f2")
    return res.splitlines()

@@ -1000,16 +1000,475 @@
 t()%5B1%5D%0A%0A
+def mac(interface):%0A    %22%22%22%0A    Get the MAC address assigned to an interface.%0A%0A    Example::%0A%0A        import fabtools%0A%0A        # Print all configured MAC addresses%0A        for interface in fabtools.network.interfaces():%0A            print(fabtools.network.mac(interface))%0A%0A    %22%22%22%0A    with settings(hide('running', 'stdout')):%0A        res = sudo(%22/sbin/ifconfig %25(interface)s %7C grep -o -E '(%5B%5B:xdigit:%5D%5D%7B1,2%7D:)%7B5%7D%5B%5B:xdigit:%5D%5D%7B1,2%7D'%22 %25 locals())%0A    return res%0A
 %0Adef nam

Add MAC address information