def subnetentry(x): """ Generate a subnet declaration block given an IPv4 prefix string for inclusion in the config file. """ if x.find(":") >= 0: # this is an IPv6 address return "" else: net = Ipv4Prefix(x) return 'echo " network %s"' % net
def addrstr(x): if x.find(":") >= 0: net = Ipv6Prefix(x) else: net = Ipv4Prefix(x) if net.max_addr() == net.min_addr(): return "" else: if os.uname()[0] == "Linux": rtcmd = "ip route add default via" else: raise Exception("unknown platform") return "%s %s" % (rtcmd, net.min_addr())
def routestr(x): if x.find(":") >= 0: net = Ipv6Prefix(x) dst = "3ffe:4::/64" else: net = Ipv4Prefix(x) dst = "10.9.8.0/24" if net.max_addr() == net.min_addr(): return "" else: if os.uname()[0] == "Linux": rtcmd = "#/sbin/ip route add %s via" % dst else: raise Exception("unknown platform") return "%s %s" % (rtcmd, net.min_addr())
def firstipv4prefix(node, prefixlen=24): """ Similar to QuaggaService.routerid(). Helper to return the first IPv4 prefix of a node, using the supplied prefix length. This ignores the interface's prefix length, so e.g. '/32' can turn into '/24'. """ for ifc in node.netifs(): if hasattr(ifc, 'control') and ifc.control == True: continue for a in ifc.addrlist: if a.find(".") >= 0: addr = a.split('/')[0] pre = Ipv4Prefix("%s/%s" % (addr, prefixlen)) return str(pre) # raise ValueError, "no IPv4 address found" return "0.0.0.0/%s" % prefixlen
def __init__(self, ip4_prefix=None, ip6_prefix=None): """ Creates an IpPrefixes object. :param str ip4_prefix: ip4 prefix to use for generation :param str ip6_prefix: ip6 prefix to use for generation :raises ValueError: when both ip4 and ip6 prefixes have not been provided """ if not ip4_prefix and not ip6_prefix: raise ValueError("ip4 or ip6 must be provided") self.ip4 = None if ip4_prefix: self.ip4 = Ipv4Prefix(ip4_prefix) self.ip6 = None if ip6_prefix: self.ip6 = Ipv6Prefix(ip6_prefix)
def addrstr(x): if x.find(":") >= 0: net = Ipv6Prefix(x) fam = "inet6 ::" else: net = Ipv4Prefix(x) fam = "inet 0.0.0.0" if net.max_addr() == net.min_addr(): return "" else: if os.uname()[0] == "Linux": rtcmd = "ip route add default via" elif os.uname()[0] == "FreeBSD": rtcmd = "route add -%s" % fam else: raise Exception, "unknown platform" return "%s %s" % (rtcmd, net.min_addr())
def subnetentry(x): """ Generate a subnet declaration block given an IPv4 prefix string for inclusion in the dhcpd3 config file. """ if x.find(":") >= 0: return "" else: addr = x.split("/")[0] net = Ipv4Prefix(x) # divide the address space in half rangelow = net.addr(net.num_addr() / 2) rangehigh = net.max_addr() return """ subnet %s netmask %s { pool { range %s %s; default-lease-time 600; option routers %s; } } """ % (net.prefix_str(), net.netmask_str(), rangelow, rangehigh, addr)