title: IPv4Network in Python - Calculating next minimum subnet of different size?
body: <p>I'm working with the <code>ipaddress</code> module in Python and trying to figure out a way of calculating the next available subnet (of either the same prefix or a different prefix) that doesn't overlap the existing subnet (the new subnet <strong>MUST</strong> be greater than the old one).</p>
  <p>Lets say I start with network:</p>
  <pre class="lang-py prettyprint-override"><code>from ipaddress import IPv4Network
  
  # From 10.90.1.0 to 10.90.1.31
  main_net = IPv4Network(&quot;10.90.1.0/27&quot;)
  </code></pre>
  <p>I know the next available address is going to be <code>10.90.1.32</code>, I can even figure this out quite easily by doing:</p>
  <pre class="lang-py prettyprint-override"><code>next_ip = main_net.broadcast_address + 1
  # will output 10.90.1.32
  print(next_ip)
  </code></pre>
  <p>If I wanted to find the next <code>/27</code>, I just create a new network like so:</p>
  <pre class="lang-py prettyprint-override"><code># From 10.90.1.32 to 10.90.1.63
  new_net = IPv4Network(f&quot;{next_ip}/27&quot;)
  </code></pre>
  <p>This is all very straightforward so far, but now what if the next subnet I am looking for is a <code>/26</code> or a <code>/28</code> - how can I find the next minimum start IP address for either of these cases in Python?</p>
  <p>I have explored using the <code>supernet</code> method, for example I could do something like this:</p>
  <pre class="lang-py prettyprint-override"><code># Will print 10.90.1.0/26
  print(main_net.supernet(new_prefix=27))
  </code></pre>
  <p>The problem with this method is that it will print <code>10.90.1.0/26</code> which overlaps the existing <code>10.90.1.0/27</code> network, I could make a loop and get it to keep generated the next <code>/26</code> until they stop overlapping, but it seems inefficient to me.  Surely, there is a better way?</p>