From d290b2661f0d71168a4d6b16691f48b7fb741a4c Mon Sep 17 00:00:00 2001 From: Kyrylo Yatsenko Date: Fri, 10 Oct 2025 19:15:53 +0300 Subject: T5942: Improve current DHCP router obtaining Change vyos.template.get_dhcp_router to use '*.lease' file instead of '.leases' file - in 'leases' there can be several last leases and current one may not be the first one in file. '*.lease' file contains current lease. --- python/vyos/template.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'python') diff --git a/python/vyos/template.py b/python/vyos/template.py index 824d42136..f384f752d 100755 --- a/python/vyos/template.py +++ b/python/vyos/template.py @@ -422,21 +422,25 @@ def is_file(filename): @register_filter('get_dhcp_router') def get_dhcp_router(interface): - """ Static routes can point to a router received by a DHCP reply. This + """Static routes can point to a router received by a DHCP reply. This helper is used to get the current default router from the DHCP reply. - Returns False of no router is found, returns the IP address as string if + Returns None if no router is found, returns the IP address as string if a router is found. """ - lease_file = directories['isc_dhclient_dir'] + f'/dhclient_{interface}.leases' + lease_file = directories['isc_dhclient_dir'] + f'/dhclient_{interface}.lease' if not os.path.exists(lease_file): return None from vyos.utils.file import read_file for line in read_file(lease_file).splitlines(): - if 'option routers' in line: - (_, _, address) = line.split() - return address.rstrip(';') + if 'new_routers' in line: + (_, address, _) = line.split("'") + if not address: + return None + # Take first one if there are several + address = address.split()[0] + return address @register_filter('natural_sort') def natural_sort(iterable): -- cgit v1.2.3