diff options
author | Daniil Baturin <daniil@baturin.org> | 2019-10-23 17:08:09 +0200 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2019-10-23 17:08:09 +0200 |
commit | 62ea78e3030802e81dd739fc4c45e6a10faddb36 (patch) | |
tree | 66205ac7fa8290e1d6eda1d4c94e57f4f78c1dd1 /python/vyos/interfaces.py | |
parent | d9ee0b95d1020b6d5412dd011ebb1ef7f6ef3fc7 (diff) | |
parent | 3f8884587ab1291c13f633b079058879241ac3c1 (diff) | |
download | vyos-1x-62ea78e3030802e81dd739fc4c45e6a10faddb36.tar.gz vyos-1x-62ea78e3030802e81dd739fc4c45e6a10faddb36.zip |
Merge branch 'current' of https://github.com/vyos/vyos-1x into current
Diffstat (limited to 'python/vyos/interfaces.py')
-rw-r--r-- | python/vyos/interfaces.py | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/python/vyos/interfaces.py b/python/vyos/interfaces.py index d69ce9d04..ecf061d17 100644 --- a/python/vyos/interfaces.py +++ b/python/vyos/interfaces.py @@ -16,9 +16,9 @@ import re import json +import subprocess import netifaces - intf_type_data_file = '/usr/share/vyos/interface-types.json' def list_interfaces(): @@ -54,3 +54,46 @@ def get_type_of_interface(intf): return key raise ValueError("No type found for interface name: {0}".format(intf)) + +def wireguard_dump(): + """Dump wireguard data in a python friendly way.""" + last_device=None + output = {} + + # Dump wireguard connection data + _f = subprocess.check_output(["wg", "show", "all", "dump"]).decode() + for line in _f.split('\n'): + if not line: + # Skip empty lines and last line + continue + items = line.split('\t') + + if last_device != items[0]: + # We are currently entering a new node + device, private_key, public_key, listen_port, fw_mark = items + last_device = device + + output[device] = { + 'private_key': None if private_key == '(none)' else private_key, + 'public_key': None if public_key == '(none)' else public_key, + 'listen_port': int(listen_port), + 'fw_mark': None if fw_mark == 'off' else int(fw_mark), + 'peers': {}, + } + else: + # We are entering a peer + device, public_key, preshared_key, endpoint, allowed_ips, latest_handshake, transfer_rx, transfer_tx, persistent_keepalive = items + if allowed_ips == '(none)': + allowed_ips = [] + else: + allowed_ips = allowed_ips.split('\t') + output[device]['peers'][public_key] = { + 'preshared_key': None if preshared_key == '(none)' else preshared_key, + 'endpoint': None if endpoint == '(none)' else endpoint, + 'allowed_ips': allowed_ips, + 'latest_handshake': None if latest_handshake == '0' else int(latest_handshake), + 'transfer_rx': int(transfer_rx), + 'transfer_tx': int(transfer_tx), + 'persistent_keepalive': None if persistent_keepalive == 'off' else int(persistent_keepalive), + } + return output |