From 6acc5f6cb03f553141b0442f5e01cbf47b8e7833 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Fri, 8 Jun 2018 12:00:11 +0200 Subject: T689: add a basic library for working with network interfaces and support for interface types to the completion script. --- data/interface-types.json | 17 +++++++++++++++++ debian/rules | 5 +++++ python/vyos/interfaces.py | 30 ++++++++++++++++++++++++++++++ src/completion/list_interfaces.py | 31 +++++++++++++++++++++++++++---- 4 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 data/interface-types.json create mode 100644 python/vyos/interfaces.py diff --git a/data/interface-types.json b/data/interface-types.json new file mode 100644 index 000000000..c452122af --- /dev/null +++ b/data/interface-types.json @@ -0,0 +1,17 @@ +{ + "loopback": "lo", + "dummy": "dum", + "ethernet": "eth", + "bonding": "bond", + "bridge": "br", + "pseudo-ethernet": "peth", + "openvpn": "vtun", + "tunnel": "tun", + "vti": "vti", + "l2tpv3": "l2tpeth", + "vxlan": "vxlan", + "wireless": "wireless", + "wirelessmodem": "wlm", + "input": "ifb", + "pppoe": "pppoe" +} diff --git a/debian/rules b/debian/rules index 5bb6b5371..d284471ec 100755 --- a/debian/rules +++ b/debian/rules @@ -4,6 +4,7 @@ DIR := debian/vyos-1x VYOS_SBIN_DIR := usr/sbin/ VYOS_BIN_DIR := usr/bin/ VYOS_LIBEXEC_DIR := usr/libexec/vyos +VYOS_DATA_DIR := /usr/share/vyos VYOS_CFG_TMPL_DIR := /opt/vyatta/share/vyatta-cfg/templates VYOS_OP_TMPL_DIR := /opt/vyatta/share/vyatta-op/templates @@ -54,3 +55,7 @@ override_dh_auto_install: # Install operational command definitions mkdir -p $(DIR)/$(VYOS_OP_TMPL_DIR) cp -r templates-op/* $(DIR)/$(VYOS_OP_TMPL_DIR) + + # Install data files + mkdir -p $(DIR)/$(VYOS_DATA_DIR) + cp -r data/* $(DIR)/$(VYOS_DATA_DIR) diff --git a/python/vyos/interfaces.py b/python/vyos/interfaces.py new file mode 100644 index 000000000..0759aaa2b --- /dev/null +++ b/python/vyos/interfaces.py @@ -0,0 +1,30 @@ +import re +import json + +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)) diff --git a/src/completion/list_interfaces.py b/src/completion/list_interfaces.py index 59c9dffad..a4968c52f 100755 --- a/src/completion/list_interfaces.py +++ b/src/completion/list_interfaces.py @@ -1,8 +1,31 @@ #!/usr/bin/env python3 -import netifaces +import sys +import argparse -if __name__ == '__main__': - interfaces = netifaces.interfaces() +import vyos.interfaces - print(" ".join(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") + +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 +else: + interfaces = vyos.interfaces.list_interfaces() + +print(" ".join(interfaces)) -- cgit v1.2.3