Przeglądaj źródła

modules: Add get_network_address() to calculate network address from given prefix.

Signed-off-by: Maximilian Wilhelm <max@rfc2324.org>
Maximilian Wilhelm 7 lat temu
rodzic
commit
437ba3adc2
1 zmienionych plików z 12 dodań i 0 usunięć
  1. 12 0
      _modules/ffho_net.py

+ 12 - 0
_modules/ffho_net.py

@@ -1195,3 +1195,15 @@ def is_subprefix (prefix, subprefix):
 	from ipcalc import Network
 
 	return subprefix in Network(prefix)
+
+# Return the network address of the given prefix
+def get_network_address (prefix, with_prefixlen = False):
+	from ipaddress import ip_network
+
+	net_h = ip_network (u'%s' % prefix, strict = False)
+	network = str (net_h.network_address)
+
+	if with_prefixlen:
+		network += "/%s" % net_h.prefixlen
+
+	return network