summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorKyrylo Yatsenko <hedrok@gmail.com>2025-10-10 19:15:53 +0300
committerKyrylo Yatsenko <hedrok@gmail.com>2025-10-14 15:05:07 +0300
commitd290b2661f0d71168a4d6b16691f48b7fb741a4c (patch)
treed82adb99f5d39df2af3a2a1f04d6381efac4ae75 /python
parent47d5f21b7770b0b8dcd29272e6c93bea9028adfe (diff)
downloadvyos-1x-d290b2661f0d71168a4d6b16691f48b7fb741a4c.tar.gz
vyos-1x-d290b2661f0d71168a4d6b16691f48b7fb741a4c.zip
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.
Diffstat (limited to 'python')
-rwxr-xr-xpython/vyos/template.py16
1 files changed, 10 insertions, 6 deletions
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):