diff options
author | Daniil Baturin <daniil@baturin.org> | 2018-06-08 12:00:11 +0200 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2018-06-08 12:00:11 +0200 |
commit | 6acc5f6cb03f553141b0442f5e01cbf47b8e7833 (patch) | |
tree | 1f881de6841b6ec6c5a1550c2cab066de8b6330d /python | |
parent | 9afd3a8607fe5ce53f3756ea1c2c7045cd7c67b3 (diff) | |
download | vyos-1x-6acc5f6cb03f553141b0442f5e01cbf47b8e7833.tar.gz vyos-1x-6acc5f6cb03f553141b0442f5e01cbf47b8e7833.zip |
T689: add a basic library for working with network interfaces and support for interface types to the completion script.
Diffstat (limited to 'python')
-rw-r--r-- | python/vyos/interfaces.py | 30 |
1 files changed, 30 insertions, 0 deletions
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)) |