-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli2json.py
More file actions
227 lines (195 loc) · 7.15 KB
/
cli2json.py
File metadata and controls
227 lines (195 loc) · 7.15 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python
'''
Name: cli2json.py
Description: Read network device CLI command output from text file or stdin and output JSON structured data
Author: David Paneels
Usage: see cli2json.py --help
Automatic parser/template selection:
The parser to use will be infered from the filename:
MyDevice_show-interfaces-status.txt -> "show interfaces status"
MyDevice_show-vlan_12345.txt -> "show vlan"
The device name MUST be first, then the show statement
Stops at the first underscore "_" or dot "." found
Dashes "-" will be replaced by white spaces
"int" will be replaced by "interface" or "interfaces"
"nei" will be replaced by "neighbors"
"det" will be replaced by "detail"
"desc" or "descr" will be replaced by "description"
...
The parser can also be specified explicitely with the "--parser" option
'''
import argparse
import sys
import os
import re
import json
import logging
from ntc_templates.parse import parse_output
# Default options
DEFAULT_OS = "nxos"
DEFAULT_DEVICENAME = "Device"
DEFAULT_PARSER = ""
def get_device_name_from_filename(filename, default=DEFAULT_DEVICENAME):
'''
Extract device name from filename
'''
# The device name starts at the beginning and ends at the first dash or underscore + "sh" (for "show")
if filename == "<stdin>":
device_name = DEFAULT_DEVICENAME
else:
filename = os.path.basename(filename)
r = re.search(r"(.*)[-_]sh", filename)
if r:
device_name = r.groups()[0]
else:
device_name = DEFAULT_DEVICENAME
return device_name
def get_parser_from_filename(filename, os=DEFAULT_OS):
'''
Infer parser name from filename
'''
# The parser name starts at the first "show" statement and ends at the next underscore
r = re.search(r"(show.*?)[_.]", filename)
if r:
s = r.groups()[0]
else:
# Return the default parser DEFAULT_PARSER
return DEFAULT_PARSER
# Replace all dashes with spaces
s = re.sub(r"-", " ", s)
# Replace usual abreviations like "sh" or "int" with the full command
s = re.sub(r"\bsho?\b", "show", s)
s = re.sub(r"\brunn?\b", "running-config", s)
s = re.sub(r"\bspan?\b", "spanning-tree", s)
s = re.sub(r"\bneig?h?b?\b", "neighbors", s)
s = re.sub(r"\bdet\b", "detail", s)
s = re.sub(r"\bdescr?\b", "description", s)
s = re.sub(r"\bsumm?\b", "summary", s)
# Put the dash back in some of those commands
s = re.sub(r"\running config\b", "running-config", s)
s = re.sub(r"\spanning tree\b", "spanning-tree", s)
s = re.sub(r"\bport channel\b", "port-channel", s)
s = re.sub(r"\bfeature set\b", "feature-set", s)
s = re.sub(r"\baccess list\b", "access-list", s)
# Is it interface or interfaceS ?
if "status" in s and "ios" in os:
s = re.sub(r"\bint\b", "interfaces", s)
else:
s = re.sub(r"\bint\b", "interface", s)
# Remove all newlines and whitespaces around the string, just in case
s = s.strip()
return s
def parse_cli_to_json(device_name, os, parser, cli_output, use_genie=False):
'''
Parse CLI output to JSON data
'''
logging.info(f"[+] Parsing CLI from device {device_name} with parser \'{parser}\' (os={os})")
if use_genie:
try:
from genie.conf.base import Device
except:
logging.critical("[!] Could not load Genie module")
sys.exit(1)
device = Device(name=device_name, os=os)
device.custom.abstraction = {"order": ["os"]}
result = device.parse(parser, output=cli_output)
else:
# TextFSM needs CLI prompts to be removed
tmp = cli_output.splitlines()
tmp = [ line for line in tmp if device_name+">" not in line and device_name+"#" not in line ]
cli_output = "\n".join(tmp)
platform = "cisco_" + os
result = parse_output(platform=platform, command=parser, data=cli_output)
return result
def main():
'''
Main program
'''
# Parse command line argument
argparser = argparse.ArgumentParser(
description="Read network device CLI output and print formatted JSON"
)
argparser.add_argument(
"-i",
"--infile",
metavar="inputfile.txt",
required=True,
type=argparse.FileType("r"),
help="text file containing show command output (use '-' for stdin)"
)
argparser.add_argument(
"-o",
"--outfile",
metavar="FILENAME",
help="save output to JSON file"
)
argparser.add_argument(
"--os",
default=DEFAULT_OS,
help=f"network OS which originated the output (default={DEFAULT_OS})"
)
argparser.add_argument(
"--parser",
#default=DEFAULT_PARSER, # if we do this we can't overwrite the default parser later on
help="Parser to use (default=infered from filename)"
)
argparser.add_argument(
"--genie",
action="store_true",
help="Use Genie parsing library instead of ntc-templates (default=False)"
)
argparser.add_argument(
"--indent",
default=2,
help="indentation for JSON output (default=2)"
)
argparser.add_argument(
"--verbose",
action="store_true",
help="print additional information to stderr"
)
args = argparser.parse_args()
# Configure logging
if args.verbose:
logging.basicConfig(format="%(message)s", level=logging.INFO)
else:
logging.basicConfig(format="%(message)s", level=logging.WARNING)
# Load text data from file handler (it has already been opened by argparse)
with args.infile as f:
infilename = f.name
cli_output = f.read()
# If no parser is specified we'll extract it from the filename or fallback to the default parser
device_name = get_device_name_from_filename(infilename)
# Select parser or infer from filename
if args.parser:
parser = args.parser
else:
parser = get_parser_from_filename(infilename)
# Parse CLI output to JSON data
result = parse_cli_to_json(
device_name=device_name,
os=args.os,
parser=parser,
cli_output=cli_output,
use_genie=args.genie
)
logging.info(f"[!] Done parsing device {device_name}, dumping JSON with indent {args.indent}")
# Print JSON to file or stdout and quit
if args.outfile:
with open(args.outfile, "w") as f:
f.write(json.dumps(result, indent=args.indent))
logging.info(f"[!] Done writing JSON to {args.outfile}")
else:
print(json.dumps(result, indent=args.indent))
if __name__ == "__main__":
'''
main()
'''
try:
main()
except KeyboardInterrupt:
print()
sys.exit(1)
except Exception as e:
logging.critical(f"[!] An error occured during the operation ({str(e)})")
sys.exit(1)