import re
import ipaddress


def readNetworks(filename):
  """
  Reads a list of networks from a file. The file should contain an IP address
  or an IP network with a bitmask, followed by a description. The description
  is to be separated from the IP network by at least one whitespace character.

  The function will ignore any lines that do not seem to contain an IP address
  or an IP network.
  """

  networks = list()

  with open(filename) as f:
    for line in f:
      matches = re.match(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:/\d{1,3})?)(?:\s+)(.*$)", line)
      if matches:
        address     = matches.group(1)
        description = matches.group(2)

        networks.append( (ipaddress.ip_network(address), description) )

  return networks