title: How to get first/last IP address of CIDR using ipaddr module
body: <p>The brute force approach:</p>
  
  <pre><code>from ipaddr import IPv4Network
  n = IPv4Network('10.10.128.0/17')
  all = list(n.iterhosts()) # will give me all hosts in network
  first,last = all[0],all[-1] # first and last IP
  </code></pre>
  
  <p>I was wondering how I would get the first and last IP address from a CIDR without having to iterate over a potentially very large list to get the first and last element?</p>
  
  <p>I want this so I can then generate a random ip address in this range using something like this:</p>
  
  <pre><code>socket.inet_ntoa(struct.pack('&gt;I', random.randint(int(first),int(last))))
  </code></pre>