Populates grains which describe whether a server has a default gateway
    configured or not. Uses `ip -4 route show` and `ip -6 route show` and greps
    for a `default` at the beginning of any line. Assuming the standard
    `default via <ip>` format for default gateways, it will also parse out the
    ip address of the default gateway, and put it in ip4_gw or ip6_gw.

    If the `ip` command is unavailable, no grains will be populated.

    Currently does not support multiple default gateways. The grains will be
    set to the first default gateway found.

    List of grains:

        ip4_gw: True  # ip/True/False if default ipv4 gateway
        ip6_gw: True  # ip/True/False if default ipv6 gateway
        ip_gw: True   # True if either of the above is True, False otherwise

def default_gateway():
    '''
    Populates grains which describe whether a server has a default gateway
    configured or not. Uses `ip -4 route show` and `ip -6 route show` and greps
    for a `default` at the beginning of any line. Assuming the standard
    `default via <ip>` format for default gateways, it will also parse out the
    ip address of the default gateway, and put it in ip4_gw or ip6_gw.

    If the `ip` command is unavailable, no grains will be populated.

    Currently does not support multiple default gateways. The grains will be
    set to the first default gateway found.

    List of grains:

        ip4_gw: True  # ip/True/False if default ipv4 gateway
        ip6_gw: True  # ip/True/False if default ipv6 gateway
        ip_gw: True   # True if either of the above is True, False otherwise
    '''
    grains = {}
    ip_bin = salt.utils.path.which('ip')
    if not ip_bin:
        return {}
    grains['ip_gw'] = False
    grains['ip4_gw'] = False
    grains['ip6_gw'] = False
    for ip_version in ('4', '6'):
        try:
            out = __salt__['cmd.run']([ip_bin, '-' + ip_version, 'route', 'show'])
            for line in out.splitlines():
                if line.startswith('default'):
                    grains['ip_gw'] = True
                    grains['ip{0}_gw'.format(ip_version)] = True
                    try:
                        via, gw_ip = line.split()[1:3]
                    except ValueError:
                        pass
                    else:
                        if via == 'via':
                            grains['ip{0}_gw'.format(ip_version)] = gw_ip
                    break
        except Exception:
            continue
    return grains