title: Comparing a set of IP addresses against subnets using python and netaddr module
body: <p>I am trying to get a list of IP addresses if they match a subnet via Python 2.7 and netaddr module.</p>
  
  <p>I am currently running this on my Windows PC with Python 2.7 installed from the www.python.org site.</p>
  
  <p>I intend to run it on RHEL 6.x with python 2.7 installed.</p>
  
  <p>This should take the IP address from <code>ip_list</code> and compare it to the <code>banned_subnets</code>
  if it finds a match then it should add these to <code>new_list</code> and print out <code>Match found</code>.</p>
  
  <p>There should be two matches</p>
  
  <pre><code>10.11.117.137 is in subnet 10.11.117.0/24
  10.24.33.21 is in subnet 10.24.33.0/24
  </code></pre>
  
  <p>I can't fathom out checking multiple values in a list against a list with multiple values.</p>
  
  <p>I have read the docs on <code>netaddr</code>, I have read the <a href="https://stackoverflow.com/questions/16138015/python-comparing-two-lists">Python - Comparing two lists</a> on this site and several others but I am just not getting it.</p>
  
  <p>Here is the bit of code I am trying to get to work</p>
  
  <pre><code>import netaddr
  
  # Range of IP Addresses that we see connections from.
  ip_list = ['10.11.117.137', '10.11.122.20', '10.24.33.21', '10.11.122.22']
  
  # List of IP Subnets that members of IP list should not be from.
  banned_subnets = ['10.11.117.0/24', '10.24.33.0/24']
  
  # Using banned_subnets as our master list see if any of the ip_list addresses are present
  # If they are present print the ip address
  
  new_list = []
  
  for Z in banned_subnets:
      for X in ip_list:
          if netaddr.IPAddress(X) == netaddr.IPNetwork(Z):
              new_list.append(X)
              print 'Match Found'
          else:
              print 'No Matches Found'
  
  print new_list
  </code></pre>
  
  <p>When I run this i get the following returned</p>
  
  <pre><code>E:\Python27\python.exe E:/Users/twelsh/PycharmProjects/lool/liist-loop.py
  Match Found
  Match Found
  Match Found
  Match Found
  Match Found
  Match Found
  Match Found
  Match Found
  ['10.11.117.137', '10.11.122.20', '10.24.33.21', '10.11.122.22', '10.11.117.137', '10.11.122.20', '10.24.33.21', '10.11.122.22']
  
  Process finished with exit code 0
  </code></pre>
  
  <p>I'm new to all this Python malarky. All advice would be appreciated.</p>