-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncat_xmlconfig
94 lines (72 loc) · 2.95 KB
/
ncat_xmlconfig
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright (C) 2014 David Vavra (vavra.david@email.cz)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
from pyrage.logger import Logger
from mako.template import Template
from mako.lookup import TemplateLookup
import sys
import argparse
from pyrage.netparse import Device
from pyrage.netparse import XML_NetworkParser
from pyrage.utils import InvalidData
def main():
"""
Handle input args
"""
argParser=argparse.ArgumentParser(description="Router audit tool rulebase customizer.")
argParser.add_argument('-r','--rulesFile_template', help="Name of the mako template file in directory chosen with the -t option")
argParser.add_argument('-n','--networkDef', help="Location of the network definition XML file")
argParser.add_argument('device_cfg',nargs='+')
inputArgs=argParser.parse_args(sys.argv[1:])
logger = Logger('warning')
"""
Try to parse specified XML file.
"""
parser = XML_NetworkParser(logger,inputArgs.networkDef)
deviceInstances={}
for dev in inputArgs.device_cfg:
deviceInstances[dev]=Device(dev)
try:
parser.parseDevice(deviceInstances[dev])
except InvalidData as e:
""" remove incorrectly parsed device """
#deviceInstances.pop(dev)
logger.log('critical',dev+str(e))
raise SystemExit(1)
"""
Generate ncat rules for every specified device.
"""
try:
mkt=Template(open(inputArgs.rulesFile_template).read(),
input_encoding='utf-8',
output_encoding='utf-8',
default_filters=['decode.utf8'],strict_undefined=True)
except Exception as e:
logger.log('critical', 'Unable to open template file! {0}'.format(str(e)))
raise SystemExit(2)
for dev,instance in deviceInstances.items():
f=open('{0}.rules'.format(dev),'w')
try:
f.write(mkt.render(
device=instance,
**instance.instances))
f.close()
except Exception as e:
logger.log('critical',dev+':'+ e.message)
raise SystemExit(3)
sys.stdout.write("Generated rules saved in '{0}.rules'.\n".format(dev))
if __name__=="__main__":
main()