Beispiel #1
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
Beispiel #2
0
def cmd_update(settings):

    """
    Initialize current host, creating host ID
    if not present yet and record
    hosts interfaces.
    Then determine the current domain, and record current iface IP's.
    Commit data if needed.
    """

    # read host ID file or record node/ifaces
    host = init_host(settings)
    print 'host', host.path()

    gateway, mac, gateway_addr = get_gateway(settings)

    net = get_network(settings, gateway.domain)

    # read SSH pubkey user domain
    #user, domain = init_domain(settings)
    print 'domain', net.path()

    # update host IP's, and update network
    updated = False
    for iface, mac, spec in domainmod.inet_ifaces():
        ip = spec['inet']['ip']
        if mac not in settings.interfaces:
            log.err("Missing iface type for HW-addr '%s'" % mac)
            continue
        iface_type = settings.interfaces[mac].type
        print '\t', iface_type, ip
        # Keep IP on host
        net_host = host.get('net')
        if iface_type not in net_host:
            net_host.get(iface_type, dict(ip=None))
        if net_host[iface_type].ip != ip:
            net_host[iface_type].ip = ip
            updated = True
        # Track the current network
        fulldomain = net.get(host.name)
        if iface_type not in fulldomain:
            fulldomain.get(iface_type, dict(ip=None))
        if fulldomain[iface_type].ip != ip:
            fulldomain[iface_type].ip = ip
            updated = True

    if updated:
        settings.commit()
Beispiel #3
0
def main():
    """
    - check current host is known
    - check gateway (default route) is known
    - set local or mobile domain to gateway internal domain

    Once ready, print hostname or node, local domain and internet domain if
    available. Ie.:

        example network.internal example.net

    Domain may be 'local' while offline (no default route to internet)
    or 'mobile' for unrecognized routes/gateways.
    """
    global settings

    print("""
Local domain check
==================
:Date: %s """ % datetime.datetime.now().isoformat())

    host, addr, mac = get_current_node()
    print(':Host: `%s <%s>`' % (host, mac))

    gateway, gateway_mac, gateway_addr = get_gateway()
    print(':Gateway: `%s <%s>`' % (gateway, gateway_mac))

    network_id = settings.nodes[gateway_mac]
    network = network_name(network_id)

    print(':Network:', network)

    if not gateway:
        err("No internet uplink. ")
        print(host, 'local')
    else:
        pass#print host, gateway

    #print 'ifinfo', pformat(ifinfo)

    settings.commit()
    reload()
Beispiel #4
0
def cmd_olddetect(settings):
    """
    Set host net domain name.
    Detect network using link
    Get full current hostname using gateway,
    update current domain and IP.
    """
    gateway, mac, gateway_addr = get_gateway(settings)
    host = get_current_host(settings)

    for iface in host.interfaces:
        iface_type = settings.interfaces[iface].type
        if iface_type not in gateway.suffix:
            raise Exception("Node %s interface for gateway %s" % (
                iface_type, gateway))
        net = gateway.suffix[iface_type]
        s = net.split('.')
        s.reverse()
        domain = '.'.join(s)
        s.insert(0, host.name.lower())
        domainhost = '.'.join(s)
        d = get_domain(settings, domain, True)
        # print host, IP
        print d.nodes[host.name.lower()], domainhost, host.name.lower()
Beispiel #5
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 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