blob: 7147bc5ff234bb806bcca7085eb3e45dd30097f3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#!/usr/bin/python3
import sys
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)
commands = config.to_commands()
except ValueError as e:
print("Could not parse the config file: {0}".format(e), file=sys.stderr)
sys.exit(1)
print(commands)
|