diff --git a/python/vyos/interfaces.py b/python/vyos/interfaces.py index ecf061d17..37c093aca 100644 --- a/python/vyos/interfaces.py +++ b/python/vyos/interfaces.py @@ -1,99 +1,99 @@ # Copyright 2018 VyOS maintainers and contributors <maintainers@vyos.io> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library. If not, see <http://www.gnu.org/licenses/>. import re import json import subprocess import netifaces intf_type_data_file = '/usr/share/vyos/interface-types.json' def list_interfaces(): interfaces = netifaces.interfaces() # Remove "fake" interfaces associated with drivers for i in ["dummy0", "ip6tnl0", "tunl0", "ip_vti0", "ip6_vti0"]: try: interfaces.remove(i) except ValueError: pass return interfaces def list_interfaces_of_type(typ): with open(intf_type_data_file, 'r') as f: types_data = json.load(f) all_intfs = list_interfaces() if not (typ in types_data.keys()): raise ValueError("Unknown interface type: {0}".format(typ)) else: r = re.compile('^{0}\d+'.format(types_data[typ])) return list(filter(lambda i: re.match(r, i), all_intfs)) def get_type_of_interface(intf): with open(intf_type_data_file, 'r') as f: types_data = json.load(f) for key,val in types_data.items(): r = re.compile('^{0}\d+'.format(val)) if re.match(r, 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 diff --git a/src/completion/list_interfaces.py b/src/completion/list_interfaces.py index 84d17f89f..47eeaf00c 100755 --- a/src/completion/list_interfaces.py +++ b/src/completion/list_interfaces.py @@ -1,51 +1,53 @@ #!/usr/bin/env python3 import sys import argparse import vyos.interfaces parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group() group.add_argument("-t", "--type", type=str, help="List interfaces of specific type") group.add_argument("-b", "--broadcast", action="store_true", help="List all broadcast interfaces") group.add_argument("-br", "--bridgeable", action="store_true", help="List all bridgeable interfaces") group.add_argument("-bo", "--bondable", action="store_true", help="List all bondable interfaces") args = parser.parse_args() if args.type: try: interfaces = vyos.interfaces.list_interfaces_of_type(args.type) - + except ValueError as e: print(e, file=sys.stderr) print("") elif args.broadcast: eth = vyos.interfaces.list_interfaces_of_type("ethernet") bridge = vyos.interfaces.list_interfaces_of_type("bridge") bond = vyos.interfaces.list_interfaces_of_type("bonding") interfaces = eth + bridge + bond elif args.bridgeable: eth = vyos.interfaces.list_interfaces_of_type("ethernet") bond = vyos.interfaces.list_interfaces_of_type("bonding") l2tpv3 = vyos.interfaces.list_interfaces_of_type("l2tpv3") openvpn = vyos.interfaces.list_interfaces_of_type("openvpn") vxlan = vyos.interfaces.list_interfaces_of_type("vxlan") wireless = vyos.interfaces.list_interfaces_of_type("wireless") tunnel = vyos.interfaces.list_interfaces_of_type("tunnel") + wireless = vyos.interfaces.list_interfaces_of_type("wireless") + interfaces = eth + bond + l2tpv3 + openvpn + vxlan + wireless + tunnel elif args.bondable: eth = vyos.interfaces.list_interfaces_of_type("ethernet") # we need to filter out VLAN interfaces identified by a dot (.) in their name for intf in eth: if '.' in intf: eth.remove(intf) interfaces = eth else: interfaces = vyos.interfaces.list_interfaces() print(" ".join(interfaces))