コード例 #1
0
def init_host(settings):
    """
    Reinitialize or initialize host instance from environment and stored settings.
    """
    host = dict( name = get_hostname() )
    hostnameId = host['name'].lower()
    if hostnameId not in settings.nodes:
        ifaces = {}
        for iface, mac, spec in domain.inet_ifaces():
            if mac in settings.interfaces:
                raise Exception("Found existing interface", mac)
            else:
                settings.interfaces[mac] = ifaces[mac] = dict(
                    node = hostnameId
                )
        if settings.interactive:
            name = Prompt.raw_input("Give a name for this node", host['name'])
        host.update(dict(
            unid = str(uuid.uuid4()),
            interfaces = ifaces.keys()
        ))
        settings.nodes[hostnameId] = host
        open(hostIdFile, 'w+').write(" ".join((host['unid'], host['name'])))
        log.std("{bwhite}Wrote new host, {green}%s {default}<{bblack}%s{default}>",
            host['name'], host['unid'])
        settings.commit()
    else:
        host = get_current_host(settings)
        log.std("{bwhite}Found host, {green}%s {default}<{bblack}%s{default}>",
            host['name'], host['unid'])
    return host
コード例 #2
0
def info():
    """
    determine gateway, and identify node by hardware address
    record new gateways
    """

    # print some stuff
    hostname = get_hostname()
    print "On node:", hostname
    gateway = get_gateway()
    print 'Internet gateway: '
    default_routes = get_default_route()
    for gateway in default_routes:
        print '\t-', gateway,
        m = get_mac(gateway)
        if not m:
            print '(invalid)'
            continue
        else:
            try:
                gateway_node = socket.gethostbyaddr(gateway)[0]
            except socket.herror, e:
                print '(invalid: %s)' % e
                continue
            print gateway_node, "[ether %s]" % m
コード例 #3
0
ファイル: domain1.py プロジェクト: dotmpe/script-mpe
def get_current_node():
    global settings

    host = get_hostname()
    assert host
    addr = socket.gethostbyname(host)
    assert addr, host
    ifinfo = parse_ifconfig()
    mac = None
    for iface, attr in ifinfo:
        if 'inet' in attr:
            if 'ip' in attr['inet']:
                mac = attr['mac']
                break
    #if not mac: # XXX:?
    #    mac = get_mac(addr)
    assert mac, addr
    assert_node(host, mac)
    host = settings.node[host]['host']
    assert host
    return host, addr, mac
コード例 #4
0
ファイル: domain1.py プロジェクト: dotmpe/script-mpe
def info():
    """
    determine gateway, and identify node by hardware address
    record new gateways
    """

    # print some stuff
    hostname = get_hostname()
    print("On node:", hostname)
    gateway = get_gateway()
    print('Internet gateway: ')
    default_routes = get_default_route()
    for gateway in default_routes:
        print('\t-', gateway,)
        m = get_mac(gateway)
        if not m:
            print('(invalid)')
            continue
        else:
            try:
                gateway_node = socket.gethostbyaddr(gateway)[0]
            except socket.herror as e:
                print('(invalid: %s)' % e)
                continue
            print(gateway_node, "[ether %s]" % m)

    for gateway in default_routes:
        m = get_mac(gateway)
        if m:
            try:
                gateway_node = socket.gethostbyaddr(gateway)[0]
            except socket.herror as e:
                print(e, file=sys.stderr)
                continue
            if gateway_node not in settings.node:
                print("New gateway: %s" % gateway_node,)
                node = {
                        'mac': m,
                        'ip': gateway,
                    }
                print(node)
                v = raw_input('Insert node? [n] ')
                if 'y' in v:
                    gw_domain = None
                    prompt = 'Domain? '
                    if '.' in gateway_node:
                        p = gateway_node.find('.')
                        gw_domain = gateway_node[p+1:]
                        if gw_domain in settings.domain:
                            prompt += '[%s] ' % gw_domain
                        else:
                            prompt += '[%s +] ' % gw_domain
                    domain = raw_input(prompt)
                    if not domain and gw_domain:
                        domain = gw_domain
                    if domain not in settings.domain:
                        pass # TODO
                    updated = True
                    node['domain'] = domain
                    setattr(settings.node, gateway_node, node)
                    #settings.commit()
            else:
                node = getattr(settings.node, gateway_node)
                if node.mac != m:
                    err("MAC mismatch: %s, %s", node.mac, m)
                    sys.exit()
                assert 'domain' in node
                domain = node.domain

    ifs = parse_ifconfig()
    network = {}
    for iface, ifconf in ifs:
        if 'inet' in ifconf:
            conf = ifconf['inet']
            assert conf['ip'] not in network
            network[conf['ip']] = iface

    fqdn, aliases, addresses = socket.gethostbyname_ex(getfqdn)
    #print socket.gethostbyname_ex(hostname)
    #assert (fqdn, aliases, addresses) == socket.gethostbyname_ex(hostname)
    print("Full address:", fqdn)
    print("Host aliases:")
    for alias in aliases:
        print('\t-', alias)
    print("IP addresses:")
    for address in addresses:
        print('\t-', address,)
        if address in network:
            print(network[address])
        else:
            print