import ipaddress


def build_subnet(subnet_str):
    """
    Convert an IPv4/IPv6 subnet in string format (e.g. 192.168.1.0/24) into an
    :class:`ipaddress.IPv4Network` or :class:`ipaddress.IPv6Network` object.

    :param str subnet_str: subnet in string format.
    :returns: :class:`ipaddress.IPv4Network` or :class:`ipaddress.IPv6Network`
        object depends on input subnet address type, or None if input invalid.
    """

    try:
        network = ipaddress.IPv4Network(subnet_str)
    except ipaddress.AddressValueError:
        try:
            network = ipaddress.IPv6Network(subnet_str)
        except ipaddress.AddressValueError:
            return None

    return network