summaryrefslogtreecommitdiff
path: root/src/op_mode/show_dhcpv6.py
diff options
context:
space:
mode:
authorhagbard-01 <39653662+hagbard-01@users.noreply.github.com>2019-06-27 11:42:04 -0700
committerGitHub <noreply@github.com>2019-06-27 11:42:04 -0700
commit9cf9fbaaa0f6c22483a8ab8ad8f79e34b12fbed0 (patch)
treeb8c729c134af5b4375cfbef2000e6fac1ca3e9af /src/op_mode/show_dhcpv6.py
parentc097bd8a07b7417fa9ea1ea47aa1c3645add9714 (diff)
parent89995dcec3a2a6b9112f11cf1c9e4a1bcb4d66d6 (diff)
downloadvyos-1x-9cf9fbaaa0f6c22483a8ab8ad8f79e34b12fbed0.tar.gz
vyos-1x-9cf9fbaaa0f6c22483a8ab8ad8f79e34b12fbed0.zip
Merge pull request #77 from jjakob/T1470-improve-show-dhcpv6-server-leases
T1470: improve output of "show dhcpv6 server leases"
Diffstat (limited to 'src/op_mode/show_dhcpv6.py')
-rwxr-xr-xsrc/op_mode/show_dhcpv6.py104
1 files changed, 88 insertions, 16 deletions
diff --git a/src/op_mode/show_dhcpv6.py b/src/op_mode/show_dhcpv6.py
index bf73b92ea..f1f5a6a55 100755
--- a/src/op_mode/show_dhcpv6.py
+++ b/src/op_mode/show_dhcpv6.py
@@ -20,11 +20,42 @@ import argparse
import ipaddress
import tabulate
import sys
+import collections
from vyos.config import Config
from isc_dhcp_leases import Lease, IscDhcpLeases
lease_file = "/config/dhcpdv6.leases"
+pool_key = "shared-networkname"
+
+lease_display_fields = collections.OrderedDict()
+lease_display_fields['ip'] = 'IPv6 address'
+lease_display_fields['state'] = 'State'
+lease_display_fields['last_comm'] = 'Last communication'
+lease_display_fields['expires'] = 'Lease expiration'
+lease_display_fields['type'] = 'Type'
+lease_display_fields['pool'] = 'Pool'
+lease_display_fields['iaid_duid'] = 'IAID_DUID'
+
+lease_valid_states = ['all', 'active', 'free', 'expired', 'released', 'abandoned', 'reset', 'backup']
+
+def in_pool(lease, pool):
+ if pool_key in lease.sets:
+ if lease.sets[pool_key] == pool:
+ return True
+
+ return False
+
+def format_hex_string(in_str):
+ out_str = ""
+
+ # if input is divisible by 2, add : every 2 chars
+ if len(in_str) > 0 and len(in_str) % 2 == 0:
+ out_str = ':'.join(a+b for a,b in zip(in_str[::2], in_str[1::2]))
+ else:
+ out_str = in_str
+
+ return out_str
def get_lease_data(lease):
data = {}
@@ -35,27 +66,55 @@ def get_lease_data(lease):
except:
data["expires"] = ""
- data["duid"] = lease.host_identifier_string
+ try:
+ data["last_comm"] = lease.last_communication.strftime("%Y/%m/%d %H:%M:%S")
+ except:
+ data["last_comm"] = ""
+
+ # isc-dhcp records lease declarations as ia_{na|ta|pd} IAID_DUID {...}
+ # where IAID_DUID is the combined IAID and DUID
+ data["iaid_duid"] = format_hex_string(lease.host_identifier_string)
+
+ lease_types_long = {"na": "non-temporary", "ta": "temporary", "pd": "prefix delegation"}
+ data["type"] = lease_types_long[lease.type]
+
+ data["state"] = lease.binding_state
data["ip"] = lease.ip
+ try:
+ data["pool"] = lease.sets[pool_key]
+ except:
+ data["pool"] = ""
+
return data
-def get_leases(leases, state=None):
+def get_leases(leases, state, pool=None, sort='ip'):
leases = IscDhcpLeases(lease_file).get()
- if state is not None:
- leases = list(filter(lambda x: x.binding_state == 'active', leases))
+ if state != 'all':
+ leases = list(filter(lambda x: x.binding_state == state, leases))
- return list(map(get_lease_data, leases))
+ # filter lease by pool name
+ if pool is not None:
+ leases = list(filter(lambda x: in_pool(x, pool), leases))
-def show_leases(leases):
- headers = ["IPv6 address", "Lease expiration", "DUID"]
+ leases = list(map(get_lease_data, leases))
+ if sort == 'ip':
+ leases = sorted(leases, key = lambda k: int(ipaddress.IPv6Address(k['ip'])))
+ else:
+ leases = sorted(leases, key = lambda k: k[sort])
+
+ return leases
+def show_leases(leases):
lease_list = []
for l in leases:
- lease_list.append([l["ip"], l["expires"], l["duid"]])
+ lease_list_params = []
+ for k in lease_display_fields.keys():
+ lease_list_params.append(l[k])
+ lease_list.append(lease_list_params)
- output = tabulate.tabulate(lease_list, headers)
+ output = tabulate.tabulate(lease_list, lease_display_fields.values())
print(output)
@@ -63,11 +122,14 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
- group.add_argument("-l", "--leases", action="store_true", help="Show DHCP leases")
- group.add_argument("-s", "--statistics", action="store_true", help="Show DHCP statistics")
+ group.add_argument("-l", "--leases", action="store_true", help="Show DHCPv6 leases")
+ group.add_argument("-s", "--statistics", action="store_true", help="Show DHCPv6 statistics")
+ group.add_argument("--allowed", type=str, choices=["pool", "sort", "state"], help="Show allowed values for argument")
- parser.add_argument("-p", "--pool", type=str, action="store", help="Show lease for specific pool")
- parser.add_argument("-j", "--json", action="store_true", default=False, help="Product JSON output")
+ parser.add_argument("-p", "--pool", type=str, help="Show lease for specific pool")
+ parser.add_argument("-S", "--sort", type=str, choices=lease_display_fields.keys(), default='ip', help="Sort by")
+ parser.add_argument("-t", "--state", type=str, choices=lease_valid_states, default="active", help="Lease state to show")
+ parser.add_argument("-j", "--json", action="store_true", default=False, help="Produce JSON output")
args = parser.parse_args()
@@ -78,9 +140,19 @@ if __name__ == '__main__':
sys.exit(0)
if args.leases:
- leases = get_leases(lease_file, state='active')
- show_leases(leases)
+ leases = get_leases(lease_file, args.state, args.pool, args.sort)
+
+ if args.json:
+ print(json.dumps(leases, indent=4))
+ else:
+ show_leases(leases)
elif args.statistics:
print("DHCPv6 statistics option is not available")
+ elif args.allowed == 'pool':
+ print(' '.join(c.list_effective_nodes("service dhcpv6-server shared-network-name")))
+ elif args.allowed == 'sort':
+ print(' '.join(lease_display_fields.keys()))
+ elif args.allowed == 'state':
+ print(' '.join(lease_valid_states))
else:
- print("Invalid option")
+ parser.print_help()