-
Notifications
You must be signed in to change notification settings - Fork 0
/
fwtest.py
executable file
·66 lines (60 loc) · 1.71 KB
/
fwtest.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
63
64
65
66
#!/usr/bin/python3
import sys
import json
import glob
import os
import collections
import csv
import sys
cfg_file_name = "frameworks.json"
web_dir = "/var/www"
exceptions = ['cgi-bin', 'html', 'log']
hostname = os.getenv('HOSTNAME')
# get the path of the script
p = sys.argv[0]
# load the frameworks config file
wp = os.path.dirname(p)
cfg_file = os.path.join(wp, cfg_file_name)
with open(cfg_file, 'r') as f:
cfg = json.load(f)
# get a list of folders in the web folder
web_folders = []
wd = os.path.join(web_dir, '*')
for f in sorted(glob.glob(wd)):
if os.path.basename(f) in exceptions:
continue
if os.path.isdir(f):
web_folders.append(f)
# browse through every folder
keys = list(cfg.keys())
frameworks = collections.OrderedDict()
for web_folder in web_folders:
fws = []
wf = os.path.join(web_folder, '**')
# print(wf)
# go through all items in folder
for thing in sorted(glob.glob(wf, recursive=True)):
# skip symlinks
if os.path.islink(thing):
continue
name = os.path.basename(thing)
if (name in keys) and (cfg[name] not in fws):
fws.append(cfg[name])
if not fws:
fws.append('Unknown')
frameworks[web_folder] = fws
#print('\n', 30 * '-')
out_file_name = hostname + "_" + "frameworks.csv"
out_file = os.path.join(wp, out_file_name)
with open(out_file, mode='w', newline='') as csv_file:
field_names = ['Host', 'Folder', 'Frameworks']
writer = csv.writer(csv_file, delimiter=',')
writer.writerow(field_names)
for k in frameworks:
f = ','.join(frameworks[k])
print(f"{k} - {f}")
l = []
l.append(hostname)
l.append(k)
l.append(f)
writer.writerow(l)