2023-09-11 11:15:56 -05:00
|
|
|
import click
|
2023-11-21 10:42:07 -06:00
|
|
|
from tabulate import tabulate
|
2023-09-11 11:15:56 -05:00
|
|
|
import utilities_common.cli as clicommon
|
|
|
|
|
|
|
|
|
2023-11-21 10:42:07 -06:00
|
|
|
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)
|
2023-09-11 11:15:56 -05:00
|
|
|
def dhcp_server():
|
2023-11-21 10:42:07 -06:00
|
|
|
"""Show dhcp_server related info"""
|
2023-09-11 11:15:56 -05:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-11-21 10:42:07 -06:00
|
|
|
@dhcp_server.group(cls=clicommon.AliasedGroup)
|
|
|
|
def ipv4():
|
|
|
|
"""Show ipv4 related dhcp_server info"""
|
2023-09-11 11:15:56 -05:00
|
|
|
pass
|
2023-11-21 10:42:07 -06:00
|
|
|
|
|
|
|
|
|
|
|
@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)
|