summaryrefslogtreecommitdiff
path: root/src/op_mode/dhcp.py
diff options
context:
space:
mode:
authorChristian Breunig <christian@breunig.cc>2023-07-29 22:53:57 +0200
committerChristian Breunig <christian@breunig.cc>2023-07-29 22:56:34 +0200
commit7826a0e35e25e3d0e1a782c9507d49352e3dd720 (patch)
tree69cb91e340f6a553d3d896c7724a10e5099b7402 /src/op_mode/dhcp.py
parent399edb32eb68ce5fa3189ff83f09b307c6e9222d (diff)
downloadvyos-1x-7826a0e35e25e3d0e1a782c9507d49352e3dd720.tar.gz
vyos-1x-7826a0e35e25e3d0e1a782c9507d49352e3dd720.zip
T3355: migrate "show dhcp client lease" to new op-mode style
vyos@vyos# ${vyos_op_scripts_dir}/dhcp.py show_client_leases --family inet --interface eth0.10 Interface eth0.10 IP address 172.16.33.123 [Active] Subnet Mask 255.255.255.0 Domain Name vyos.net Router 172.16.33.254 Name Server 172.16.254.30 DHCP Server 172.16.33.254 DHCP Server 86400 Last Update Sat Jul 29 21:13:32 CEST 2023 Expiry Sun Jul 30 21:13:31 CEST 2023 vyos@vyos# ${vyos_op_scripts_dir}/dhcp.py show_client_leases --family inet --interface eth0.10 --raw [ { "last_update": "Sat Jul 29 21:13:32 CEST 2023", "reason": "BOUND", "interface": "eth0.10", "new_expiry": "1690744411", "new_dhcp_lease_time": "86400", "medium": "", "alias_ip_address": "", "new_ip_address": "172.16.33.123", "new_broadcast_address": "172.16.33.255", "new_subnet_mask": "255.255.255.0", "new_domain_name": "vyos.net", "new_network_number": "172.16.33.0", "new_domain_name_servers": "172.16.254.30", "new_routers": "172.16.33.254", "new_static_routes": "", "new_dhcp_server_identifier": "172.16.33.254", "new_dhcp_message_type": "5", "old_ip_address": "", "old_subnet_mask": "", "old_domain_name": "", "old_domain_name_servers": "", "old_routers": "", "old_static_routes": "" } ]
Diffstat (limited to 'src/op_mode/dhcp.py')
-rwxr-xr-xsrc/op_mode/dhcp.py78
1 files changed, 77 insertions, 1 deletions
diff --git a/src/op_mode/dhcp.py b/src/op_mode/dhcp.py
index 3e51e990b..653e670c5 100755
--- a/src/op_mode/dhcp.py
+++ b/src/op_mode/dhcp.py
@@ -14,10 +14,12 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+import os
import sys
import typing
from datetime import datetime
+from glob import glob
from ipaddress import ip_address
from isc_dhcp_leases import IscDhcpLeases
from tabulate import tabulate
@@ -27,8 +29,9 @@ import vyos.opmode
from vyos.base import Warning
from vyos.configquery import ConfigTreeQuery
-from vyos.utils.process import cmd
from vyos.utils.dict import dict_search
+from vyos.utils.file import read_file
+from vyos.utils.process import cmd
from vyos.utils.process import is_systemd_service_running
config = ConfigTreeQuery()
@@ -287,6 +290,79 @@ def show_server_leases(raw: bool, family: ArgFamily, pool: typing.Optional[str],
return _get_formatted_server_leases(lease_data, family=family)
+def _get_raw_client_leases(family='inet', interface=None):
+ lease_dir = '/var/lib/dhcp'
+ lease_files = []
+ lease_data = []
+
+ if interface:
+ tmp = f'{lease_dir}/dhclient_{interface}.lease'
+ if os.path.exists(tmp):
+ lease_files.append(tmp)
+ else:
+ # All DHCP leases
+ lease_files = glob(f'{lease_dir}/dhclient_*.lease')
+
+ for lease in lease_files:
+ tmp = {}
+ with open(lease, 'r') as f:
+ for line in f.readlines():
+ line = line.rstrip()
+ if 'last_update' not in tmp:
+ tmp.update({'last_update' : line})
+ continue
+
+ k, v = line.split('=')
+ tmp.update({k : v.replace("'", "")})
+
+ lease_data.append(tmp)
+
+ return lease_data
+
+def _get_formatted_client_leases(lease_data, family):
+ from time import localtime
+ from time import strftime
+
+ from vyos.validate import is_intf_addr_assigned
+
+ data_entries = []
+ for lease in lease_data:
+ data_entries.append(["Interface", lease['interface']])
+ if 'new_ip_address' in lease:
+ tmp = '[Active]' if is_intf_addr_assigned(lease['interface'], lease['new_ip_address']) else '[Inactive]'
+ data_entries.append(["IP address", lease['new_ip_address'], tmp])
+ if 'new_subnet_mask' in lease:
+ data_entries.append(["Subnet Mask", lease['new_subnet_mask']])
+ if 'new_domain_name' in lease:
+ data_entries.append(["Domain Name", lease['new_domain_name']])
+ if 'new_routers' in lease:
+ data_entries.append(["Router", lease['new_routers']])
+ if 'new_domain_name_servers' in lease:
+ data_entries.append(["Name Server", lease['new_domain_name_servers']])
+ if 'new_dhcp_server_identifier' in lease:
+ data_entries.append(["DHCP Server", lease['new_dhcp_server_identifier']])
+ if 'new_dhcp_lease_time' in lease:
+ data_entries.append(["DHCP Server", lease['new_dhcp_lease_time']])
+ if 'last_update' in lease:
+ data_entries.append(["Last Update", lease['last_update']])
+ if 'new_expiry' in lease:
+ tmp = strftime('%a %b %d %H:%M:%S %Z %Y', localtime(int(lease['new_expiry'])))
+ data_entries.append(["Expiry", tmp])
+
+ # Add empty marker
+ data_entries.append([''])
+
+ output = tabulate(data_entries, tablefmt='plain')
+
+ return output
+
+def show_client_leases(raw: bool, family: ArgFamily, interface: typing.Optional[str]):
+ lease_data = _get_raw_client_leases(family=family, interface=interface)
+ if raw:
+ return lease_data
+ else:
+ return _get_formatted_client_leases(lease_data, family=family)
+
if __name__ == '__main__':
try:
res = vyos.opmode.run(sys.modules[__name__])