summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2019-10-31 02:37:54 +0100
committerChristian Poessinger <christian@poessinger.com>2019-10-31 02:37:54 +0100
commitcd6fc1182e649a72e842567d9aaa6dd39f304e2a (patch)
tree02909b62bdc5945c90c402d3def901591c5b5314 /src/utils
parent9805067e9d80ec1676cf2c2039be3136401c9fa3 (diff)
parentc5aefce07b1f505a1796c440801459112ed734d1 (diff)
downloadvyos-1x-cd6fc1182e649a72e842567d9aaa6dd39f304e2a.tar.gz
vyos-1x-cd6fc1182e649a72e842567d9aaa6dd39f304e2a.zip
Merge branch 'current' of github.com:vyos/vyos-1x into equuleus
* 'current' of github.com:vyos/vyos-1x: [conf completion]: T1779: Add tunnels to completion [XML templates] T1772: Changed old hacks to proper regex, according to the fix Add a function for retrieving config dicts. snmp: make script extension code more readable snmp: use proper stat literals on chmod() snmp: fix verify() indent on script extensions snmp: fix verify() bail out early order snmp: T1738: cleanup import statements T1759: Fixing dependency bug from previous commit T1773, T1774: add a show config operation with JSON and raw options. T1759: Merging interface.py into ifconfig.py Allow list arguments in the vyos.config show_config() function. Replace the try and wait for segfault approach with explicit inSession check. T1773: add a script for converting the config to JSON. It also exposes those functions in vyos.configtree [XML templates] T1772: Add escaping of `\` symbol in `<regex>`
Diffstat (limited to 'src/utils')
-rwxr-xr-xsrc/utils/vyos-config-to-json40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/utils/vyos-config-to-json b/src/utils/vyos-config-to-json
new file mode 100755
index 000000000..e03fd6a59
--- /dev/null
+++ b/src/utils/vyos-config-to-json
@@ -0,0 +1,40 @@
+#!/usr/bin/python3
+
+import sys
+import json
+
+from signal import signal, SIGPIPE, SIG_DFL
+from vyos.configtree import ConfigTree
+
+signal(SIGPIPE,SIG_DFL)
+
+config_string = None
+if (len(sys.argv) == 1):
+ # If no argument given, act as a pipe
+ config_string = sys.stdin.read()
+else:
+ file_name = sys.argv[1]
+ try:
+ with open(file_name, 'r') as f:
+ config_string = f.read()
+ except OSError as e:
+ print("Could not read config file {0}: {1}".format(file_name, e), file=sys.stderr)
+
+# This script is usually called with the output of "cli-shell-api showCfg", which does not
+# escape backslashes. "ConfigTree()" expects escaped backslashes when parsing a config
+# string (and also prints them itself). Therefore this script would fail.
+# Manually escape backslashes here to handle backslashes in any configuration strings
+# properly. The alternative would be to modify the output of "cli-shell-api showCfg",
+# but that may be break other things who rely on that specific output.
+config_string = config_string.replace("\\", "\\\\")
+
+try:
+ config = ConfigTree(config_string)
+ json_str = config.to_json()
+ # Pretty print
+ json_str = json.dumps(json.loads(json_str), indent=4, sort_keys=True)
+except ValueError as e:
+ print("Could not parse the config file: {0}".format(e), file=sys.stderr)
+ sys.exit(1)
+
+print(json_str)