@netmanchris asked for a method to determine the smallest common subnet for two hosts. Below is my solution based on the python netaddr library
from netaddr import *
for net in superNets:
if highIP in net:
On line 2 and 3 the ip are used to create to an IPNetwork. On line 4 a list is created containing al supernets of the IPNetwork. As the list is from large to small it needs te be reserved. By looping over eacht of the supernets and checking of the second ip is part of the subnet the common subnet is determined.
The Python netaddr is very versatile and can help you with various tedious ip operations