Browse Source

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

Signed-off-by: Maximilian Wilhelm <max@rfc2324.org>
Maximilian Wilhelm 6 years ago
parent
commit
437ba3adc2
1 changed files with 12 additions and 0 deletions
  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