From c64d702d307c6cf99260af5c8c15cf8cb5b4ae87 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Fri, 8 Jun 2018 08:53:47 +0200 Subject: T689: add a new script for 'show hardware cpu summary'. Since the format is common in /proc, make parsing the data a library function. --- python/vyos/util.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++ src/op_mode/cpu_summary.py | 24 ++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 python/vyos/util.py create mode 100755 src/op_mode/cpu_summary.py diff --git a/python/vyos/util.py b/python/vyos/util.py new file mode 100644 index 000000000..9a36ef84f --- /dev/null +++ b/python/vyos/util.py @@ -0,0 +1,50 @@ +import re + + +def colon_separated_to_dict(data_string, uniquekeys=False): + """ Converts a string containing newline-separated entries + of colon-separated key-value pairs into a dict. + + Such files are common in Linux /proc filesystem + + Args: + data_string (str): data string + uniquekeys (bool): whether to insist that keys are unique or not + + Returns: dict + + Raises: + ValueError: if uniquekeys=True and the data string has + duplicate keys. + + Note: + If uniquekeys=True, then dict entries are always strings, + otherwise they are always lists of strings. + """ + key_value_re = re.compile('([^:]+)\s*\:\s*(.*)') + + data_raw = re.split('\n', data_string) + + data = {} + + for l in data_raw: + l = l.strip() + if l: + match = re.match(key_value_re, l) + if match: + key = match.groups()[0].strip() + value = match.groups()[1].strip() + if key in data.keys(): + if uniquekeys: + raise ValueError("Data string has duplicate keys: {0}".format(key)) + else: + data[key].append(value) + else: + if uniquekeys: + data[key] = value + else: + data[key] = [value] + else: + pass + + return data diff --git a/src/op_mode/cpu_summary.py b/src/op_mode/cpu_summary.py new file mode 100755 index 000000000..3da5835e9 --- /dev/null +++ b/src/op_mode/cpu_summary.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 + +import re + +from vyos.util import colon_separated_to_dict + + +FILE_NAME = '/proc/cpuinfo' + +with open(FILE_NAME, 'r') as f: + data_raw = f.read() + +data = colon_separated_to_dict(data_raw) + +# Accumulate all data in a dict for future support for machine-readable output +cpu_data = {} +cpu_data['cpu_number'] = len(data['processor']) +cpu_data['models'] = list(set(data['model name'])) + +# Strip extra whitespace from CPU model names, /proc/cpuinfo is prone to that +cpu_data['models'] = map(lambda s: re.sub('\s+', ' ', s), cpu_data['models']) + +print("CPU(s): {0}".format(cpu_data['cpu_number'])) +print("CPU model(s): {0}".format(",".join(cpu_data['models']))) -- cgit v1.2.3 From 873e9ef110039289b3554e4e579bbaa249f9ca77 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Fri, 8 Jun 2018 09:12:03 +0200 Subject: T652: user encrypted-key not possible without engineid --- src/conf_mode/snmp.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/conf_mode/snmp.py b/src/conf_mode/snmp.py index 7623206b4..83ae27bbd 100755 --- a/src/conf_mode/snmp.py +++ b/src/conf_mode/snmp.py @@ -634,6 +634,9 @@ def verify(snmp): if user['privPassword'] == '' and user['privMasterKey'] == '': raise ConfigError('Must specify encrypted-key or plaintext-key for user privacy') + if user['privMasterKey'] and user['engineID'] == '': + raise ConfigError('Can not have "encrypted-key" without engineid') + if user['authPassword'] == '' and user['authMasterKey'] == '' and user['privTsmKey'] == '': raise ConfigError('Must specify auth or tsm-key for user auth') -- cgit v1.2.3 From a4b60ec2056662767b2736206d8f3be00f94a275 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Fri, 8 Jun 2018 10:36:53 +0200 Subject: T652: import SNMP keys from volatile into nonvolatile location --- src/conf_mode/snmp.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/conf_mode/snmp.py b/src/conf_mode/snmp.py index 83ae27bbd..863f7e2e2 100755 --- a/src/conf_mode/snmp.py +++ b/src/conf_mode/snmp.py @@ -18,6 +18,7 @@ import sys import os +import shutil import stat import pwd import time @@ -46,7 +47,6 @@ OIDs = { 'des' : '.1.3.6.1.6.3.10.1.2.2', 'none': '.1.3.6.1.6.3.10.1.2.1' } - # SNMPS template - be careful if you edit the template. client_config_tmpl = """ ### Autogenerated by snmp.py ### @@ -714,12 +714,30 @@ def generate(snmp): def apply(snmp): if snmp is not None: - if not os.path.exists('/config/snmp/tls'): - os.makedirs('/config/snmp/tls') - os.chmod('/config/snmp/tls', stat.S_IWUSR | stat.S_IRUSR) + nonvolatiledir = '/config/snmp/tls' + volatiledir = '/etc/snmp/tls' + if not os.path.exists(nonvolatiledir): + os.makedirs(nonvolatiledir) + os.chmod(nonvolatiledir, stat.S_IWUSR | stat.S_IRUSR) # get uid for user 'snmp' snmp_uid = pwd.getpwnam('snmp').pw_uid - os.chown('/config/snmp/tls', snmp_uid, -1) + os.chown(nonvolatiledir, snmp_uid, -1) + + # move SNMP certificate files from volatile location to non volatile /config/snmp + if os.path.exists(volatiledir) and os.path.isdir(volatiledir): + files = os.listdir(volatiledir) + for f in files: + shutil.move(volatiledir + '/' + f, nonvolatiledir) + os.chmod(nonvolatiledir + '/' + f, stat.S_IWUSR | stat.S_IRUSR) + + os.rmdir(volatiledir) + os.symlink(nonvolatiledir, volatiledir) + + if os.path.islink(volatiledir): + link = os.readlink(volatiledir) + if link != nonvolatiledir: + os.unlink(volatiledir) + os.symlink(nonvolatiledir, volatiledir) # start SNMP daemon os.system("sudo systemctl restart snmpd.service") -- cgit v1.2.3 From 9afd3a8607fe5ce53f3756ea1c2c7045cd7c67b3 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Fri, 8 Jun 2018 10:40:39 +0200 Subject: snmp.xml: make help messages match net-snmp naming --- interface-definitions/snmp.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interface-definitions/snmp.xml b/interface-definitions/snmp.xml index 7928de5d7..76885a02a 100644 --- a/interface-definitions/snmp.xml +++ b/interface-definitions/snmp.xml @@ -216,11 +216,11 @@ auth - Messages are authenticated but not encrypted (AuthNoPriv) + Messages are authenticated but not encrypted (authNoPriv) priv - Messages are authenticated and encrypted (AuthPriv) + Messages are authenticated and encrypted (authPriv) (noauth|auth|priv) -- cgit v1.2.3 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 From e916c9a55ffdff970c1796829d1bee319802b353 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Fri, 8 Jun 2018 20:50:12 +0200 Subject: Move the version.json file used by newer code to /usr/share/vyos --- python/vyos/defaults.py | 3 +++ python/vyos/version.py | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 python/vyos/defaults.py diff --git a/python/vyos/defaults.py b/python/vyos/defaults.py new file mode 100644 index 000000000..43f222cc7 --- /dev/null +++ b/python/vyos/defaults.py @@ -0,0 +1,3 @@ +directories = { + "data": "/usr/share/vyos/" +} diff --git a/python/vyos/version.py b/python/vyos/version.py index 5d32d878d..545d4e76b 100644 --- a/python/vyos/version.py +++ b/python/vyos/version.py @@ -17,9 +17,14 @@ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +import os import json -def get_version_data(file='/opt/vyatta/etc/version.json'): +import vyos.defaults + +version_file = os.path.join(vyos.defaults.directories['data'], 'version.json') + +def get_version_data(file=version_file): with open(file, 'r') as f: version_data = json.load(f) return version_data -- cgit v1.2.3