sonic-buildimage/dockers/docker-dhcp-server/cli/show/plugins/show_dhcp_server.py
Xichen96 ee38e2447d
[dhcp_server] Add show dhcp_server ipv4 lease (#17125)
* Add show dhcp_server ipv4 lease
* add ut for show dhcp_server ipv4 lease
2023-11-21 08:42:07 -08:00

47 lines
1.3 KiB
Python

import click
from tabulate import tabulate
import utilities_common.cli as clicommon
import ipaddress
from datetime import datetime
def ts_to_str(ts):
return datetime.fromtimestamp(int(ts)).strftime("%Y-%m-%d %H:%M:%S")
@click.group(cls=clicommon.AliasedGroup)
def dhcp_server():
"""Show dhcp_server related info"""
pass
@dhcp_server.group(cls=clicommon.AliasedGroup)
def ipv4():
"""Show ipv4 related dhcp_server info"""
pass
@ipv4.command()
@click.argument('dhcp_interface', required=False)
@clicommon.pass_db
def lease(db, dhcp_interface):
if not dhcp_interface:
dhcp_interface = "*"
headers = ["Interface", "MAC Address", "IP", "Lease Start", "Lease End"]
table = []
dbconn = db.db
for key in dbconn.keys("STATE_DB", "DHCP_SERVER_IPV4_LEASE|" + dhcp_interface + "|*"):
entry = dbconn.get_all("STATE_DB", key)
interface, mac = key.split("|")[1:]
port = dbconn.get("STATE_DB", "FDB_TABLE|" + interface + ":" + mac, "port")
if not port:
port = "<Unknown>"
table.append([interface + "|" + port, mac, entry["ip"], ts_to_str(entry["lease_start"]), ts_to_str(entry["lease_end"])])
click.echo(tabulate(table, headers=headers))
def register(cli):
cli.add_command(dhcp_server)