From 68a041e2b9b052eec479bbb8426551c45a35ec1a Mon Sep 17 00:00:00 2001
From: Oskar Stenberg <oskar.stenberg@control.lth.se>
Date: Wed, 26 Mar 2025 17:35:03 +0100
Subject: [PATCH] Enforce non-empty hostnames, get fqdn and base fqdn on ip
name-tag if applicable
---
src/hostinfo/nettools.py | 35 ++++++++++++-----------------------
1 file changed, 12 insertions(+), 23 deletions(-)
diff --git a/src/hostinfo/nettools.py b/src/hostinfo/nettools.py
index 8d21571..8b59cca 100644
--- a/src/hostinfo/nettools.py
+++ b/src/hostinfo/nettools.py
@@ -1,5 +1,4 @@
-import sys
-import os
+import datetime
import hostinfo.util as util
# File syntax is according to nettools manual:
# IP-adress;hostnamn;kommasepareradeMAC-adresser;DHCP-grupp;LucatID;kommentar
@@ -36,7 +35,8 @@ 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
+ ip, hostname, maclist, dhcp_group, owner, _ = h
+ comment = datetime.datetime.now().isoformat(timespec='seconds')
return generate_row(ip, hostname, maclist, dhcp_group, owner, comment)
def filter_nets(tree, options, ip):
@@ -51,8 +51,16 @@ def emit_hosts(tree, options):
for iface in tree._host_._interface_:
for ip in iface._ip_:
if ip and ip.address[0]:
+ try:
+ fqdn = util.fqn(iface, tree)[0:-1]
+ if ip.name[0]:
+ parts = fqdn.split('.')
+ parts[0] = ip.name[0]
+ fqdn = '.'.join(parts)
+ except:
+ fqdn = iface.name[1] + ".control.lth.se"
result.append((ip.address[0],
- iface.name[1],
+ fqdn,
[iface.ethernet[0] or ""]))
pass
pass
@@ -61,27 +69,8 @@ def emit_hosts(tree, options):
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 = []
--
GitLab