Skip to main content
Sign in
Snippets Groups Projects
Commit fc4f1175 authored by OskarStenberg's avatar OskarStenberg
Browse files

Missing file...

parent ee8bebe9
Branches
Tags
No related merge requests found
import sys
import os
import hostinfo.util as util
# File syntax is according to nettools manual:
# IP-adress;hostnamn;kommasepareradeMAC-adresser;DHCP-grupp;LucatID;kommentar
# Where IP-adress and hostnamn are not optional
# There are a few reserved IPs for each network, for now these seem to be standardized as:
RESERVED_IPS = {
'1': 'gateway',
'2': 'ldc-admin',
'3': 'ldc-admin',
'254': 'ldc-monitoring',
'255': 'broadcast',
}
def generate_row(ip,
hostname,
maclist = [],
dhcp_group = "",
owner = "",
comment = ""):
if not ip: #or not hostname:
raise util.HostinfoException("Ip and hostname are non-optional",
where=ip if not ip else hostname)
if type(maclist) is list:
maclist = ','.join(maclist)
pass
template = ("""%s;%s;%s;%s;%s;%s"""
% (ip, hostname, maclist, dhcp_group, owner, comment))
return template
def generate_row_tup(host):
h = list(host)
# Ensure proper length of tuple to unpack
h = h + [""]*(6 - len(h))
ip, hostname, maclist, dhcp_group, owner, comment = h
return generate_row(ip, hostname, maclist, dhcp_group, owner, comment)
def filter_nets(tree, options, ip):
net = util.get_subnet(tree, util.address(ip))
if net:
return (net.nettools[0] is not None and
ip.split('.')[-1] not in RESERVED_IPS)
return False
def emit_hosts(tree, options):
result = []
for iface in tree._host_._interface_:
for ip in iface._ip_:
if ip and ip.address[0]:
result.append((ip.address[0],
iface.name[1],
[iface.ethernet[0] or ""]))
pass
pass
pass
result = list(filter(lambda e: filter_nets(tree, options, e[0]), result))
return result
def subnet_hosts(tree, options):
nets = [util.network(s) for s in tree._subnet_ if s.nettools[0] is not None]
hosts = [str(h) for n in nets for h in n.hosts()]
return list(filter(lambda e: filter_nets(tree, options, e), hosts))
def generate(tree, options):
hosts = emit_hosts(tree, options)
expected_hosts = subnet_hosts(tree, options)
for eh in expected_hosts:
exists = False
for h in hosts:
if h[0] == eh:
exists = True
break
pass
if not exists:
hosts.append((eh, ""))
#print("Appended", eh)
pass
pass
hosts.sort(key=lambda e: [int(v) for v in e[0].split('.')])
output = []
for h in hosts:
output.append(generate_row_tup(h))
return '\n'.join(output)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment