示例#1
0
文件: main.py 项目: ngsru/heaver
def must_parse_net_arg(arg):
    "Helper for parsing net args"
    if ":" not in arg:
        report.die("No bridge interface supplied")
    br_conf, _colon, addrs_str = arg.partition(":")
    if "/" in br_conf:
        br_dev, _slash, hwaddr = br_conf.partition("/")
        hwaddr = hwaddr.replace(".", "").lower()
        if len(hwaddr) > 12:
            report.die("Hardware address is too long (must be 12 hex digits)")
        elif len(hwaddr) < 12:
            report.die("Hardware address is too short (must be 12 hex digits)")
        else:
            for idx, char in enumerate(hwaddr):
                if not ("0" <= char <= "9" or "a" <= char <= "f"):
                    report.die(("Invalid character in hardware address "
                        "at position %d (only hex digits allowed)") % idx)

    else:
        br_dev = br_conf
        hwaddr = None

    if not os.path.exists(os.path.join("/sys/class/net/", br_dev)):
        report.die("No such device: %s" % br_dev)

    addrs = addrs_str.split(",")
    if not addrs:
        report.die("At least one address should be used (bridge %s)" % br_dev)

    ips = []
    for addr in addrs:
        if "/" not in addr:
            report.die("No netmask supplied for address %s" % addr)

        ip, mask = addr.split("/")
        try:
            addrpool.parse_address(ip)
        except:
            report.die("Invalid ip address: %s" % ip)

        try:
            int(mask)
        except:
            report.die("Invalid mask: %s" % mask)

        if not (32 >= int(mask) > 0):
            report.die("Mask is out of range: %s" % mask)

        ips.append((ip, mask))

    return br_dev, hwaddr, ips
示例#2
0
文件: main.py 项目: ngsru/heaver
def must_gen_net(net, count):
    "Generate addresses and hwaddr for network"

    raw_addresses = net["pool"].alloc_addresses(count)
    if raw_addresses is None:
            report.die("Not enough free addresses")

    # FIXME: ip address datatype - string or tuple or smth else?
    raw_mac = (02, random.randint(1, 255)) + addrpool.parse_address(raw_addresses[0])

    mac = ":".join("%x" % digit for digit in raw_mac)
    addresses = [(addr, "24") for addr in raw_addresses] # FIXME: hardcoded netmask!
    return mac, addresses