-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_file.py
62 lines (50 loc) · 1.86 KB
/
gen_file.py
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
#!/usr/bin/env python3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import print_function
import os
import sys
import argparse
import sys
import json
from string import Template
def main():
parser = argparse.ArgumentParser(description='Generate config for intrasploit')
parser.add_argument("--template", "-t", type=str,
help="Template to use",
default="config.tmpl"
)
parser.add_argument("--json", "-j", type=str,
help="Additional json to use in template",
default="{}"
)
parser.add_argument("--output", "-u", type=str,
help="Output file",
default="~/.config/intrasploit.ini"
)
args = vars(parser.parse_args())
try:
params = json.loads(args["json"])
except Exception as e:
print("ERROR: Unable to parse json: {}, error: {}".format(args["json"], e))
sys.exit(1)
with open(args["template"], "r") as tmpl_file:
template = tmpl_file.read()
template = Template(template)
try:
output = template.substitute(params)
except KeyError as key:
print("ERROR: Key '{}' was found in template, but not json".format(key))
sys.exit(1)
oname = args["output"]
if oname.startswith("~"):
oname = os.path.join(os.path.expanduser("~"), oname[2:])
dirname = os.path.dirname(oname)
if not os.path.exists(dirname):
os.makedirs(dirname)
with open(oname, "w") as out_file:
out_file.write(output)
print("Wrote file to {}".format(args["output"]))
if __name__ == '__main__':
main()